Adding "1.123+4.242 = 5" problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding "1.123+4.242 = 5" problem
# 1  
Old 11-29-2011
Adding "1.123+4.242 = 5" problem

I wan't to add two numbers (e.g 1.123 and 4.242) from different files but the add operation seems to cut the decimals. Any ideas how to convert or rewrite?
(The numbers in the file are in 1.123 and not 1,123)

-------------------
Code:
awk 'NR>9{print $3,$4,$5}' rlx.4000.dump > pos_0
awk 'NR>9{print $3,$4,$5}' rlx.4050.dump > pos_1

paste pos_0 pos_1 | awk '{x0=$1+$4; y0=$2+$5; z0=$3+$6 ; print x1,y0,z0}' > pos_2

-------------------


Tommy

Last edited by Franklin52; 11-30-2011 at 03:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-29-2011
awk, by default, uses double precision (floating decimal) arithmetic.

Use:
Code:
 printf("%.3f %.3f %.3f\n", x1, y0, z0)

instead of your print x1, y0, z0 print statement
# 3  
Old 11-29-2011
That doesn't solve it. I think the problem is that the two numbers are read in with a dot decimal separator instead of a comma separator. How can I make awk keep the decimals ?
# 4  
Old 11-29-2011
Part of your problem might be related to the fact that you assign x0 yet print x1.

Code:
awk '{x0=$1+$4; y0=$2+$5; z0=$3+$6 ; print x0,y0,z0}'

If that change doesn't help, have a look in your two intermediate files, and run your paste command, piping it to more, to see what it is producing (determine where the data is getting messed up).
# 5  
Old 11-29-2011
Code:
awk 'END{print a[3],a[4],a[5]}FNR>9{for(i=2;i++<5;) {a[i]+=$i}}' rlx.4000.dump rlx.4050.dump > pos_2

# 6  
Old 11-29-2011
Code:
# echo "1.123 + 4.242" | bc
5.365


Last edited by balajesuri; 11-30-2011 at 02:12 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

4. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

7. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Unix "at" / "Cron" Command New Problem...Need help

Hi All, I am trying to schedule a one time job using the at command with the help of shell script for my project. The shell script should take a parameter as a command line argument from the at command itself. Is it possible to take a command line parameter for a shell script in the command... (3 Replies)
Discussion started by: Mohanraj
3 Replies
Login or Register to Ask a Question