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


 
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
# 8  
Old 01-10-2010
Hello ,
perhaps you would want a little faster with larger data sets. use the Swiss Knife. Smilie
Code:
gaurav@localhost:~$ cat file1 
5
6
7
8
9
gaurav@localhost:~$ cat file2 
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
gaurav@localhost:~$ perl -e 'open(F1,"<file1");open(F2,"<file2");while(($l1=<F1>)&&($l2=<F2>)) {foreach (split(/\s+/,$l2)){ print $_ - $l1," ";}print "\n"}close(F1);close(F2);'
-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 
gaurav@localhost:~$

Regards.
Gaurav.
# 9  
Old 01-10-2010
Brilliant!!! Smilie
# 10  
Old 01-10-2010
Also suitable for large files:
Code:
awk '{getline x<f; for(i=1;i<=NF;i++)$i=$i-x}1' f=file1 file2

# 11  
Old 01-10-2010
Quote:
Originally Posted by danmero
awk only Smilie
Code:
awk 'NR==FNR{a[NR]=$0;next}{for(i=0;++i<=NF;){$i=($i-a[FNR])}}1'  file1 file2

The command gives a good sample on how to use NR and FNR.Smilie
# 12  
Old 01-10-2010
Quote:
Originally Posted by rdcwayx
The command gives a good sample on how to use NR and FNR.Smilie
Yep, but Scrutinizer solution is the best for large files Smilie
# 13  
Old 01-11-2010
I am trying to put a perl command in a csh script, but having problems. Output is empty.

Must be some problem when I try to pass the filenames perhaps.

Code:
    set file1 = $fin1.vtab
    set file2 = $fin2.vtab
    perl -e ' open(F1,"<$file1"); open(F2,"<$file2");    \
      while( ($l1=<F1>) && ($l2=<F2>) ) {                    \
        foreach (split(/\s+/,$l2)) { print $_ - $l1," "; }    \
        print "\n"                                                                          \
      }                                                                                           \
      close(F1); close(F2); ' > $fin1-$fin2.vtab

Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question