Re: OT: Script to add line to file if it doesn't exist



On 2010-04-07 15:45, Mart Frauenlob wrote:
On 07.04.2010 22:10, Kent West wrote:
I'm asking you folks, 'cause y'all know this stuff (I've been wrestling
with this simple task all day).

I've got a text file; I just want a script (a one-liner sed or awk
command, etc, would be awesome) to check to see if the file contains a
certain line of text, and if not, to add that line at the bottom of the
file.

For example, say the file has these four lines in it:

john
mary
fred
martin

I want a script that will read the file and look for the name "fred",
and if it's found, leave the file alone, but if it's not found, to add
the name "fred" to the bottom of the file.

Any help appreciated. Thanks!



#!/bin/sh
grep -w "fred" file || printf "%s\n" "fred" >>file


Mart's method is the shell way. The 3GL Way is:

grep -w "$NAME" "$FILE"
TMP=$?
if [ "$TMP" = "1" ];
then
echo -e "$NAME\n" >> "$FILE"
fi


--
"History does not long entrust the care of freedom to the weak
or the timid." Dwight Eisenhower


--
To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". Trouble? Contact listmaster@xxxxxxxxxxxxxxxx
Archive: http://lists.debian.org/4BBCF528.3030606@xxxxxxx