Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi Folks,
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.
Thanks for your help

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
TwelveOr 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

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |