例如:我有两个文件
输入文件
one
two
three
four
five
Run Code Online (Sandbox Code Playgroud)
输出.txt
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
我想合并这两个文件并获得另一个这样的输出文件(例如 match.txt),
one 1
two 2
three 3
...
Run Code Online (Sandbox Code Playgroud)
此外,当我随机打乱这两个 .txt 文件时,输出文件(match.txt)也会像这样合并正确的数据......
three 3
two 2
five 5
...
Run Code Online (Sandbox Code Playgroud)
我如何编写shell脚本?
只需使用paste
命令:
paste -d' ' input.txt output.txt > match.txt
Run Code Online (Sandbox Code Playgroud)
的match.txt
内容:
one 1
two 2
three 3
four 4
five 5
Run Code Online (Sandbox Code Playgroud)
与混洗(通过sort
命令):
paste -d' ' input.txt output.txt | sort -R
Run Code Online (Sandbox Code Playgroud)
示例输出:
two 2
four 4
one 1
three 3
five 5
Run Code Online (Sandbox Code Playgroud)