I have two files with N number of columns
File1:
A 1 2 3 ....... Na1
B 2 3 4 ....... Nb1
File2:
A 2 2 4 ....... Na2
B 1 3 4 ....... Nb2
i want a output where 1st column value from File1 will be subtracted from 1st column of File2, and this way till column N as shown below:
A -1 0 -1 ........ (Na1-Na2)
B 1 0 0 ........ (Nb1-Nb2)
How to do this is AWK, or Perl scripting in Linux environment?
Try:
Alternatively put this in a file
and execute as:
Something like this perhaps? I'm afraid I can't test this code as I have no PC to hand at present.
This program expects the names of the two files as parameters on the command line, and outputs the results to
STDOUT
.Here's one way using
GNU awk
. Run like:Contents of
script.awk
:Alternatively, here's the one-liner:
Results:
Something like this:
To run:
This has already been answered, but I will add a one-liner. It uses
paste
, to concatenate the files, andawk
to subtract:Validation:
It requires both files to have the same number of columns. Non-numeric columns should be at the same position. It prints the value in the first file if non-numeric, otherwise prints the difference.