我正在尝试使用以下awk命令从一个文件到另一个文件中找到单词:
path.log
文件内容为:
文件1:
vidhu 1
gangwar 2
file2的:
1 1
2 4980022
预期产出:
vidhu 1
gangwar 4980022
但输出就是这样:
vidhu 1
4980022
帮我找出这个问题。
答案 0 :(得分:4)
@try:
awk 'FNR==NR{A[$2]=$1;next} ($1 in A){print A[$1], $2}' Input_file1 Input_file2
<强>解释强>
awk 'FNR==NR #### Checking condition of FNR==NR which will be TRUE when first Input_file1 is being read.
{A[$2]=$1; #### create an array named A with index of field 2 and have value as first field.
next} #### using next keyword(built-in awk) so it will skip all next statements.
($1 in A) #### Now checking if first field of file2 is present in array A, this will be checked only when Input_file2 is being read.
{print A[$1], $2 #### printing value of array A's value whose index is $1 and $2 of Input_file2.
}' Input_file1 Input_file2 #### Mentioning the Input_file1 and Input_file2 here.
答案 1 :(得分:0)
您也可以使用以下内容:
join -1 2 -2 1 file1 file2 -o 1.1,2.2
假设您的文件按顺序排列。
如果没有,那么先对它们进行排序。