Operations on columns of 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Operations on columns of 2 files
# 1  
Old 09-12-2008
Operations on columns of 2 files

Hi
I have 2 file with many lines and colums and i want to do some operation for each value in the 2 files :

Matrix1 :
a11 a12 a13 a14 ...
a21 a22 a23 a42 ...
a31 a32 a33 a32 ...
...

Matrix2 :
b11 b12 b13 b14 ...
b21 b22 b23 b42 ...
b31 b32 b33 b32 ...
...

I want to have the operation in new matrix :
(a11-b11) (a12-b12) (a13-b13) (a14-b14) ...
(a21-b21) (a22-b22) (a23-b23) (a24-b24) ...
(a31-b31) (a32-b32) (a33-b33) (a34-b34) ...
...

Thanks a lot
# 2  
Old 09-12-2008
One option is to use awk, load the first file into a 2-dimensional array, then output the differences when you process the second file.
# 3  
Old 09-12-2008
{
for(i=1; i<=NF; i++)
a[i, NR]=$i
nf=NF;nr=NR
}

how can process the second file?
# 4  
Old 09-12-2008
If you use awk -f yourscript.awk file1 file2 then you can use this trick:

Code:
FNR==NR {
for(i=1; i<=NF; i++)
a[i, NR]=$i
nf=NF;nr=NR
next
}
{
# process second file here
}

Because FNR is only equal to NR while processing the first file, the first part of code is only executed for that file.
# 5  
Old 09-12-2008
Something is rong in my script !


Code:
FNR==NR {
for(i=1; i<=NF; i++)
a[i, NR]=$i
nf=NF;nr=NR
next
}
{
for(i=1; i<=NF; i++)
    b[i, NR]=$i
  nbf=NF;nbr=NR
}
END {
  for(f=1; f<=nf;f++)
     for(r=1; r<=nr;r++)
       printf("%s%s", a[f,r]-b[f,r], (r==nr) ? "\n" : FS)
}

# 6  
Old 09-12-2008
FNR is reset to 1 at the beginning of each input file. NR is not.

Code:
FNR==NR {
for(i=1; i<=NF; i++)
a[i, FNR]=$i
nf=NF;nr=FNR
next
}
{
for(i=1; i<=NF; i++)
    b[i, FNR]=$i
  nbf=NF;nbr=FNR
}
END {
  for(f=1; f<=nf;f++)
     for(r=1; r<=nr;r++)
       printf("%s%s", a[f,r]-b[f,r], (r==nr) ? "\n" : FS)
}

# 7  
Old 09-12-2008
It work fine
Thanks !

Last edited by rauchy; 09-12-2008 at 04:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

3. Shell Programming and Scripting

Read and write operations on files.

Dears. kindly guide !!! I have data, which is delimited by | . it should contain 26 columns, but one column data contain | makes few row to 27 columns. I want to find rows have 27 columns and then concatenate the specific columns to single column to make it 26 columns. Kindly help, Can... (3 Replies)
Discussion started by: sadique.manzar
3 Replies

4. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

5. Shell Programming and Scripting

Sorting operations on several files

I have over 250 files (named grad.1000, grad.1001, grad.1002) - see attachment - that have this format: # 0.004 0.692758 # 6.23025467936 6.23025467936 6.23025467936 44.9620453206 44.9620453206 44.9620453206 # 0.8 1.19989 0.99914606306 1.0117015948 1.03761854021 1.07717125288 1.13095455063... (4 Replies)
Discussion started by: kayak
4 Replies

6. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

7. Shell Programming and Scripting

columns into different files

hello all, my input file(excel workbook) has the following structure mir-111 mir-222 mir-333 mir-444 abc,45 h,5 jhu,k jhg,111111 def,56 u,90 kil,10 k,0987 g,5 ki,09 huy,09 o,00000 j,7 molkijhn,88 erf,111 u,9876 k,8 kou,098 eef,122 jjj,kkkkkk now, i would like to break... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

8. Shell Programming and Scripting

Math operations with file columns values.

Hello everybody, I have some large files containing statistical data. The data is stored in the following generic format: >cat my_file 1, 2, 3 1, 2, 3 1, 2, 3 > The values of columns no.2 and 3 are expressed in bytes. I would like to transform them in Megabytes, by dividing them with... (3 Replies)
Discussion started by: fabian23
3 Replies

9. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

10. Shell Programming and Scripting

Matrix Operations of two files

Hi , I have two files aaa.txt (which contains) 1 2 3 4 5 6 7 8 9 10 11 12 and bbb.txt (which contains) -1 -2 -3 -4 -5 -6 5 -8 0 3 0 0 the output that I intended to have is 0 0 0 0 0 0 6 0 4.5 6.5 5.5 6 i.e. Averaging the script is in the file abc Begin{START of the... (2 Replies)
Discussion started by: narendra_linux
2 Replies
Login or Register to Ask a Question