Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要比较两个文件中的两列,并使用unix将所有列写入新文件_Unix - Fatal编程技术网

需要比较两个文件中的两列,并使用unix将所有列写入新文件

需要比较两个文件中的两列,并使用unix将所有列写入新文件,unix,Unix,我在unix环境中有2个文件 文件1: 1 a 2 b 3 c 文件2: ---------- ## Heading ## 2 bb 1 aa 3 cc 如何获取输出文件 输出: 1 a 1 aa 2 b 2 bb 3 c 3 cc 使用unix shell脚本编写使用awk。这是awk的经典之作: $ awk 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1],$0}' file1 file2 2 b 2 bb 1 a 1 aa 3 c 3 cc 解

我在unix环境中有2个文件

文件1:

1 a
2 b
3 c
文件2:

----------
## Heading ##

2 bb
1 aa
3 cc
如何获取输出文件

输出:

1 a 1 aa
2 b 2 bb
3 c 3 cc

使用unix shell脚本编写

使用awk。这是awk的经典之作:

$ awk 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1],$0}' file1 file2
2 b 2 bb
1 a 1 aa
3 c 3 cc
解释:

$ awk '
NR==FNR {             # process first file
    a[$1]=$0          # hash record, use first field as hash key
    next              # move to next record
}
$1 in a {             # second file, if key found in the hash
    print a[$1],$0    # output it from the hash along with the current record
}' file1 file2        # mind the order

订单将是第二个文件订单。如果您想以其他顺序进行排序,可以对文件进行排序(
awk…file1请查找文件详细信息文件1 a 2 b 3 c文件2 b 22 a 11 c 33输出1 a 11 2 b 22 3 c 33有一个专门的站点:另外,
人1粘贴
粘贴
粘贴
。它被称为
粘贴
。此外,它也是一个完美的例子:)@是的,我知道。只是试着按照定义生活(专业和热心程序员的问答网站:)(顺便问一下,你是说
加入
?)