Computing.Net > Forums > Unix > How to use if/else logic in sed/awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to use if/else logic in sed/awk

Reply to Message Icon

Name: lamos
Date: May 1, 2004 at 07:28:48 Pacific
OS: Solaris 8
CPU/Ram: 1GB
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: WilliamRobertson
Date: May 1, 2004 at 09:06:40 Pacific
Reply:

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


0

Response Number 2
Name: lamos
Date: May 1, 2004 at 10:01:11 Pacific
Reply:

Hi William,

I used the last solution. It works nicely. Thanks a bunch!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to use if/else logic in sed/awk

CRYPT - How to use it? www.computing.net/answers/unix/crypt-how-to-use-it/1255.html

sed - How to do conditional line in www.computing.net/answers/unix/sed-how-to-do-conditional-line-in/7605.html

how to use Hitachi DVD-Rom in OpenLinux 2.2N www.computing.net/answers/unix/how-to-use-hitachi-dvdrom-in-openlinux-22n/504.html