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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk simple code

Status
Not open for further replies.

yesornot

Technical User
Apr 15, 2015
7
PL

just want someone confirm there is no error in my micro awk code below
* if given on "f" not exists in input, then print not exists
* if given on "f" exists and given on "s" is greater than number in file a, print 'too small'

Code:
$ cat a
200 /f1
300 /f2
500 /f4
$ awk -vf=/f5 -vs=400 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
/f5 not exists
$ awk -vf=/f4 -vs=400 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
$ awk -vf=/f4 -vs=500 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
$ awk -vf=/f4 -vs=600 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
too small
$ awk -vf=/f3 -vs=600 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
/f3 not exists
$ awk -vf=/f2 -vs=600 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
too small
$ awk -vf=/f2 -vs=200 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
$ awk -vf=/f2 -vs=201 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
$ awk -vf=/f2 -vs=301 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
too small
$ awk -vf=/f2 -vs=300 'BEGIN {c=0} ($NF==f) {if ($1<s) print "too small";c++} END { if (c==0) print f" not exists" }' a
$

btw. could someone propose code with single variable for all desired in a, so instead of many awk commands with -vf=/fX -vs=NNN, run one with single variable with all to be checked, eg. comma separated:

-vexpected="/f2:300,/f4:400,/f5:200
 
Try this:
Code:
awk -vexpected='/f2:200,/f2:201,/f2:300,/f2:301,/f2:600,/f3:600,/f4:400,/f4:500,/f4:600,/f5:400' 'BEGIN{n=split(expected,x,","); for (i in x) print "Expected#"i,x[i]}
{
  for (i=1;i<=n;++i){
    f0=substr(x[i],1,3);s0=substr(x[i],5,3);s=$1;f=$2;
    if (f0==f&&s<s0) {printf"%s %s < %s\n",f,s,s0;c++}
    }
}
END { if (c==0) print f" not exists" }' a
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top