Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help for awk with if.. else..

Status
Not open for further replies.

6656

Programmer
Nov 5, 2002
104
US
Hi All,

Below codes will print the messages for each of unmatched records. How to correct it to print only one 'Not Found' message at the end of a file if the word is not found?

#-------- Script --------
read string?'[Enter the old name]: '
awk '{ if ( $1 == word )
print "[ The new name is]: " $2
else print " Not FOUND!"
}' word="$string" $HOME/old_new.txt

Thanks,
Mike
 
read string?'[Enter the old name]: '
awk '{ if ( $1 == word )
print "[ The new name is]: " $2
else notFound++;
} END { if (notFound) print " Not FOUND!"}' word="$string" $HOME/old_new.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Untested, but may get you started

awk '{ if ( $1 == word ){
print &quot;[ The new name is]: &quot; $2
fnd = 1
}
}
END{ if (!fnd) print &quot; Not FOUND!&quot; }'


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Thanks Vlad and Cakiwi,

Both of your codes returened the closed results I expected. However the &quot;Not Found&quot; message will be printed in either conditions (true/fails) met. How to fixed?

Thanks again,
Mike
 
could you post a sample file you're trying to parse, pls!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
It works now by adding a declare fnd=0 or notfound=0 befor awk.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top