Linux 如何比较列ID中两个文件中的行?
我有两个动态长度为1到30行的文件,这些数据:Linux 如何比较列ID中两个文件中的行?,linux,bash,file,compare,Linux,Bash,File,Compare,我有两个动态长度为1到30行的文件,这些数据: [File1] Time | Name | Name | ID1 | ID2 10:50 | Volume | Xxx | 55 | 65 12:50 | Kate | Uh | 35 | 62 15:50 | Maria | Zzz | 38 | 67 15:50 | Alex | Web | 38 | 5 ... [File2] Time | Name | Name | ID1 | ID2 10:50 | Les | Xxx
[File1]
Time | Name | Name | ID1 | ID2
10:50 | Volume | Xxx | 55 | 65
12:50 | Kate | Uh | 35 | 62
15:50 | Maria | Zzz | 38 | 67
15:50 | Alex | Web | 38 | 5
...
[File2]
Time | Name | Name | ID1 | ID2
10:50 | Les | Xxx | 31 | 75
15:50 | Alex | Web | 38 | 5
...
如何将两个文件[仅ID1和ID2列]:[File1]和[File2]与文件{File1]的所有第一行与{File2]的所有行进行比较。
如果数据存在于保存到文件的两个文件中[File3]数据添加字符*
除了文件{File3]之外,还有来自[File1]的其他数据
结果:
[File3]
Time | Name | Name | ID1 | ID2
15:50 | Alex | Web | * 38 | 5
10:50 | Volume | Xxx | 55 | 65
12:50 | Kate | Uh | 35 | 62
15:50 | Maria | Zzz | 38 | 67
使用awk
awk 'BEGIN{t="Time | Name | Name | ID1 | ID2"}
FNR==1{next}
NR==FNR{a[$4 FS $5];next}
{ if ($4 FS $5 in a)
{$4="*"$4;t=t RS $0}
else{s=s==""?$0:s RS $0}
}
END{print t RS s}' FS=\| OFS=\| file2 file1
Time | Name | Name | ID1 | ID2
15:50 | Alex | Web |* 38 | 5
10:50 | Volume | Xxx | 55 | 65
12:50 | Kate | Uh | 35 | 62
15:50 | Maria | Zzz | 38 | 67
解释
BEGIN{t="Time | Name | Name | ID1 | ID2"} # set the title
FNR==1{next} # ignore the title, FNR is the current record number in the current file.for each file
NR==FNR{a[$4 FS $5];next} # record the $4 and $5 into Associative array a
{ if ($4 FS $5 in a)
{$4="*"$4;t=t RS $0} # if found in file1, mark the $4 with start "*" and attach to var t
else{s=s==""?$0:s RS $0} # if not found, attach to var s
{print t RS s} # print the result.
输出文件是否有任何预期的排序顺序?非常感谢您的帮助我在结尾处添加了对文件的写入:>File3以及如何执行生成的文件File3中的所有行都已保存变量?您可以详细更新您的请求吗?请提供一些示例来描述您需要的内容,并输入原始问题N