Take each number in table row and find the difference from the corresponding line


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Take each number in table row and find the difference from the corresponding line
# 1  
Take each number in table row and find the difference from the corresponding line

I have a two files containing numbers like below. First one contains one number on each line, the other is a table of numbers, each separated by a space. There are the same number of lines in each file.

I want to take each number in the row of the table and find the difference from the corresponding line of the first list. So each value in the row is subtracted from the value in the list at the corresponding line number using awk.

For example

Code:
5
6
7
8
9

Code:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
1 2 3 4 5
5 4 3 2 1

Result should be

Code:
-4 -3 -2 -1 0
-1 -2 -3 -4 -5
-6 -5 -4 -3 -2
-7 -6 -5 -4 -3
-4 -5 -6 -7 -8


Actual files will be something like below

Code:
0.4117983
0.4735530
0.6167929
0.7095670
0.7897300
0.8662801
0.9425200
1.020120
1.099846
1.181320
1.263659
1.347084
1.431774
1.520824
1.610451
1.701591
1.793289
1.882924
1.969465

Code:
0.403564 0.403564 0.403564 0.403564 0.403564
0.464079 0.464079 0.464079 0.464079 0.464079
0.604454 0.604454 0.604454 0.604454 0.604454
0.695379 0.695379 0.695379 0.695379 0.695379
0.773935 0.773935 0.773935 0.773935 0.773935
0.848954 0.848954 0.848954 0.848954 0.848954
0.923670 0.923670 0.923670 0.923670 0.923670
0.999718 0.999718 0.999718 0.999718 0.999718
1.077850 1.077850 1.077850 1.077850 1.077850
1.157690 1.157690 1.157690 1.157690 1.157690
1.238390 1.238390 1.238390 1.238390 1.238390
1.320140 1.320140 1.320140 1.320140 1.320140
1.403130 1.403130 1.403130 1.403130 1.403130
1.520820 1.520820 1.520820 1.520820 1.520820
1.642660 1.642660 1.642660 1.642660 1.642660
1.735620 1.735620 1.735620 1.735620 1.735620
1.829160 1.829160 1.829160 1.829160 1.829160
1.920580 1.920580 1.920580 1.920580 1.920580
2.008860 2.008860 2.008860 2.008860 2.008860


Last edited by kristinu; 01-10-2010 at 10:27 AM..
# 2  
Quote:
paste -d" " file1 file2 | awk ' { var=$1; for( i=2; i<=NF; ++i ) { $(i-1) = $i - var }; NF=NF-1; print } '
# 3  
It works great. Thanks buddy. Smilie
# 4  
awk only Smilie
Code:
awk 'NR==FNR{a[NR]=$0;next}{for(i=0;++i<=NF;){$i=($i-a[FNR])}}1'  file1 file2

# 5  
It works ok, but there is a feature I never encountered before. What's the purpose of the 1 at the end. Without it does not work when I redirect the output to a file. So it must be quite important!!!
# 6  
Quote:
brksvx58# cat file
0.4117983
0.4735530
0.6167929
brksvx58# awk ' { print } ' file
0.4117983
0.4735530
0.6167929
brksvx58# awk ' 1 ' file
0.4117983
0.4735530
0.6167929
1 means true equivalent to { print }
# 7  
Oh. Ok, I get it!!!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #367
Difficulty: Medium
The Single UNIX Specification specifies vi, so every conforming UNIX system must have it.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do we display specific row of an output from bottom given line number?

I pass a number to my script. Passing "1" below. ./getfile.sh 1 echo "User entered: $1" ls -ltr *.conf | sed -n '$p' I wish to use ls -ltr i.e list files in ascending order of time the latest showing at the bottom of the output. Number 1 should get me the last row of ls -ltr output i.e... (9 Replies)
Discussion started by: mohtashims
9 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

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

4. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

5. Shell Programming and Scripting

In php, Moving a new row to another table and deleting old row

Hi, I already succeed moving a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). But it can't delete the old row. Please help me with the script. my php script: INSERT INTO... (2 Replies)
Discussion started by: jazzyzha
2 Replies

6. Shell Programming and Scripting

Moving new row and deleting old row to another table

Hi, I want to move a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). I already use this script but doesn't work as I expected. CHECK_KEYWORD="$( mysql -uroot -p123456 smsd -N... (7 Replies)
Discussion started by: jazzyzha
7 Replies

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

8. UNIX for Dummies Questions & Answers

Shell Script: Traverse Database Table Row by Row

Hello Everyone, My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row. I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table. Please help. Thanks &... (5 Replies)
Discussion started by: ahsan.asghar
5 Replies

9. Shell Programming and Scripting

search for difference and find the above line

hello All, i have to two files package.today and package.yesterday , the extension of the files says what day the file belongs to . contents of the file change little bit everyday . i am taking only the package and sub package from the files awk '/^/{print $0}' Packages.today 82 ... (0 Replies)
Discussion started by: posner
0 Replies

10. UNIX for Dummies Questions & Answers

To find the Row number

Hi Can any one tell me what is the command to find out the row id or row number for a particular record Thanks sri (6 Replies)
Discussion started by: laxmi131
6 Replies

Featured Tech Videos