Using nawk with output from solaris utilities 
Author Message
 Using nawk with output from solaris utilities

Hey everyone-

Ive got a question that will most likely be very easy for you all. Im
writing a script to help diagnose some IO problems we are having on
one of our servers, and so far my line looks like so:

$ iostat -xe | sed '1,3d' | nawk
'{print$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
$9"|"$10"|"$11"|"$12"|"$13"|"$14}'

which produces output as follows:

sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0
(so on and so forth)

I would like the output to take advantage of a UNIX variable, which
displays host, date and time delimited by pipes:

sys_box|071801|054403

so my desired output would look like
sys_box|071801|054403sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0

any ideas?



Mon, 05 Jan 2004 05:50:33 GMT  
 Using nawk with output from solaris utilities
Quote:

> Ive got a question that will most likely be very easy for you all. Im
> writing a script to help diagnose some IO problems we are having on
> one of our servers, and so far my line looks like so:

> $ iostat -xe | sed '1,3d' | nawk
> '{print$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
> $9"|"$10"|"$11"|"$12"|"$13"|"$14}'

> which produces output as follows:

> sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0
> (so on and so forth)

> I would like the output to take advantage of a UNIX variable, which
> displays host, date and time delimited by pipes:

> sys_box|071801|054403

> so my desired output would look like
> sys_box|071801|054403sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0

                      ^^

Are you sure you want to concatenate the values without a separator
(a vertical bar)?

Quote:
> any ideas?

Let us assume that the "UNIX variable, which displays host, date
and time delimited [sic] by pipes" is named HDT. Let us also assume
that there are exactly 14 fields in the fourth and subsequent lines
of the output of the 'iostat -xe' command, and that you want to
print these 14 fields. Then something like this (untested)

$ iostat -xe |

Quote:
> nawk 'NR > 3 { $1 = $1; print HDT, $0 }' FS='|' OFS='|' HDT=$HDT

should work. If you want to concatentate the values of HDT and $0
instead of separate them, then simply remove the comma and don't
bother setting the value of OFS.

Good luck.

--
Jim Monty

Tempe, Arizona USA



Mon, 05 Jan 2004 08:06:34 GMT  
 Using nawk with output from solaris utilities

Quote:

> Hey everyone-

> Ive got a question that will most likely be very easy for you all. Im
> writing a script to help diagnose some IO problems we are having on
> one of our servers, and so far my line looks like so:

> $ iostat -xe | sed '1,3d' | nawk
> '{print$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
> $9"|"$10"|"$11"|"$12"|"$13"|"$14}'

> which produces output as follows:

> sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0
> (so on and so forth)

> I would like the output to take advantage of a UNIX variable, which
> displays host, date and time delimited by pipes:

> sys_box|071801|054403

> so my desired output would look like
> sys_box|071801|054403sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0

> any ideas?

Isn't clear where the variable gets set.  If it is set for each
machine where iostat is run then I guess you are asking how to
reference an shell variable.

This may be what you are after.  Dumping sed, and setting Output Field
Separator (OFS) makes it little more readable too.

Assuming SHELL_VAR="sys_box|071801|054403"

iostat -xe |nawk 'BEGIN {OFS="|"}

Quote:
>NR>2 {print "'"$SHELL_VAR"'",$1,$2,$3,$4,$5,$6,$7,\

$8,$9,$10,$11,$12,$13,$14}'

(All one line if possible... wrapped here for mail)

sys_box|071801|054403|cmdk0|0.4|0.1|3.0|0.5|0.0|0.0|19.9|0|1|0|0|0|0
sys_box|071801|054403|cmdk1|0.0|0.0|0.0|0.0|0.0|0.0|9.6|0|0|0|0|0|0
sys_box|071801|054403|fd0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0|0|0|0|0|0
sys_box|071801|054403|sd0|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0|0|0|0|0|0
sys_box|071801|054403|nfs1|0.0|0.0|0.0|0.0|0.0|0.0|0.0|0|0|0|0|0|0



Mon, 05 Jan 2004 07:00:29 GMT  
 Using nawk with output from solaris utilities
Quote:

> Ive got a question that will most likely be very easy for you all. Im
> writing a script to help diagnose some IO problems we are having on
> one of our servers, and so far my line looks like so:

> $ iostat -xe | sed '1,3d' | nawk
> '{print$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
> $9"|"$10"|"$11"|"$12"|"$13"|"$14}'

> which produces output as follows:

> sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0
> (so on and so forth)

> I would like the output to take advantage of a UNIX variable, which
> displays host, date and time delimited by pipes:

> sys_box|071801|054403

> so my desired output would look like
> sys_box|071801|054403sd1|0.4|1.3|9.8|15.4|0.0|0.1|30.9|0|2|0|0|0|0

                      ^^

Are you sure you want to concatenate the values without a separator
(a vertical bar)?

Quote:
> any ideas?

Let us assume that the "UNIX variable, which displays host, date
and time delimited [sic] by pipes" is named HDT. Let us also assume
that there are exactly 14 fields in the fourth and subsequent lines
of the output of the 'iostat -xe' command, and that you want to
print these 14 fields. Then something like this (untested)

$ iostat -xe |

Quote:
> nawk 'NR > 3 { $1 = $1; print HDT, $0 }' OFS='|' HDT=$HDT

should work. If you want to concatentate the values of HDT and $0
instead of separate them, then simply remove the comma.

Good luck.

--
Jim Monty

Tempe, Arizona USA



Mon, 05 Jan 2004 08:10:20 GMT  
 Using nawk with output from solaris utilities

% $ iostat -xe | sed '1,3d' | nawk
% '{print$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
% $9"|"$10"|"$11"|"$12"|"$13"|"$14}'

First suggestion: sed isn't really adding anything here. You could
get the same effect in by writing
  iostat -xe | nawk 'NF > 3 { ... }'

Second suggestion: rather than putting in all that concatenation, you
could either set OFS to "|", and write
 BEGIN { OFS = "|" } NF > 3 { print $1,$2,$3, ..., $14 }

(... is not awk notation, it's just me being lazy), or, if $1,...,$14
is the complete list of fields output by iostat:

 BEGIN { OFS = "|" } NF > 3 { $1=$1; print }

or, you could print the fields in a loop:
 { for (i = 1;i <= 14; i++) printf "%s%s", $i, i < 14 ? "|" : "\n" }

% I would like the output to take advantage of a UNIX variable, which
% displays host, date and time delimited by pipes:
%
% sys_box|071801|054403

The usual suggestion is to use the -v argument:
  SYS_BOX=sys_box|071801|054403
  iostat -xe | nawk -v sb="$SYS_BOX" 'NF > 3 { printf "%s", sb
          for (i = 1; i <= 14; i++) printf "|%s", $i
          print "" }'

if the $1=$1 notation appeals to you, and how could it fail:
  iostat -xe | nawk -v sb="$SYS_BOX" 'NF > 3 { $1 = $1; print sb "|" $0 }'
--

Patrick TJ McPhee
East York  Canada



Mon, 05 Jan 2004 08:11:58 GMT  
 Using nawk with output from solaris utilities


Quote:


<snip>
> or, you could print the fields in a loop:
>  { for (i = 1;i <= 14; i++) printf "%s%s", $i, i < 14 ? "|" : "\n" }

> % I would like the output to take advantage of a UNIX variable, which
> % displays host, date and time delimited by pipes:
> %
> % sys_box|071801|054403

> The usual suggestion is to use the -v argument:
>   SYS_BOX=sys_box|071801|054403
>   iostat -xe | nawk -v sb="$SYS_BOX" 'NF > 3 { printf "%s", sb

Bug here                                ^^, should be NR!

Quote:
>           for (i = 1; i <= 14; i++) printf "|%s", $i
>           print "" }'

> if the $1=$1 notation appeals to you, and how could it fail:
>   iostat -xe | nawk -v sb="$SYS_BOX" 'NF > 3 { $1 = $1; print sb "|" $0 }'

and here                                ^^

As the output of iostat -xe on Solaris is always 14 columns, you could use
     i <= NF
in the for construct.

HTH
--


Opinions expressed are my own and not necessarily those of my employer



Mon, 05 Jan 2004 16:13:00 GMT  
 Using nawk with output from solaris utilities


Quote:
> Ive got a question that will most likely be very easy for you all. Im
> writing a script to help diagnose some IO problems we are having on
> one of our servers, and so far my line looks like so:

> $ iostat -xe | sed '1,3d' | nawk
> '{print$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
> $9"|"$10"|"$11"|"$12"|"$13"|"$14}'

DATE=`date +%d%m%y\|%H%M`

SB=`uname -n`

iostat -xe | sed '1,3d' | nawk -v sb=$SB -v date=$DATE \
 '{print SB |date $1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"
$9"|"$10"|"$11"|"$12"|"$13"|"$14}'
it is exactly what are you want

ciao

camil



Thu, 08 Jan 2004 06:21:34 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Limit for the Line handle by NAWK/AWK on SOLARIS 2.5.7

2. Question about > in Solaris nawk

3. nawk script outputs input against my will

4. Help! (Using sys var in nawk body)

5. Outputting info from awk and using the info to name the awk output file

6. presenting realtime output from a utility

7. Solaris f77 and g77 give different output

8. expect losing output at program exit (Solaris, Mac OS X)

9. Piping output from background tasks to TCL scripts under Solaris 2.5.x

10. Problem using the IEWTPORT utility

11. using unix utility in awk expression

12. Using wxPython Resource Utilities

 

 
Powered by phpBB® Forum Software