Monday, December 27, 2010

Using sed to delete line with text containing file path

Hi

It took me a day to figure out, how can i delete a line in file using sed. This was pretty simple.

#sed 's/your_string//g'

Now if your string is in a variable than you are going to use is.


#sed 's/$var//g'

**Now problem was your variable contains a path of a file 'say /etc/passwd '
It really doesn't works simply. because of '/' constraints. 

I figured it out to be worked as....

sed -i "\#$i#d" $infile

This way value in variable $i will be deleted from file "$infile" 
This is not very easily available on net.




Some more sed important examples...
-> To delete blank line from a text file.
sed '/^$/d'

This is to remove all spaces from the file.

sed 's/ //g'

No comments:

Post a Comment