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

Transposing rows into columns

Status
Not open for further replies.

Blivo

Technical User
May 2, 2005
9
US
I've seen something like this in other places but I'm extremely new to using awk and couldn't make any sense of it. Could someone please tell me how to get this:

System Availability 100.00%
Total Uptime 551d 8h 12m 23s
Total Downtime 0d 0h 24m 51s
Total Reboots 11
Mean Time Between Reboots 50.12 days
Total Bluescreens 0

To this:

System Availability Total Uptime Total Downtime
100.00% 551d 8h 12m 23 s 0d 0h 24m 51s

and the rest of the columns would be formatted like this also.

If it's not too much trouble once you're done with it could please explain the coding to me?

Thanks
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
awk '
/executions/{ E = $NF }
/compilations/ { C = $NF }
/deleted/ { A[++i] = sprintf("%10s%10s%10s", E, C, $NF) }
END { for (; i > 0; i--) print A | \
"sort -r -k" COL "," COL }' COL=1 file

That's an example i found that I tried to implement into my code, but with my little awk experience I didn't have much luck. I also tried this one:

/executions/{ E = $NF }
/compilations/ { C = $NF }
/deleted/ { printf("%10s%10s%10s\n", E, C, $NF) | "sort -nr" }

 
I may just not have implemented it correctly, I'll try again and could someone please explain to me what each part of both sets of code mean? thanks
 
Code:
# Set the field-separator.
BEGIN { FS = SUBSEP }

# Main loop.
{
  # Remove leading whitespace.
  sub( /^[ \t]+/, "" )

  # Split at first occurrance of 2 or more spaces
  # or a tab.
  sub( /(  +|\t)/, SUBSEP )

  # Remove trailing whitespace from field 2.
  sub( /[ \t]+$/, "", $2 )

  # Compress whitespace in field 2.
  gsub( /[ \t]+/, " ", $2 )

  column[ 1, NR ] = $1
  column[ 2, NR ] = $2
}

END {
  for (i=1; i<3; i++ )
  { gap = ""
    for (j=1; j<=NR; j++)
    { width = length( column[ 1, j ] )
      w = length( column[ 2, j ] )
      if ( w > width )
        width = w
      printf gap "%-" width "s", column[ i, j ]
      gap = "  "
    }
    print ""
  }
}
Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top