5

I have two folders A1 and A2. The names and the number of files are same in these two folders. Each file has 15 columns. Column 6 of each file in folder 'A1' needs to substrate from the column 6 of each file in folder 'A2'. I would like to print column 2 and 6(after subtraction) from each file to a folder A3 with the same filenames. How can I do this with awk?

f1.txt file in folder A1

RAM     AA   159.03  113.3  122.9  34.78    116.3 
RAM     BB   151.24   70    122.9  142.78   66.4
RAM     CC   156.70   80    86.2   70.1     54.8


  f1.txt file in folder A2    

RAM     AA   110.05  113    122.9    34.78    116.3
RAM     BB   150.15  70     122.9    140.60   69.4 
RAM     CC   154.70  89.2   86.2     72.1     55.8


desired output

AA   0
BB  2.18
CC  -2
user1588971
  • 55
  • 1
  • 1
  • 6
  • Could you guys please answer this question then: http://stackoverflow.com/questions/15049478/subtructing-n-number-of-columns-from-two-files-with-awk – Shumon Shumon Feb 24 '13 at 07:14

2 Answers2

17

Try this:

paste {A1,A2}/f1.txt | awk '{print $2,$6-$13}'

In bash: {A1,A2}/f1.txt will expand to A1/f1.txt A2/f1.txt (It's just a shortcut. Never mind.)
I use paste command to merge files vertically.
The awk command is quite simple here.

kev
  • 155,172
  • 47
  • 273
  • 272
  • Is `join` a shell command (for bash)? `{}` specifies input file set, the `/` the output file? – Levon Aug 10 '12 at 02:41
  • Yes, I test it in `bash`(Ubuntu 12.04). – kev Aug 10 '12 at 02:42
  • Thanks - providing some basic explanation with the solutions makes for better and more useful answers in my opinion. – Levon Aug 10 '12 at 02:44
  • This is way simpler and more thoroughly explained than the answer that was chosen as the best, which could not even fit on one line and contained no explanation of how anything worked. The first time I visited this page, this elegant solution was hidden and I didn't notice it. I hope the possibility of having this question moved to the top is strongly considered. – Nike Dec 11 '13 at 14:13
3

One way using awk:

awk 'FNR==NR { array[$1]=$2; next } { if ($1 in array) print $1, array[$1] - $2 > "A3/f1.txt" }' ~/A1/f1.txt ~/A2/f1.txt

FIRST EDIT:

Assuming an equal number of files in both directories (A1 and A2) with filenames paired in the way you describe:

for i in A1/*; do awk -v FILE=A3/${i/A1\//} 'FNR==NR { array[$1]=$2; next } { if ($1 in array) print $1, array[$1] - $2 > FILE }' A1/${i/A1\//} A2/${i/A1\//}; done

You will need to create the directory A3 first, or you'll get an error.

SECOND EDIT:

awk 'FNR==NR { array[$2]=$6; next } { if ($2 in array) print $2, array[$2] - $6 > "A3/f1.txt" }' ~/A1/f1.txt ~/A2/f1.txt

THIRD EDIT:

for i in A1/*; do awk -v FILE=A3/${i/A1\//} 'FNR==NR { array[$2]=$6; next } { if ($2 in array) print $2, array[$2] - $6 > FILE }' A1/${i/A1\//} A2/${i/A1\//}; done
Steve
  • 51,466
  • 13
  • 89
  • 103
  • Thank you for your answer. My file has 15 columns. I would like to print column2 and column6 after subtracting the values of column6. I changed $2,$6 instead of $1,$2 in your code.The results of subtraction are not correct. How can I change your code to get the correct values? – user1588971 Aug 10 '12 at 08:40
  • @user1588971: I am having trouble interpreting your request. Please consider updating your question with an example of your 15-column files as well as an example of the output you are expecting. I have made an edit to what I think you are asking for, but I may have completely mis-interpreted the question. – Steve Aug 10 '12 at 10:29
  • Thank you for your comment. I get the following error when I use your second edit code. awk: cmd. line:1: fatal: cannot open file `/home/nimi/A1/f1.txt' for reading (No such file or directory). when I use your first edit code for multiple files, I don't get any error. But the answer of the subtraction between column6 is incorrect. column2 is printing correctly. – user1588971 Aug 10 '12 at 23:21
  • @user1588971: I have tested the second edit and it works for me. The error you're getting suggests that your input, `A1/f1.txt`, can't be found. You may want to check that this file (and it's pair) exists. I have updated my answer to allow multiple files, but the script is essentially the same. – Steve Aug 11 '12 at 02:18