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!

Convert fields 4 & 5 to GB/TB

Status
Not open for further replies.

CalgaryCR9

Technical User
Aug 12, 2005
80
CA
My ascii file reads like:
4076 aggr0 pd5fi3no 4930916644 5238321964 94.1
391 aggr0 pd2fi4so 4930918156 5238321964 94.1
7087 aggr0 pd3fi2so 4930911684 5238321964 94.1
5394 aggr0 pd5fi2no 4930911304 5238321964 94.1


and so on.

Using awk (?) I'd like to convert fields/columns 4 & 5 to GB or TB still displaying the other 4 columns. For example purposes let's call the file /foo.

Please assist me as I'm new to scripting. In case it matters I'm using Solaris 9.
 
Sorry everyone, I should have put the format of my desired output:

4076 aggr0 pd5fi3no 4.59TB 4.88TB 94.1

And so on. Some lines will be in GB, not all are TB
 

Try this:

Code:
awk '{
     p4=$4/1024/1024/1024; p5=$5/1024/1024/1024; l4="G"; l5="G";
     if (p4 > 1024) {p4=p4/1024;l4="T";}; if (p5 > 1024) {p5=p5/1024;l5="T";}
     printf "%s %s %s %.2f%s %.2f%s %.1f\n",$1,$2,$3,p4,l4,p5,l5,$6;}
' inFile.txt
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
CODE
awk '{
p4=$4/1024/1024/1024; p5=$5/1024/1024/1024; l4="G"; l5="G";
if (p4 > 1024) {p4=p4/1024;l4="T";}; if (p5 > 1024) {p5=p5/1024;l5="T";}
printf "%s %s %s %.2f%s %.2f%s %.1f\n",$1,$2,$3,p4,l4,p5,l5,$6;}
' inFile.txt

Thank you so much for replying. My code is:


Code:
awk '{
        p4=$4/1024/1024/1024; p5=$5/1024/1024/1024; 14="G";
        if (p4 > 1024) {p4=p4/1024;14="T";}; if (p5 > 1024) {p5=p5/1024/1024;15="T";}
        printf "%s %s %s %.2f%s %.2f%s %.1f\n",$1,$2,$3,p4,14,p5,15,$6;}'$HOME/input_file_foo

I get:
awk: syntax error near line 4
awk: bailing out near line 4

Permissions are 777 on the file so that's not the show stoppper. What is?

Also, what do the %.2f%s and %.1f\n things do? I followed you entirely up until that point.

Thanks so much again.

 
Aha...are the %s, %.2f%s and %.1f\n are spaces??????

If so, what are the characters for tabs?
 
Under Solaris, use /usr/xpg4/bin/awk.

This will preserve the spacing between columns.
Put in file "convertfields.awk" and run with
[tt]/usr/xpg4/bin/awk -f convertfields.awk /foo[/tt]
Code:
# Produces array of nonmatching and matching
# substrings. The size of the array will
# always be an odd number. The first and the
# last item will always be nonmatching.
function shatter( s, shards, regexp )
{ gsub( regexp, "\1&\1", s  )
  return split( s, shards, "\1" )
}

function kilo( n,      count )
{ while ( n >= 1024 )
  { n /= 1024
    count++
  }
  if ( 0 == count )  return n "B"
  return sprintf("%.2f",n) substr("KMGT",count,1) "B"
}

{
  # Remove leading space.
  sub( /^[ \t]+/, "" )
  shatter( $0, array, "[ \t]+" )
  array[ 7 ] = kilo( array[ 7 ] )
  array[ 9 ] = kilo( array[ 9 ] )
  s = ""
  for ( i = 1; i in array; i++ )
    s = s array[ i ]
  print s
}
I hope that this helps. [thumbsup2]
 


Change the '15' and '16' in the first script to 'l5' (L-5) and L-6) to avoid the "awk: syntax error near line 4"
[noevil]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top