Combining awk printf and print strftime command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining awk printf and print strftime command
# 1  
Old 03-20-2014
Tools Combining awk printf and print strftime command

I have a lines like below, captured from rrdtool fetch command,
Code:
1395295200 2.0629986254e+06 7.4634784967e+05
1395297000 2.0198121616e+06 6.8658888903e+05
1395298800 1.8787141122e+06 6.7482866452e+05
1395300600 1.7586118678e+06 6.7867977653e+05
1395302400 1.8222762151e+06 7.1301678859e+05

I'm able to convert the first field from epoch time format to "normal date" format and also separate the field with comma with awk command (directly from the rrdtool fetch command) :

Code:
/usr/bin/rrdtool fetch rrdfiles.rrd AVERAGE -r 3600 -e "Mar 20 2014" -s e-14d | cut -f1-2 -d":" |\
 sed 's/://g' | sed '/-nan/d' | awk '{print strftime("%c",$1)","($2)","($3)}'

Code:
Thu 20 Mar 2014 01:00:00 PM WIT,2.0629986254e+06,7.4634784967e+05
Thu 20 Mar 2014 01:30:00 PM WIT,2.0198121616e+06,6.8658888903e+05
Thu 20 Mar 2014 02:00:00 PM WIT,1.8787141122e+06,6.7482866452e+05
Thu 20 Mar 2014 02:30:00 PM WIT,1.7586118678e+06,6.7867977653e+05
Thu 20 Mar 2014 03:00:00 PM WIT,1.8222762151e+06,7.1301678859e+05

What I want to achieve is how to format the second and third field to decimal format with awk printf command :
Code:
printf "%12.2f\n"

The final output should be :
Code:
Thu 20 Mar 2014 01:00:00 PM WIT,2062998.6254,746347.8497
Thu 20 Mar 2014 01:30:00 PM WIT,2019812.1616,686588.8890
Thu 20 Mar 2014 02:00:00 PM WIT,1878714.1122,674828.6645
Thu 20 Mar 2014 02:30:00 PM WIT,1758611.8678,678679.7765
Thu 20 Mar 2014 03:00:00 PM WIT,1822276.2151,713016.7886

How to combine print strftime and printf command in one line ? . Sorry I have try several combination but not work.
Or maybe there are other solution.

TIA.

Last edited by vbe; 03-20-2014 at 06:44 AM..
This User Gave Thanks to rk4k For This Post:
# 2  
Old 03-20-2014
Code:
awk '{printf("%s,%12.4f,%12.4f\n",strftime("%c",$1),$2,$3) } '

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 03-20-2014
Another way to do it is by changing:
Code:
awk '{print strftime("%c",$1)","($2)","($3)}'

to:
Code:
awk -v OFMT='%.4f' '{print strftime("%c",$1)","$2+0","$3+0}'

Note that I used %.4f rather than the %12.2f you specified because you showed that you want 4 digits after the decimal point in your output (not 2), and there is no need to force leading spaces when printing smaller values in a CSV formatted output file so the minimum field width 12 is not needed.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-20-2014
With this code :

Code:
awk '{printf("%s,%12.4f,%12.4f\n",strftime("%c",$1),$2,$3) } '

I̶ ̶h̶a̶v̶e̶ ̶s̶p̶a̶c̶e̶ ̶i̶n̶ ̶a̶t̶ ̶t̶h̶e̶ ̶t̶h̶i̶r̶d̶ ̶f̶i̶e̶l̶d̶,̶ ̶n̶o̶t̶ ̶a̶ ̶b̶i̶g̶ ̶p̶r̶o̶b̶l̶e̶m̶. Ummm I think the space is correct. Smilie
Code:
Fri 21 Mar 2014 05:30:00 AM WIT, 778785.6976, 235032.1928
Fri 21 Mar 2014 06:00:00 AM WIT, 850720.1745, 291613.5261
Fri 21 Mar 2014 06:30:00 AM WIT,1136718.0249, 370101.2030
Fri 21 Mar 2014 07:00:00 AM WIT,1246803.9930, 443087.2727
Fri 21 Mar 2014 07:30:00 AM WIT,1456401.2317, 460454.3023
Fri 21 Mar 2014 08:00:00 AM WIT,1378386.3119, 500824.3566

Could you explain the logic behind the code anbu23 ?

and with this

Code:
awk -v OFMT='%.4f' '{print strftime("%c",$1)","$2+0","$3+0}'

I have result like these :

Code:
Fri 21 Mar 2014 05:30:00 AM WIT,778786,235032
Fri 21 Mar 2014 06:00:00 AM WIT,850720,291614
Fri 21 Mar 2014 06:30:00 AM WIT,1.13672e+06,370101
Fri 21 Mar 2014 07:00:00 AM WIT,1.2468e+06,443087
Fri 21 Mar 2014 07:30:00 AM WIT,1.4564e+06,460454
Fri 21 Mar 2014 08:00:00 AM WIT,1.37839e+06,500824

Appreciate your solution Don. I'm using Ubuntu using /usr/bin/gawk fyi.

Thanks all.

Last edited by rk4k; 03-20-2014 at 10:44 PM..
This User Gave Thanks to rk4k For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining awk command to make it more efficient

VARIABLE="jhovan 5259 5241 0 20:11 ? 00:00:00 /proc/self/exe --type=gpu-process --channel=5182.0.1597089149 --supports-dual-gpus=false --gpu-driver-bug-workarounds=2,45,57 --disable-accelerated-video-decode --gpu-vendor-id=0x80ee --gpu-device-id=0xbeef --gpu-driver-vendor... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

How to add printf statement in awk command?

hi all i need to add the prinf statement in awk command for the converted comma separated output.... below is my code : Code Credits :RudiC awk -F, 'NF==2 {next} {ITM=$1 AMT=$2+0 CNT=$3+0 TOTA+=$2 ... (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

3. Shell Programming and Scripting

Help on awk strftime

cat file 41285.000034722223 41285.000567129631 41285.000069444446 41285.001122685186 41285.000092592592 41285.001620370371 41285.000138888892 41285.00340277778 41285.000185185185 41285.000405092593 41285.000196759262 41285.000856481478 41285.000208333331 41285.000717592593... (5 Replies)
Discussion started by: phpshell
5 Replies

4. Shell Programming and Scripting

How to combine print and printf on awk

# cat t.txt 2,3,4,5,A,2012-01-01 00:00:28 2,6,4,5,A,2012-01-02 00:00:28 2,7,4,5,A,2012-01-02 02:00:28 # awk -F"," '{OFS=",";print $2,"";printf("%s", strftime("%m%d%y",$6));printf("%s", strftime("%H%M%S \n",$6));print ("",$1)}' t.txt 3, 010170073332 ,2 6, 010170073332 ,2 7,... (3 Replies)
Discussion started by: before4
3 Replies

5. Shell Programming and Scripting

AWK Too many open streams to print/printf

hallow all i need your advice about this script i have script like this: INDEX=/zpool1/NFS/INDEX/${1} SCRIPT=/zpool1/NFS/script/${1} LIST=SAMPLE cd ${SCRIPT} for i in `cat ${LIST}` do GETDATE=`echo ${i}|awk '{print substr($1,9,8)}'` /usr/xpg4/bin/awk -F ":" '{close(f);f=$4}{print >>... (4 Replies)
Discussion started by: zvtral
4 Replies

6. Shell Programming and Scripting

What's the difference between print and printf in command?

For example, in this command: ls /etc/rc0.d/ -print ls /etc/rc0.d/ -printfThe outputs are quite different, why? (7 Replies)
Discussion started by: Henryyy
7 Replies

7. Shell Programming and Scripting

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (2 Replies)
Discussion started by: kbmkris
2 Replies

8. UNIX for Dummies Questions & Answers

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (1 Reply)
Discussion started by: kbmkris
1 Replies

9. Shell Programming and Scripting

perl replace awk strftime

Hi Everyone i have a perl file below, one of the line is convert the pcho time to human readable format. $value=`awk 'BEGIN{print strftime("%c",1273236600)}' | tr -d '\n'`; if image, if i have lots of pcho time value in a file, if i use this awk, strftime, then tr -d to remove the \n,... (2 Replies)
Discussion started by: jimmy_y
2 Replies

10. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies
Login or Register to Ask a Question