[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: string manipulation. sed? gawk? other? how?



On 10/11/2011 03:22 PM, Dom wrote:
On 11/10/11 20:41, 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.

sed "s/^The lazy dog.*$/The lazy dog is my favorite./"

Fairly basic. Find some good reading on sed :)


Except that this results in:

The lazy dog is my favorite.
The dog ate my lunch.
The lazy dog is my favorite.
The lazy dog is my favorite.

instead of

The lazy dog is my favorite.
The dog ate my lunch.

Thanks, though!

--
Kent West<*)))><
http://kentwest.blogspot.com
Praise Yah! \o/



Reply to: