printf format with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printf format with awk
# 1  
Old 08-06-2012
printf format with awk

Hello
Here is an easy one

Data file
Code:
12345 (tab) Some text (tab) 53.432
23456 (tab) Some longer text (tab) 933.422
34567 (tab) Some different text (tab) 29.309

I need to awk these three tab-delimited columns so that the first two are unchanged (unformatted) and the third shows two decimal places:

Desired output
Code:
12345 (tab) Some text (tab) 53.43
23456 (tab) Some longer text (tab) 933.42
34567 (tab) Some different text (tab) 29.31


Thanks!
# 2  
Old 08-06-2012
How about:
Code:
awk -F'\t' '{$3=sprintf("%.2f", $3)} 1' OFS='\t' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk printf format in columns

Hi all, i have written this script: awk -F';' ' BEGIN { printf "\n" printf "\n" printf "\n" printf "----------------------------------------------\n" print " For test " printf "----------------------------------------------\n" test_200 = 0 test_300 = 0 test_500 = 0 test_1000 = 0... (11 Replies)
Discussion started by: arrals_vl
11 Replies

2. UNIX for Dummies Questions & Answers

awk printf in required format

Hi my awk variable $0 contains the below data Input file 000001 The Data 000002* The line 2 000003* The line3 output file Req 000001* The Data 000002** The line 2 000003** The line3 one * at column seven needs to be appended to the input lines, (5 Replies)
Discussion started by: rakeshkumar
5 Replies

3. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

4. 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

5. Shell Programming and Scripting

printf to format lines.

Hey, I'm trying to read printf(3) and seems I need to use the * WIDTH but I'm not quite sure how to go about it. there needs to be 3 fields per line, the lines read in from file looks like this: JUICE - APPLE:JUST JUICE:7 MILK - CHOCOLATE:BREAKA:7 this needs to be output backwards with... (6 Replies)
Discussion started by: gcampton
6 Replies

6. Shell Programming and Scripting

IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name... (3 Replies)
Discussion started by: thom.mattson
3 Replies

7. Shell Programming and Scripting

How to format the output using float in awk{printf}

Hi I'm using awk to manipulate the data in the 6th field of the file xxx_yyy.hrv. The sample data that is available in this field is given below 220731.7100000000000000 When i tried using this command cat xxx_yyy.hrv | awk '{printf("%23.16f\n",$6*-1)}' I get the output as... (4 Replies)
Discussion started by: angelarosh
4 Replies

8. Shell Programming and Scripting

printf in awk

Hi friends.. I am confused about awk printf option.. I have a comma separated file 88562848,21-JAN-08,2741079, -1188,-7433,TESTING 88558314,21-JAN-08,2741189, -1273,-7976,TESTING and there is a line in my script ( written by someone else) What is the use of command? I guess... (10 Replies)
Discussion started by: clx
10 Replies

9. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

10. Shell Programming and Scripting

printf format

hi, i would like to extract the header and put it in a variable, then use printf to output the variable, but i keep on getting errors...please tell me if my format is incorrect. HDR = "`ps -e -o user,pid,ppid,pcpu,stime,etime,time,comm | head -n 1`" printf (%s, $HDR); thanks! (3 Replies)
Discussion started by: laila63
3 Replies
Login or Register to Ask a Question