PDA

View Full Version : [SOLVED] Failing sed & echo if line-to-be-added already exists



Intrepid Ibex
January 17th, 2011, 08:31 PM
Hi,

So this is quite simple: is there a way to fail echo and sed if the line that I was about to add already existed (wihtout going with eg. grep)?

Didn't find anything in the manpages.

Thanks beforehand,
Det

geirha
January 17th, 2011, 09:16 PM
I don't understand what you mean. I think you'll have to explain more what you mean by adding a line with echo and sed.

Intrepid Ibex
January 17th, 2011, 09:43 PM
I meant just using the other one :D.

e.g. sed 's/$/add text to the end"/g' somfile
or: echo text >> somefile

However I just did this by grepping and checking whether it returned any results.

geirha
January 18th, 2011, 12:07 AM
Well, you have to actually look in the file to see if it contains your string already.

That sed appends text to the end of each line and outputs it to stdout, the echo appends a line of text to the file.

But anyway, I assume you meant that sed to be something like
sed -i '$a add text to the end' somefile

I don't know of any way to do that with sed. I'd just do

grep -Fq "some text" somefile || printf '%s\n' "some text" >> somefile

Or you could use awk

awk '/some text/{found=1;exit} END{if (!found) print "some text" >> FILENAME}' somefile