1834346 Members
1851 Online
110066 Solutions
New Discussion

help with sed

 
chinnaga
Advisor

help with sed

Hi Guys,

I am trying to read a line and replace 2nd occourring string using sed, but I am not able to do it. Can u pls help me with this. Or is there any other way I can do it.

here is the senario.
echo 00015-90002ALL|N|CE|ALL|IN|19981001|120540| ...now I want to replace this |ALL| with some other string.

The end result should be something like this.

00015-90002ALL|N|CE|AB|IN|19981001|120540|
4 REPLIES 4
Steven Schweda
Honored Contributor

Re: help with sed

td176> echo '00015-90002ALL|N|CE|ALL|IN|19981001|120540|' | sed -e 's/|ALL|/|ab|/'
00015-90002ALL|N|CE|ab|IN|19981001|120540|

As you've show it, only the second "ALL" is
bounded by "|" on both sides, so replace that
whole thing ("|ALL|").
chinnaga
Advisor

Re: help with sed

Hi Steve,

This is the command I used

echo $staging_product_load | sed -e 's/'"|ALL|"'/'"$cntry"'' >> file.txt

I am using it inside a shell script, and its a C shell
but its giving me error
sed: Function s/|ALL|/BP cannot be parsed.
Steven Schweda
Honored Contributor

Re: help with sed

> sed: Function s/|ALL|/BP cannot be parsed.

Missing the final "/". Also, you seem to
have gone a little nuts with the quotation.
How about this (assuming that "$cntry"
doesn't have the vertical bars already)?:

echo "$staging_product_load" | sed -e "s/|ALL|/|$cntry|/"

Of course, if you expect a lot of exotic
characters in the strings, more complicated
quotation could be needed.

td176> cntry=ab
td176> echo "00015-90002ALL|N|CE|ALL|IN|19981001|120540|" | sed -e "s/|ALL|/|$cntry|/"
00015-90002ALL|N|CE|ab|IN|19981001|120540|
chinnaga
Advisor

Re: help with sed

Thanks a lot Steve,

I solved the problem ...now its working. I made minor modification from u'r previous reply. Its working fine.

Thanks a lot.