Computing.Net > Forums > Programming > AWK help

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 help

Reply to Message Icon

Name: user57
Date: August 19, 2009 at 04:38:59 Pacific
OS: AIX 5.3
CPU/Ram: 4 CPU / 32 GB RAM
Subcategory: General
Comment:

Hello

I have a range of servers which have a 3-digit identifier in their name (refer to input file below).
Not all of the numeric range is use i.e. the following do not exist:

serv103
serv109

$cat input
serv101
serv102
serv104
serv105
serv106
serv107
serv108
serv110

I am attempting to write a script to identify the 3-digit identifiers NOT in use.

Desired output:
101:serv101
102:serv102
103:FREE
104:serv104
105:serv105
106:serv106
107:serv107
108:serv108
109:FREE
110:serv110

My attempt so far has failed to print the free identifier and text "FREE".
I'm having problems with the else statement say for instance when id=103 or 109

for i in `cat id` # range from 101-110
do


awk -v id=$1 '{

num=sprintf("%s, substr($1,length($1)-2))

if(id==num) { printf("%s%s%s\n", id,":",$1) }

}' input

Can anyone assist or perhaps suggest a better approach?



Sponsored Link
Ads by Google

Response Number 1
Name: ricardo647
Date: August 19, 2009 at 07:20:54 Pacific
Reply:

Would a batch file be suitable for you? I can suggest an easy solution using a bat.


0

Response Number 2
Name: user57
Date: August 19, 2009 at 07:41:54 Pacific
Reply:

Unfortunately I have been asked to complete the task in awk


0

Response Number 3
Name: nails
Date: August 19, 2009 at 09:42:59 Pacific
Reply:

Since my OS is Solaris, I'm using nawk. Also, there is no need to use sprintf:

#!/bin/ksh

while read line
do
  nawk -v id=$line ' BEGIN { xfree=0 }
{
     num=substr($1,length($1)-2)

     if(id == num)
       {
       printf("%s:%s\n", id,$1)
       xfree=1
       }
}
END { if(xfree == 0)
         printf("%s:FREE\n", id)
}
' input

done < id


0

Response Number 4
Name: user57
Date: August 19, 2009 at 23:48:02 Pacific
Reply:

nails,

Great - that did the job. Thanks for the post.


0

Response Number 5
Name: nails
Date: August 20, 2009 at 09:41:21 Pacific
Reply:

Thank you user57, but I couldn't let this one go. This is a good example for using awk's associative arrays. I read the input file completely into an array and processed the id file in the main processing loop:

nawk ' BEGIN {
   while ( getline < "input" > 0 )
      {
      num=substr($1,length($1)-2)
      arr[num]=$1
      }
}
{
        if($1 in arr)
           printf("%s:%s\n", $1, arr[$1])
        else
           printf("%s:FREE\n", $1)

} ' id


1

Related Posts

See More



Response Number 6
Name: user57
Date: August 26, 2009 at 07:16:01 Pacific
Reply:

nails,

Having already done the job I have just viewed your further post using awk's associative array.

An impressive solution! Once again thanks for taking the time to rely nails.


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 Programming Forum Home


Sponsored links

Ads by Google


Results for: AWK help

awk help plz! www.computing.net/answers/programming/awk-help-plz-/10495.html

awk help www.computing.net/answers/programming/awk-help/17145.html

Shell scripting and awk help www.computing.net/answers/programming/shell-scripting-and-awk-help/9594.html