Operating System - HP-UX
1835819 Members
3705 Online
110085 Solutions
New Discussion

Chaning a string using sed giving parse error

 
Enda Martin_1
Occasional Contributor

Chaning a string using sed giving parse error

Hi,
I'm trying to chg a string in a file with another string within a loop, but am getting a parse error on the sed operation :

for e in `cat $hm/l1`
do
e0=`$hm/i1 $e`
sed -e 's/'$e'/'$e0'/g' $hm/dw > $hm/dw1
done

Error is
sed: Function s/030113/ Mon cannot be parsed.
sed: Function s/030114/ Tue cannot be parsed.

where file l1 contains
030113
030114
and job i1 returns a day

Any ideas - Appologies if it's something obvious

Enda
8 REPLIES 8
Dietmar Konermann
Honored Contributor

Re: Chaning a string using sed giving parse error

Looks like your "i1" jobs returns a trailing blank.

Try this instead:

$hm/i1 $e | read e0

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
H.Merijn Brand (procura
Honored Contributor

Re: Chaning a string using sed giving parse error

Nah, I guess that either $e or $e0 contains a slash, making the sed command bogus.

My first suggestion is to use double quotes to make the command more readable:

sed -e "s/$e/$e0/g" $hm/dw >$hm/dw1

to be more lenient to the pattern, use perl's alternate syntax for s///, that would catch this:

perl -pe "s{$e}{$e0}g" $hm/dw >$hm/dw1

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Jean-Louis Phelix
Honored Contributor

Re: Chaning a string using sed giving parse error

hi,

Or perhaps i1 returns '/' characters ... You can use any character as a separator instead of /. Try for example ';' :

sed -e 's;'$e';'$e0';g' $hm/dw > $hm/dw1

Regards.
It works for me (© Bill McNAMARA ...)
Enda Martin_1
Occasional Contributor

Re: Chaning a string using sed giving parse error

Dietmar,
thanks for that. This has lead me to another problem in that the second string 030114 converts ok but the first string 030113 doesn't convert at all...it's as if sed is ignoring the first loop(if I put in a grep to see if the correct strings are been used and are in the dw file everything looks ok)

Cheers.
Rodney Hills
Honored Contributor

Re: Chaning a string using sed giving parse error

Enda,

It looks like if $hm is set before the loop, then $hm/dw and $hm/dw1 will be the same value for each iteration of the loop.

Thus $hm/dw1 will have the last sed command run, which is your 030114. The 030113 is overwritten.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Chaning a string using sed giving parse error

If you are trying to do multiple changes to a file with sed, then put the s/// commands in a file and use:

sed -f myfile $hm/dw > $hm/dw1

This would require building "myfile" with s/// commands from your $hm/l1 file.

If you used perl, then you could do something like

perl -en '$hm=ENV{"hm"};$x=`$hm/i1 $_`; print "s/$_/$x/g\n";}' $hm/l1 >myfile

to create "myfile".

But if you used perl, you could do it with using "sed".

HTH

-- Rod Hills
There be dragons...
H.Merijn Brand (procura
Honored Contributor

Re: Chaning a string using sed giving parse error

Rodney, I hope you ment

perl -en '$hm=ENV{hm};chomp($x=`$hm/i1 $_`); print "s/$_/$x/g\n";}' $hm/l1 >myfile

backticks are not (yet) autochomped
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: Chaning a string using sed giving parse error

I'm bad about chomp's sometime, especially on the one-liners. Thanks for keeping me on my toes.

-- Rod Hills
There be dragons...