1754020 Members
7483 Online
108811 Solutions
New Discussion юеВ

awk question

 
SOLVED
Go to solution
Allanm
Super Advisor

awk question


I am having the output of du -xk in a file and it's showing up as this :

grep M tmp_space|more

1022M /alerts
1015M /heartbeat
987M /home/allan
983M /home/allan2
969M /home/aint.....
932K /home/aint4

I need to go away with k's ( kilo bytes ) and show only M's ( megabytes ) along with the dir listing. How do I do that. Please help.
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: awk question

Save yourself a ton of work and do a search in the Forums for a script of Bill Hassell's called "bdfmegs".
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor
Solution

Re: awk question


Hmmm,

1) my "du -xk" does not output a K, nor an M
2) my grep for M does not return a line like "932K /home/aint4" without an M
3) do you want to just not just any line with K, or 'convert' to M? If converting, round? truncate or use decimals?
4) where is awk in this picture?
I suspect an awk script made the M/K output lines. Why not show (the critical parts) of it?

Finally, my guess for a solution... just make that grep more precise.
What's the whitespace between M and / ?
Is it a TAB as per du, or spaces?

for spaces try:
grep -E "M +\/" tmp_space

for tab try
grep -E "M[[:cntrl:]]\/" tmp_space

in perl

perl -ne 'print if /M\s+\//' tmp_space
or
perl -ne 'print if m:M\s+/:' tmp_space

cheers,
Hein.
Dennis Handly
Acclaimed Contributor

Re: awk question

If you just want to use awk to change the lines with "K" you can use:
awk '
{
len = length($1)
number = substr($1, 1, len - 1)
unit = substr($1, len, 1)
if (unit == "M")
print $0
else # assume K
printf "%.3fM %s\n", number/1024, $2
} ' tmp_space