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



Relevant Pages

  • OT: Script to add line to file if it doesnt exist
    ... 'cause y'all know this stuff (I've been wrestling with this simple task all day). ... I just want a script 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. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)
  • Re: OT: Script to add line to file if it doesnt exist
    ... command, etc, would be awesome) to check to see if the file contains a ... I want a script that will read the file and look for the name "fred", ... the name "fred" to the bottom of the file. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)
  • Re: OT: Script to add line to file if it doesnt exist
    ... I just want a script (a one-liner sed or awk ... I want a script that will read the file and look for the name "fred", ... the name "fred" to the bottom of the file. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • RFH: slow awk scripts wanted
    ... generally awk is already more than fast enough for me. ... a awk script you use regularly on large sets of data ... As active Debian users, ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: how can I obtain the old fglrx_drv.so?
    ... There is a script I downloaded which could help to get rid of the ... I do not know enough about awk nor about the structure of .so files to say how to change it though. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx with a subject of "unsubscribe". ...
    (Debian-User)