所以我有两个如下所示的文件:
文件1
userName | cpu% | command | date created
user1 101.6 plasma-de+ Thu Aug 8 09:30:17 MDT 2019
user2 100.0 plasma-de+ Thu Aug 8 09:30:17 MDT 2019
user3 102.0 plasma-de+ Thu Aug 8 09:30:17 MDT 2019
文件2
userName | cpu% | command | date created | date updated
user1 101.6 plasma-de+ Mon Aug 5 06:35:39 MDT 2019 Mon Aug 5 06:35:39 MDT 2019
user2 100.0 plasma-de+ Mon Aug 5 06:35:39 MDT 2019 Mon Aug 5 06:35:39 MDT 2019
命令运行后的 file2
userName | cpu% | command | date created | date updated
user1 101.6 plasma-de+ Mon Aug 5 06:35:39 MDT 2019 Thu Aug 8 09:30:17 MDT 2019
user2 100.0 plasma-de+ Mon Aug 5 06:35:39 MDT 2019 Thu Aug 8 09:30:17 MDT 2019
user3 102.0 plasma-de+ Thu Aug 8 09:30:17 MDT 2019 Thu Aug 8 09:30:17 MDT 2019
我想获取 file1 的 col1 ,如果 file2 的 col1 中有匹配项,则更新最后一列中的“更新日期”。如果没有匹配项,我想将 file1 的整行附加到 file2 并在该行附加一个“更新日期”值。
我目前正在使用awk 'NR==FNR{c[$1]++;next};c[$1] > 0' file2 file1
基线比较,但是如果存在匹配,则会错误地打印整行,而且我也无法弄清楚如何添加另一个条件来更新日期列。我也尝试在 shell 脚本中执行此操作。
答案1
$ cat tst.awk
BEGIN { OFS=" " }
NR==FNR {
if (NR > 2) {
user = $1
file1[user] = $0
sub(/^[[:space:]]*([^[:space:]]+[[:space:]]+){3}/,"")
date[user] = $0
}
next
}
$1 in file1 {
sub(/([[:space:]]+[^[:space:]]+){6}[[:space:]]*$/,"")
$0 = $0 OFS date[$1]
delete file1[$1]
}
{ print }
END {
for (user in file1) {
print file1[user] OFS date[user]
}
$ awk -f tst.awk file1 file2
userName | cpu% | command | date created | date updated
user1 101.6 plasma-de+ Mon Aug 5 06:35:39 MDT 2019 Thu Aug 8 09:30:17 MDT 2019
user2 100.0 plasma-de+ Mon Aug 5 06:35:39 MDT 2019 Thu Aug 8 09:30:17 MDT 2019
user3 102.0 plasma-de+ Thu Aug 8 09:30:17 MDT 2019 Thu Aug 8 09:30:17 MDT 2019