例如:我有两个文件
输入.txt
one
two
three
four
five
输出.txt
1
2
3
4
5
我想合并这两个文件并获得另一个输出文件(例如,match.txt),如下所示,
one 1
two 2
three 3
...
此外,当我随机打乱这两个 .txt 文件时,输出文件(match.txt)也会合并正确的数据,就像这样......
three 3
two 2
five 5
...
如何编写shell脚本?
答案1
简单地与paste
命令:
paste -d' ' input.txt output.txt > match.txt
内容match.txt
:
one 1
two 2
three 3
four 4
five 5
和洗牌(通过sort
命令):
paste -d' ' input.txt output.txt | sort -R
示例性输出:
two 2
four 4
one 1
three 3
five 5
答案2
$ cat input.txt
five
one
three
two
four
$ awk 'BEGIN{a["one"]=1;a["two"]=2;a["three"]=3;a["four"]=4;a["five"]=5}$0 in a{print $0,a[$0]}' input.txt
five 5
one 1
three 3
two 2
four 4