Re: OT: string manipulation. sed? gawk? other? how?
On Tue, 11 Oct 2011 14:41:10 -0500, Kent West wrote:
> I tried googling my answer, but my google-foo seems to be weak.
>
> I have a text file which may or may not have several similar lines, like
> so:
>
> The lazy dog ate my lunch.
> The dog ate my lunch.
> The lazy dog slept all day.
> The lazy dog plopped down.
>
> I want to delete all lines starting with "The lazy dog" and replace it
> with the single line, "The lazy dog is my favorite." where the first
> "The lazy dog" was found in the file.
>
> I want this to work if the file has no occurrences of "The lazy dog"; I
> want it to work if it has one occurrence of it. The text above would be
> converted to:
>
> The lazy dog is my favorite.
> The dog ate my lunch.
>
> I believe I can use
> sed 'The lazy dog'/d
> to delete all the lines containing that string, but I don't know how to
> put my new line where the first deleted line was.
>
> Thanks for any help.
Doubtless there are better ways, but I would use (in bash)
while read LINE; do
blah
blah
done < lazy.txt
Reply to: