need help with sed problem

Tony Pursell ajp at princeswalk.fsnet.co.uk
Fri Mar 18 18:22:36 UTC 2011


On Fri, 2011-03-18 at 17:47 +0000, Joao Ferreira gmail wrote:
> Hello all
> 
> I need to use sed to replace a given line in a text file with the
> current working directory. But this is getting quite tricky. Problem
> ilustrated below:
> 
> ====================================
> $ cat text.txt 
> aaaa
> bbbb:/some/wrong/path/
> cccc
> 
> $ sed s/bbbb.*/bbbb:$TERM/ text.txt
> aaaa
> bbbb:xterm
> cccc
> 
> $ sed s/bbbb.*/bbbb:$PWD/ text.txt
> sed: -e expression #1, char 16: unknown option to `s'
> =====================================
> 
> I'd like to have
> 
> ====================================
> aaaa
> bbbb:/home/myself
> cccc
> ====================================
> 
> Can anyone help ? thanks in advance.
> 
> Joao
> 
> 
> 

This is what you want

sed /^bbbb.*/s?.*?bbbb:$PWD? txt.txt > newtxt.txt

Note:
/bbbb.*/ addresses the line you want to change.

The error you got is due to $PWD being expanded to /home/myself *before*
the command is executed.  The way round it is to use the little known
facility to use any character instead of / in the substitute command (I
have used ?).

Tony

  








More information about the ubuntu-users mailing list