Subtructing列的n个由两个文件AWK [英] Subtructing n number of columns from two files with AWK

查看:111
本文介绍了Subtructing列的n个由两个文件AWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,​​列N多的

I have two files with N number of columns

文件1:

A   1   2    3  .......  Na1
B   2   3    4  .......  Nb1

文件2:

A   2   2    4  .......  Na2
B   1   3    4  .......  Nb2

我希望有一个输出,其中从文件1第1列值将从File2中的第一列中减去,这样一来,直到N列如下图所示:

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)

如何做,这是AWK,还是在Linux环境下的Perl脚本?

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

推荐答案

事情是这样的:

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);

要运行:

 perl script.pl input1 input2

这篇关于Subtructing列的n个由两个文件AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆