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

Using nawk "if" like a shell case statement 2

Status
Not open for further replies.

madasafish

Technical User
Jul 18, 2006
78
TH
#!/bin/ksh
LOG=/var/log/oss/libiiop/libiiop_11-06-21_08:00.log
TMPLOG1=/tmp/templog1.log
TMPLOG2=/tmp/templog2.log


#nawk -F"mid=" '{print substr ($2,1,12)}' $LOG > $TMPLOG1
#sort -u $TMPLOG1 | grep -v "^;" > $TMPLOG2

nawk '{a[substr ($0,1,6)]++}END{for (i in a) printf "%-6s -> %-2d \n", i, a}' $TMPLOG2

The above script produces the following output....which are manufacturers MAC addresses (first 6 Hex Numbers)

-> 1
0022CE -> 2178
00223A -> 6
001BD7 -> 3275
001CEA -> 373
00252E -> 284
001AC3 -> 3697
001947 -> 392
001868 -> 620
0000F0 -> 7055
484487 -> 13980
00214C -> 27824
005094 -> 4933
001692 -> 48
602AD0 -> 2650
445829 -> 3656
001632 -> 29923
54D46F -> 84

What I am trying to achieve is the following output...

Mfcter MacAddr Qty
BoxA -> 0022CE -> 2178
BoxB -> 00223A -> 6
BoxC -> 001BD7 -> 3275
BoxD.....etc

I have tried many variations on the theme and understand there is no "case" statement in nawk.

The closest I can get in script form is shown as follows. Please note: The script below errors.


nawk '{a[substr ($0,1,6)]++}END{

for (i in a)
if ( i ~ '0022CE' );x="BoxA"

if ( i ~ '00223A' );x="BoxB"

#.....etc

else
x="Not Found"

printf "%-12s %-6s -> %-2d \n", x, i, a}' $TMPLOG2


Any advice appreciated
Thanks in advance.

Madasafish
 
What about this ?
Code:
nawk '{a[substr ($0,1,6)]++}
END{for(i in a){
  x="Not Found"
  if(i=="0022CE")x="BoxA"
  if(i=="00223A")x="BoxB"
#.....etc
  printf "%-12s %-6s -> %-2d \n", x, i, a[i]
}}' $TMPLOG2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Rather than using clunky ifs I would build a hash in my BEGIN { ... } clause such as:

Code:
mfcter["0022CE"]="BoxA"
mfcter["00223A"]="BoxB"
...

And then just before printing do something like:

Code:
if (i in mfcter) x=mfcter[i]; else x="Not Found"

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top