Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inconsistent GNU sed behaviour 1

Status
Not open for further replies.
Jun 22, 2000
6,317
AU
Using this sed script:

Code:
/blah/{s/blah/yak/;n;}
/yak/{s/yak/blah/;n;}
/foo/{s/foo/blah/;n;}
/bar/{s/bar/blah/;n;}

On this input:

[tt]blah
yak
ugga
foo
bar[/tt]

I get this with GNU sed version 3.02 on RHEL AS2.1:

[tt]yak
blah
ugga
blah
blah[/tt]

But this with GNU sed version 4.0.7 (note the extra line) on RHEL AS3:

[tt]yak
blah
ugga
blah
blah
blah[/tt]

And this with GNU sed version 4.0.9 on SLES9:

[tt]yak
blah
ugga
blah
blah[/tt]

Has anyone encountered this? Known bug in 4.0.7 maybe? Any suggestions of how I can safely and portably code around it (or improvements to my original script for multiple searches and replacements).

Annihilannic.
 
I ended up doing this (I didn't realise that the 'n' command doesn't make it start the next input cycle, however 'd' does).

Code:
/blah/{s/blah/yak/;p;d;}
/yak/{s/yak/blah/;p;d;}
/foo/{s/foo/blah/;p;d;}
/bar/{s/bar/blah/;p;d;}

Annihilannic.
 
And this ?
/blah/{s/blah/yak/;t
}
/yak/{s/yak/blah/;t
}
/foo/{s/foo/blah/;t
}
/bar/{s/bar/blah/;t
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV, I was looking at the 't' command (after I found my solution above) but wasn't sure if it was better in any way... does it have an advantage? Two fewer characters I guess! [smile]

Annihilannic.
 
I don't think the length of the script is the real issue ...
In fact I think the t command is the logical one to use:
sed_manpage said:
t label
Branches to the colon :)) command bearing label if any substitutions have been made since the most recent reading of an input line or execution of a t command. If label is empty, t branches to the end of the script.


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Good point PHV, I also omit the [tt]t[/tt] command many times. Regarding the code, there is no need for the addresses.
Code:
sed 's/blah/yak/;t
s/yak/blah/;t
s/foo/blah/;t
s/bar/blah/;t' /input/file

Feherke.
 
True... in that example. In the real data the address is more complex than the search and replacement parts, I should have mentioned that. e.g.

[tt]s/blah([^a-z]|$)/{s/blah/yak/;t;}[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top