delete line upto the nth occurence of a particular charachter.

hi all

i want to delete a line upto a particular character. here is example.

cp cms/images/wifi-zone.png

i want to delete the line till . (cp cms/images/wifi-zone.) so the output wud be "png" only

how can i do it?

also please note down that dot (.) can also occur multiple times. so i need that delete the whole line till the occurrence of last dot (.) in the line.

e.g

input wud be...

cp cms/images/wifi-zone.css.php

and output wud be

php

Thanks in advance.

kashif

# with shell:
$> A='cp cms/images/wifi-zone.css.php'
$> echo ${A##*.}
php
with sed:
$> echo 'cp cms/images/wifi-zone.css.php'| sed 's/.*\.\([^.]*\)$/\1/'
php

---------- Post updated at 12:48 PM ---------- Previous update was at 12:48 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

$sed 's/.*\.//g'
cp cms/images/wifi-zone.css.php
php

Entered input in Stdin in the above execution.

Thank You very much guys. both solutions are working fine. this is really awesome. Hats Off for you people.

 echo 'cp cms/images/wifi-zone.css.php'| sed 's/.*\.//'
 echo 'cp cms/images/wifi-zone.css.php'| awk -F. '{print $NF}'
echo 'cp cms/images/wifi-zone.css.php'| grep -o '[^.]*$'

Thank You all. now i got the totally opposite scenario.

i need that keep the line till the occurrence of last slash (/) and delete anything after that.

e.g

input wud be...

cp cms/images/wifi-zone.css.php

and output wud be

cp cms/images/

Thanks in advance.

Solution 1

$ dirname "cp cms/images/wifi-zone.css.php"
cp cms/images

Solution 2

$ sed 's/\/[^\/]\+$//'
cp cms/images/wifi-zone.css.php
cp cms/images

You can use parameter expansion

# MYPATH=cms/images/wifi-zone.png
# echo ${MYPATH##*/}
wifi-zone.png
# echo ${MYPATH%/*}/
cms/images/

or use basename and dirname :cool:

awk -F/ '{print $1"/"$2"/"}'

hi,

Thanks for the reply. its working fine. but just for more readability i dont want remove / at the end of line.
e.g

input wud be...

cp cms/images/wifi-zone.css.php

and output should be

cp cms/images/

currently out put using

sed 's/\/[^\/]\+$//' 

is

cp cms/images

(/ is not there at the end).

so i just want to remove everything After slash (/) which will not include slash itself.

Should be something like:

sed 's!\(.*/\).*!\1!'

Regards

sed 's/\/[^\/]\+$/\//'

Thank You All for Efforts and quick reply.