AWK: formating number without printf


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: formating number without printf
# 1  
Old 01-15-2009
Data AWK: formating number without printf

Hello,

I wrote a script that does lot of things, and I would like to change the format of a number but without printing it now (so I don't want to use printf as it will print the value immediately).

Schematically here is what I have:

Code:
awk 'BEGIN{number=0.01234567}
$1==$2{$3=number}
{print}' myfile


I would like to have my output with the format 1.234567E-02, but of course the BEGIN part cannot be changed. The problem with using printf is that the lines matching $1==$2 don't have all the same number of elements, or the same spacing...

Every suggestion is very welcome.

Thanks
# 2  
Old 01-15-2009
This works from the command line:

-bash-3.00$ myvar=$(printf "%1.2f" 1.35811);echo $myvar;
1.36

Of course alter it however you see fit! Smilie
# 3  
Old 01-15-2009
Unfortunately I cannot not assign a variable like that in my awk script ( ^ syntax error)...
# 4  
Old 01-15-2009

Use sprintf().
# 5  
Old 01-15-2009
cfajohnson - he knows everything!! I aspire to be like him Smilie
# 6  
Old 01-15-2009
Quote:
Originally Posted by cfajohnson
Use sprintf().
Awesome!!! Smilie

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with ... Formating the file using awk

I have a file like below position1 0 7802 7802 0 client1 - - position1 8 8032 8032 0 client1 ... (7 Replies)
Discussion started by: manas_ranjan
7 Replies

2. UNIX for Beginners Questions & Answers

awk HTML Conditional Formating

I am receiving the below output in text format. The output is converted to HTML table using the code mentioned below output in text LogDate DayOfWeek/Hours _0_ _1_ _2_ _3_ _4_ _5_ _6_ _7_ _8_ _9_ _10_ _11_ _12_ _13_ _14_ _15_ _16_ ... (3 Replies)
Discussion started by: Dumpi16
3 Replies

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

4. Shell Programming and Scripting

formating sql file using awk or sed

Hi, I have a file where I would like to add a prompt type object_name statement before every create commnad create or replace force view test_view_01 ( col1 col2 col3 ) as (select a,b,c from sometable ); create or replace view test_view_02 ( col4 col5 col6 ) as (5 Replies)
Discussion started by: jville
5 Replies

5. UNIX for Dummies Questions & Answers

formating number strings

I have treid searching for what I thought would be a common question...but to no avail. I have to long numbers (4-8) digits. how can I format them so they are printed as 1,234,567 rather than 1234567? I have no idea where to start, have played with varying types of awk, print, substr. Also,... (10 Replies)
Discussion started by: nhatch
10 Replies

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

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

8. Shell Programming and Scripting

Displaying Number with printf

I am trying to display a number with commas printf "%d\n" 323232 printf "%d\n" 1234567 I want the output to be: 323,232 1,234,567 I tried to change %d to other formats and could find the solution. any idea? (7 Replies)
Discussion started by: ynixon
7 Replies

9. UNIX for Dummies Questions & Answers

Displaying Number with printf

I am trying to display a number with commas printf "%d\n" 323232 printf "%d\n" 1234567 I want the output to be: 323,232 1,234,567 I tried to change %d to other formats and could find the solution. any idea? (7 Replies)
Discussion started by: ynixon
7 Replies

10. Shell Programming and Scripting

printf returning unknown number

Hi, Can anybody tell me why this function returns 49? printf "%d\n" \'10 I don't understand what the \ and ' are there for? thanks!!! (1 Reply)
Discussion started by: m0nk3y
1 Replies
Login or Register to Ask a Question