在bash中逐行并行显示两个文件内容[重复]

问题描述 投票:0回答:1

例如,我有两个文本文件:

file1.txt

line 1 file 1
line 2 file 1
...

file2.txt

line 1 file 2
line 2 file 2
...

我确定file1.txtfile2.txt的行数相同。

我想要的是如何使用以下格式导出到file3.txt

line 1 file 1
line 1 file 2

line 2 file 1
line 2 file 2

...

我只想逐行查看这些内容。命令diff file1.txt file2.txt对我没有帮助,

bash ubuntu
1个回答
0
投票
awk 'FNR==NR{a[FNR]=$0;next} {print a[FNR] ORS $0}' Input_file1 Input_file2

输出将如下。

line 1 file 1
line 1 file 2
line 2 file 1
line 2 file 2

说明:添加以上代码的详细说明。

awk ' ##Starting awk program from here. FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when 1st Input_file named file1 is being read here. a[FNR]=$0 ##Creating an array named a whose index is FNR and value is current line value. next ##next will skip all further statements from here. } ##Closing BLOCK for FNR==NR condition here. { ##Block from here will be executed when Input_file2 will be started being read. print a[FNR] ORS $0 ##Printing array a value with current FNR status ORS(new line) and current line value here. } ##Closing BLOCK for above opened one here. ' file1 file2 ##Mentioning Input_file names here.
© www.soinside.com 2019 - 2024. All rights reserved.