Computing.Net > Forums > Unix > awk return value

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.

awk return value

Reply to Message Icon

Name: salem
Date: January 12, 2007 at 09:07:30 Pacific
OS: aix
CPU/Ram: 400mhz
Product: p560
Comment:

Hello,

I need to get a return value of 0 if the format is correct or 1 if the format is incorrect?
here is an example?

awk -F[.] '($1 ~ /^[0-9]+$/) && ($2 ~ /^[a-zA-Z0-9]+$/)' file1.txt

cat file1.txt
192.168.1.1 host1
host2 192.168.1.2
host3 192.168.1.3
192.168.1.4 host4



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: January 12, 2007 at 10:32:50 Pacific
Reply:

awk -F[.] '$1!~/^[0-9]+$/||$2!~/^[a-zA-Z0-9]+$/{exit 1}' file1.txt

The above command will not output any of the lines. If all lines pass, $? will contain 0. If even one line does not pass, $? will contain 1.


0

Response Number 2
Name: salem
Date: January 12, 2007 at 13:49:27 Pacific
Reply:

Excellent!!!
I see what you did, you only changed the condition from && to ||

You are the best!
How may I reward you?


0

Response Number 3
Name: salem
Date: January 12, 2007 at 14:08:25 Pacific
Reply:

actually, the ! too?

Thanks again!


0

Response Number 4
Name: salem
Date: January 12, 2007 at 14:11:07 Pacific
Reply:

can you please explain what are the parameters mean exactly?

$1!~/^[0-9]+$/


0

Response Number 5
Name: James Boothe
Date: January 15, 2007 at 11:11:46 Pacific
Reply:

salem,

First, you are quite welcome. You can reward me by going to the man pages and study regular expressions, as this will help you a lot!

The code posted by you was checking for valid lines and taking the default awk action of printing those lines.  Lines that did not match this filter were simply ignored.  I could state that logic something like this:

if (word1 is all digits) AND (word2 is all digits/letters)
   then print the line
   else do nothing with this line

But I wanted to check for invalid lines so that I could exit with a status code of 1 if any are found, and that logic would look like this:

if (word1 is NOT all digits) OR (word2 is NOT all digits/letters)
   then exit with status 1
   else do nothing with this line


$1!~/^[0-9]+$/

The above says "if word1 is NOT all digits".  The ~ is the matching operator (does the expression being tested match the specified pattern).  The ! means NOT.

The easiest way to explain is perhaps by a series of examples ...

Does word1 contain abc
$1~/abc/

Does word1 start with abc
$1~/^abc/

Does word1 end with abc
$1~/abc$/

Does word1 contain a digit
$1~/[0-9]/

Does word1 contain a series of one or more digits
$1~/[0-9]+/

Does word1 contain abc, followed by one or more digits, followed by def
$1~/abc[0-9]+def/

Let's test the above:

echo xxabc45defyy | awk '$1~/abc[0-9]+def/'
xxabc45defyy

echo xxabcdefyy | awk '$1~/abc[0-9]+def/'

In the second test above, with the digits removed, the echoed line no longer passes the test, thus it is not displayed.  But let's do those same two tests again, but this time test for zero-or-more digits instead of one-or-more digits:

echo xxabc45defyy | awk '$1~/abc[0-9]*def/'
xxabc45defyy

echo xxabcdefyy | awk '$1~/abc[0-9]*def/'
xxabcdefyy

In regular expressions, which are also referred to as REs, it is important to note that a bracketed expression represents a single character, but a bracketed expression can be followed by * or + to change it into a series of characters.

$2 ~ /^[a-zA-Z0-9]+$/

The above checks entire word2 for a series of characters (one or more) where each character must be either a lowercase letter, an uppercase letters, or a digit - in other words, no special characters.


0

Related Posts

See More



Response Number 6
Name: salem
Date: March 4, 2007 at 22:34:16 Pacific
Reply:

James,
The awk statement need to return a value of one when the file has only one field?

192.168.1.0
192.168.1.1 host1
host2 192.168.1.2
host3 192.168.1.3
192.168.1.4 host4


0

Sponsored Link
Ads by Google
Reply to Message Icon






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: awk return value

Return value of awk www.computing.net/answers/unix/return-value-of-awk/7172.html

awk help www.computing.net/answers/unix/awk-help/7726.html

Unix, Shell Script, awk & variables www.computing.net/answers/unix/unix-shell-script-awk-amp-variables/5148.html