INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

awk simple code

awk simple code

(OP)

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 --> aaaa

$ 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"

RE: awk simple code

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close