Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

Excellent!!!
I see what you did, you only changed the condition from && to ||You are the best!
How may I reward you?

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 lineBut 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/'
xxabc45defyyecho 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/'
xxabc45defyyecho xxabcdefyy | awk '$1~/abc[0-9]*def/'
xxabcdefyyIn 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.

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

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

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