[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Re: need help with sed problem



> i agree with that - the underscore that was used is also valid. you
> might look at proper quoting of variables to avoid this. something
> like cat text.txt | sed -e 's/bbb.*/bbbb:"$PWD"/' > new.txt

The output from that, given Joao's original text.txt, is

aaaa
bbbb:"$PWD"
cccc

The reason is that " and $ have no special effect inside ''.

I would prefer to use some other character for the s command delimiter
as earlier posters in this thread suggested, because it is easier to
read. But another way you can do it is to escape all the backslashes
in $PWD using a shell expansion:

$ sed "s/bbbb.*/bbbb:${PWD//\//\\/}/" text.txt
aaaa
bbbb:/home/clive/temp
cccc

You can see its effect on the string passed to sed using set -x:
$ (set -x; sed s/bbbb.*/bbbb:${PWD//\//\\/}/ text.txt)
+ sed 's/bbbb.*/bbbb:\/home\/clive\/temp/' text.txt

A brief explanation of ${PWD//\//\\/} :-
${PWD//A/B} replaces all occurrences of pattern A with string B.
For A use \/ - the \ prevents / being treated as a delimiter.
For B use \\/ - the first \ causes the second \ to be taken literally.

I hope this helps.

-- 
Cheers,
Clive


Reply to: