sed script and sed command line give different results
I want to replace the 3rd occurrence of a single pipe "|" in some text with a double pipe "||". I can do it with a simple command line but not with a script. May I ask why?
Code:
$ cat sample.txt |
One way is to count to the third pipe:
Code:
sed -r -e 's/^(([^|]+\|){3})\|/\1/' |
Thanks Turbocapitalist but I would like to know why my own script does not work. Also I would prefer to avoid the -r option because all my other sed scripts are not compatible with it. I haven't tried perl for the same reason: all my scripts are with sed already. Anyway the sed command you suggest does not seem to work:
Code:
$ sed -r -e 's/^(([^|]+\|){3})\|/\1/' sample.txt |
you ought to use set -xv in this case:
Code:
$ set -xv From the other hand you don't need to escape anything at all. Code:
echo 'field1 | field2 | field4 | field5 | field6' | sed 's/ | / || /3' Code:
s/ | / || /3 |
The embedded sed script is in a 'string'. After a ' the following ' ends it.
Code:
sed 's/ '\|' / \|\| /3' sample.txt Code:
s/ | / \|\| /3 Code:
s/ '\|' / \|\| /3 Code:
sed 's/ | / || /3' sample.txt Code:
sed -r 's/ \| / || /3' sample.txt Code:
#!/bin/sed -f Code:
#!/bin/sed -rf |
Quote:
|
Thank you all.
|
if you wish to say thanks just click on yes
|
All times are GMT -5. The time now is 06:07 PM. |