匹配两个文件的列元素并使用 AWK/PERL 将其替换为匹配的行
我有两个文件,每个文件有 3 列。我想将 file1 的第 3 列的元素与 file2 的第 3 列进行匹配。如果匹配,则将 file1 的整行替换为 file2 中与匹配项对应的行,否则移至下一行。
示例如下: 在 file2 中,第 3 列元素 reg[2] 和 reg[9][9] 出现在 file1 的第 3 列中。因此,file1 的相应行被 file2 的行替换。
文件1:
Nancy Owen reg[2]
Nancy Owen reg[4_8]
Nancy Owen reg[7]
Nancy Owen reg[9][9]
Nancy Owen reg[54]
文件2:
Done Approval reg[9][9]
Nancy Owen reg[10_8]
Nancy Owen reg[4][10]
Done Approval reg[2]
所需的输出
Done Approval reg[2]
Nancy Owen reg[4_8]
Nancy Owen reg[7]
Done Approval reg[9][9]
Nancy Owen reg[54]
尝试的代码:
awk -F, 'NR==FNR{a[$3]=$0;next;}a[$3]{$0=a[$3]}1' file2 file1
我仍然是使用oneliner awk命令的新手。我在上面的代码中肯定做错了什么。我想做的是将第三列以键的形式放置,将整行作为值。如果该键存在于 file1 的第 3 列中,则将 fil1 当前行替换为 file2 中的当前值。否则跳过并移至下一行。
I have two files each having 3 columns. I want to match element of column 3 of file1 with column3 of file2. If it matches, replace the entire line of file1 with the line corresponding to the match from file2, otherwise move to the next line.
Below is the example: In file2, the 3rd column element reg[2] and reg[9][9] are present in the column 3 of file1. So, the corresponding line of file1 is replaced with that of line from file2.
File1:
Nancy Owen reg[2]
Nancy Owen reg[4_8]
Nancy Owen reg[7]
Nancy Owen reg[9][9]
Nancy Owen reg[54]
File2:
Done Approval reg[9][9]
Nancy Owen reg[10_8]
Nancy Owen reg[4][10]
Done Approval reg[2]
Desired output
Done Approval reg[2]
Nancy Owen reg[4_8]
Nancy Owen reg[7]
Done Approval reg[9][9]
Nancy Owen reg[54]
Attempted code:
awk -F, 'NR==FNR{a[$3]=$0;next;}a[$3]{$0=a[$3]}1' file2 file1
I am still a novice in using oneliner awk commands. I am definitely doing something wrong in the above code. What I am trying to do is put the 3rd column in the form of key and the entire line as value. If the key exists in column3 of file1, replace fil1 current line with current value from file2. Otherwise skip and move to next line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
注意 perl
标签,这是一个 Perl 解决方案:
perl -ane 'if ($eof) {
if (exists $h{ $F[2] }) {
print $h{ $F[2] }
} else { print }
} else {
$h{ $F[2] } = $_;
$eof = 1 if eof;
}' -- file2 file1
-n
逐行读取输入,运行每行的代码;-a
将空白处的每一行分割到 @F 数组中;- 我们在第一个文件(即file2)的末尾设置变量$eof;
- 在读取第一个文件(file2)时,我们将每一行存储到由第三列作为键控的哈希中;
- 在读取第二个文件 (file1) 时,我们检查哈希是否包含第三列的行:如果是,则打印它,否则打印当前行。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我将使用 GNU
AWK
以下方式,让file1.txt
内容为file2.txt
内容,然后
输出
说明:从处理
file2.txt
通过将每一行存储在数组arr
中,键为第三列 ($3
) 值,不执行任何其他操作(因此next用法),然后处理
file1.txt
如果 arr 键中存在第三个值($3 in arr
)则print
相应的值,否则print
当前行($0
)。为此,我采用了所谓的三元运算符条件?
valueiftrue:
valueiffalse(在 GNU Awk 5.0.1 中测试)
I would use GNU
AWK
following way, letfile1.txt
content beand
file2.txt
content bethen
output
Explanation: start from processing
file2.txt
by storing each line in arrayarr
under key being 3rd column ($3
) value, do nothing else (thusnext
usage), then processfile1.txt
if 3rd value is present among arr keys ($3 in arr
) doprint
corresponding value otherwiseprint
current line ($0
). In order to do so I employ so-called ternary operator condition?
valueiftrue:
valueiffalse(tested in GNU Awk 5.0.1)