1836937 Members
2301 Online
110111 Solutions
New Discussion

Re: awk query

 
SOLVED
Go to solution
Declan Mc Kay
Occasional Advisor

awk query

Hi,

I want to total up the 5th Column of a text file. However, the total being printed is not printing/displaying in the correct format..I think my total is working fine but my printf is missing something..as follows:
cat $TRAFFIC | awk '{t1 +=$5}
END {printf("TRAFFIC TOTAL: "t1"")}' >> $TRAFFIC

Printf outputs as follows: e.g.
TRAFFIC TOTAL: 5.97367e+06

I think this is because the total will be quite a large number...I don't know how to display this correctly..?

Also, I want to convert & display this value from bytes into kb's & mb's...Any easy method anyone?
5 REPLIES 5
Elmar P. Kolkman
Honored Contributor
Solution

Re: awk query

You could try:
printf("Traffic total: %d",t1);

Every problem has at least one solution. Only some solutions are harder to find.
Jean-Luc Oudart
Honored Contributor

Re: awk query

try
printf("TRAFFIC TOTAL %12d\n",t1)

Rgds,
Jean-Luc
fiat lux
Jean-Luc Oudart
Honored Contributor

Re: awk query

for more on printf

man printf

Rgds,
Jean-Luc
fiat lux
Mark Grant
Honored Contributor

Re: awk query

You need something like

printf "TRAFFIC CONTROL %ld",t1

The simplest way to get your other answers is to divide by 1024 :)
Never preceed any demonstration with anything more predictive than "watch this"
Michael Schulte zur Sur
Honored Contributor

Re: awk query

Hi,

you use printf, but provide no format string to format the number. You can print with or without leading zero and formatted to a specified number of digits.

{printf("Total %09d \n",t1)}

Nine digits with leading zeroes and newline.

greetings,

Michael