How to combine print and printf on awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to combine print and printf on awk
# 1  
Old 10-03-2012
How to combine print and printf on awk

Code:
[root@localhost ~]# 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


[root@localhost ~]# 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,
010170073332 
,2

how to make the output to be on single line

Code:
3,010170073332,2
6,010170073332 ,2
7,010170073332 ,2

Thanks
# 2  
Old 10-03-2012
Use only print.
That aside, is that output what you really want? Or you want something like:
Code:
3,010112,000028,2
6,010212,000028,2
7,010212,020028,2

If you want the above output, try:
Code:
awk -F, '{gsub(/[-:]/," ",$6);print $2,strftime("%m%d%y",mktime($6)),strftime("%H%M%S",mktime($6)),$1}' OFS=, t.txt

# 3  
Old 10-03-2012
Which awk version does provide a strftime function?
# 4  
Old 10-03-2012
gawk does.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Combining awk printf and print strftime command

I have a lines like below, captured from rrdtool fetch command, 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+05I'm... (3 Replies)
Discussion started by: rk4k
3 Replies

3. Shell Programming and Scripting

How to combine data from 2 file ? How to print ?

Dear friends, I am just trying to print data from 2 file,namely file_1.txt and file_1.dat (specific column of ROW 1) file_1.txt 12 13 14 15 99 AMC 69 36 89 12 13 14 15 99 AMC 69 84 -12 12 13 14 ... (6 Replies)
Discussion started by: nex_asp
6 Replies

4. Programming

[Perl] Different printf formating for different print options

Hi, Struggling with single quotes, double quotes, etc. I want to print a header line, followed by lines with actual values, based on a print option. In real life it is going to be something like 15 print options and 50 values. Output will be 1 header and several value lines. In this example... (5 Replies)
Discussion started by: ejdv
5 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

How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf. If I try printing it using printf it is printing as follows. output ------- str1 str2 str3 str4 btw I'm working in AIX. This is my first post in this forum :) regards, rakesh (4 Replies)
Discussion started by: enigmatrix
4 Replies

8. Shell Programming and Scripting

AWK printf help

Target file contains short text (never more than 1 line) and filenames. The format is, e.g.,: TEXT1 filename1 TEXT2 TEXT3 filename3dddd filename3dddd TEXT4 filename4 TEXT5 filename5dddd filename5dddd filename5 where dddd is a random 4-digit whole number. Desired output: (4 Replies)
Discussion started by: uiop44
4 Replies

9. Shell Programming and Scripting

awk and printf

echo $bbsize 1.5 echo $fillpercent .95 echo $bbsize | awk '{printf "%.2f\n",$0*$fillpercent}' 2.25 echo $bbsize | awk '{printf "%.2f\n",$0*.95}' 1.42 1.42 is what I'm expecting... echo $blocksize 4096 echo $bbsize | awk '{printf "%.2f\n",$0*$blocksize}' 2.25 echo $bbsize |... (3 Replies)
Discussion started by: xgringo
3 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