why does this number become 0.0 on fprint?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting why does this number become 0.0 on fprint?
# 1  
Old 08-20-2006
Tools why does this number become 0.0 on fprint?

basically helpme.txt has three columns - the first two are decimals and the third is something like 45.5%...

sed is used to discard the % sign as was suggested here earlier...

here is the line of code I'm interested in...

cat helpme.txt | awk '{$1 $2 $3}NR<5{printf("%-20d %-20d %-20.1f\n",$1,$2,$3)}' | sed 's/%//' | sort $3

the output is correct except for the third column - it disappears to 0 values, or rather 0.0

so i am confused. Are there other options I have which don't lose this value? I expected to see 45.5 in the third column of the output. If anyone can give me another hint I would greatly appreciate it as I can't find other alternatives at this point.

-- or is there a way of formatting this output into fixed width columns without it all being the same, ie some fields text and others decimal in the same column?
# 2  
Old 08-20-2006
a sample input and the required output .. would be very useful !!! Smilie
# 3  
Old 08-20-2006
The input would be something like

Code:
435 1345 45.5%
12345 345676 29%

The output is now

Code:
435            1345            0.0
12345         345675         0.0


The third column somehow loses its value when I use the printf formatting to widen the columns... declaring the third column floating point.

thanks
# 4  
Old 08-20-2006
it works

Your command works fine for me. I feel the sed isn't necessary and the sort should be based on key - syntax is
Code:
sort -k3

Code:
$>echo "12 13 45.5%" |awk 'NR<5{printf("%-20d %-20d %-20.1f \n " ,$1,$2,$3)}'|sed 's/%//'
12                   13                   45.5

Code:
$>echo "12 13 45.5%" |awk 'NR<5{printf("%-20d %-20d %-20.1f\n",$1,$2,$3)}'
12                   13                   45.5

# 5  
Old 08-20-2006
i cut and pasted your code onto my command line and got the same answer as before...

third column is 0.0

what could be causing this then I'm confused.
# 6  
Old 08-20-2006
Also, when I add a fourth field why does the output double space the lines?

Code:
echo "12 13 45.5%" |awk 'NR<5{printf("%-20d %-20d %-20d %-20.1f\n",$1,$1,$2,$3)}'

i'm only new at UNIX and it seems there are no end of little traps and caveats which i don't understand
# 7  
Old 08-21-2006
The problem seems to be the % column is treated as a string on my account rather than a number - so I'm getting 0.0

I think i need to speak to my sys admin as it isn't doing this on their account on the same unix system.

Thanks for everyone's help. I now know the problem which is a major step.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

4. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

5. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

6. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

7. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

8. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

9. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies
Login or Register to Ask a Question