Substring n number of columns from two files with AWK

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 need a conclusion where the value of the 1st column from File1 will be subtracted from the 1st column of File2 and thus to column N, as shown below:

A  -1   0    -1  ........ (Na1-Na2)
B   1   0     0  ........ (Nb1-Nb2)

How to do this, is it AWK or Perl scripting in Linux environment?

+5
source share
6 answers

Something like that:

use strict;
use warnings;

my (@fh, @v);
for (@ARGV) {
  open (my $handle, "<", $_) or die ("$!: $_");
  push @fh, $handle;
}
while (@v = map { [split ' ', <$_> ] } @fh and defined shift @{$v[0]}) {
  print join(" ", (shift @{$v[1]}, map { $_ - shift(@{$v[1]}) } @{$v[0]})), "\n";
}
close $_ for (@fh);

For start:

 perl script.pl input1 input2
+1
source

This has already been answered, but I will add a single line. It uses pasteto combine files, and awkfor subtraction:

paste file{1,2} | awk '{for (i=1;i<=NF/2;i++) printf "%s ", ($i==$i+0)?$i-$(i+NF/2):$i; print ""}'

Validation:

$ cat file1
A   1   2    3   4  5
B   2   3    4   5  6

$ cat file2
A   2   2    4 10 12 
B   1   3    4  3 5

$ paste file{1,2} | awk '{for (i=1;i<=NF/2;i++) printf "%s ", ($i==$i+0)?$i-$(i+NF/2):$i; print ""}'
A -1 0 -1 -6 -7 
B 1 0 0 2 1

, . . , , .

+2

Try:

awk '{split($0,S); getline<f; for(i=2; i<=NF; i++) $i-=S[i]}1' OFS='\t' f=file1 file2
+2

: GNU awk. :

awk -f script.awk File2 File1 | rev | column -t | rev

script.awk:

FNR==NR {
    for(i=2;i<=NF;i++) {
        a[$1][i]=$i
    }
    next
}

{
    for(j=2;j<=NF;j++) {
        $j-=a[$1][j]
    }
}1

:

awk 'FNR==NR { for(i=2;i<=NF;i++) a[$1][i]=$i; next } { for(j=2;j<=NF;j++) $j-=a[$1][j] }1' File2 File1 | rev | column -t | rev

:

A  -1  0  -1
B   1  0   0
+2
awk 'FNR==NR{for(i=2;i<=NF;i++)a[FNR"-"i]=$i;next}{printf "\n"$1" ";for(i=2;i<=NF;i++){printf $i-a[FNR"-"i]" "}}' file1 file2
> cat file1
A   1   2    3
B   2   3    4
> cat file2
A   2   2    4
B   1   3    4
> awk 'FNR==NR{for(i=2;i<=NF;i++)a[FNR"-"i]=$i;next}{printf "\n"$1" ";for(i=2;i<=NF;i++){printf $i-a[FNR"-"i]" "}}' file1 file2 
A 1 0 1 
B -1 0 0 
>

#!/usr/bin/awk
FNR==NR{
   for(i=2;i<=NF;i++)
   a[FNR"-"i]=$i;next
     }
   {
    printf "\n"$1" ";
    for(i=2;i<=NF;i++)
    {
     printf $i-a[FNR"-"i]" "
    }
   }

:

awk -f file.awk file1 file2
+2

- ? , , .

STDOUT.

use strict;
use warnings;
use autodie;

my @fh;
for my $filename (@ARGV) {
  open my $fh, '<', $filename;
  push @fh, $fh;
}

until (grep eof $_, @fh) {
  my @records;
  for my $fh (@fh) {
    my $line = <$fh>;
    chomp $line;
    push @records, [ split ' ', $line ];
  }

  $records[0][$_] -= $records[1][$_] for 1 .. $#{$records[0]};
  print "@{$records[0]}\n";
}
+1

All Articles