In sed and/or awk, how would I formulate an if/else logic to skip a command if a specific text already exist in a file?
I am getting multiple entries in a text file when the script is executed again.
awk will work on all lines of a file by default, or you can use a regular expression and have it only act on matching (or non-matching) lines. e.g:
$ cat testit.txt
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
$ awk '/i/ {print $0}' testit.txt
Five
Six
Eight
Nine
$ awk '!/i/ {print $0}' testit.txt
One
Two
Three
Four
Seven
Ten
Eleven
Twelve
Or if you need to check the whole file for the existence of some expression first, and then conditionally execute some commands, you could try something like
if grep -l "Six" testit.txt >/dev/null
then
print Text was found
else
print Text was not found
fi
Quick Links:
Do you have
a UNIX Question?
Unix Home: Unix System Administration
Hints and Tips