AWK printf command question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK printf command question
# 1  
Old 01-19-2012
AWK printf command question

Hi,

I am using below awk code to convert a csv file data into fixed file format.

Code:
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
================
Code:
XXXXXXXXXX,YYYYYYYY,10/31/2011,10.05,1
AAAAAAAAAA,BBBBBBBB,10/31/2011,10.04,1

For the second row alone, 10.04 is converted into 10.03. i am not sure what is causing this problem. Please clarify.

Thanks,
Bala

---------- Post updated at 12:53 PM ---------- Previous update was at 12:27 PM ----------

Hi Admin,

Please delete this post. I mistaken posted it here in AIX section. Just noticed it. I am not sure how to remove this post.

Last edited by radoulov; 01-19-2012 at 04:09 PM.. Reason: Code tags!
# 2  
Old 01-19-2012
Floating point numbers don't have infinite precision and don't have boundaries on nice round numbers like you'd expect decimal numbers to. If you want perfect precision without tiny math inaccuracies, manipulate that field with string operations, not math operations. Try stripping out the dot then printing with "%4s".

Code:
sub(/\./, "", $4);

# 3  
Old 01-19-2012
Hi Corona688,

Thanks for your reply.

Thanks,
Bala

---------- Post updated at 03:01 PM ---------- Previous update was at 02:58 PM ----------

However, I observed that print command comes with the exact value but not printf. Why there is a difference in both outputs.

---------- Post updated at 03:46 PM ---------- Previous update was at 03:01 PM ----------

Changing the format from decimal to string prevents this conversion.

Code:
fmt = "%10s%010d%10s%s%1d\n"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

Question about a awk command

Hi, I am studying an awk command: awk '{ sub(/\/\/.*/, "", $NF); print }' input.txt The input.txt is: char*s1="//hello"; //comment //delete /* hello //*/ The output is : char*s1="//hello"; /* hello (2 Replies)
Discussion started by: jeffwang66
2 Replies

4. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

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

6. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. UNIX for Dummies Questions & Answers

AwK printf question

Hi, Does anyone know a easy way to printf $3,$4, ... all the way to the last field in the file? I will need to modify $1 and $2 and then printf modified $1 and $2 and the rest of the fields(which are not changed). I know I can use NF as the total number of field. Do I use a for next statement to... (4 Replies)
Discussion started by: whatisthis
4 Replies

8. Shell Programming and Scripting

printf question

can you take input from another command and do printf? such as awk '{print $2,$1}' | sort -k1,1 -k2,2 | printf "%-10s,%15s" this does not work.. but there must be a way.. please help me.. thank you. (3 Replies)
Discussion started by: hankooknara
3 Replies

9. Programming

multiple forks and printf question

Hello *NIX gurus, I have a slight perplexing problem with multiple forks giving different results... Here is the deal. From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the... (4 Replies)
Discussion started by: navigator
4 Replies

10. Programming

Question: pthread_cancel() and printf()

Hello! First of all, sorry for my English, I'm not a native English speaker. I know, that printf() function uses write() function. "man cancellation" says that write() function is a cancellation point. But when I call pthread_cancel() for my thread, which calls printf() in infinite cycle, it... (4 Replies)
Discussion started by: prankster
4 Replies
Login or Register to Ask a Question