File 在unix中更新列
需要提供以下结果的Unix代码或脚本: 假设文件1有以下数据File 在unix中更新列,file,unix,replace,File,Unix,Replace,需要提供以下结果的Unix代码或脚本: 假设文件1有以下数据 ABC 1 cvb DEF 2. bnm ABC 3 DEF 4 文件2有以下数据 ABC 1 cvb DEF 2. bnm ABC 3 DEF 4 输出文件应该包含如下数据 ABC 3 cvb DEF 4 bnm 使用awk: $ awk 'NR==FNR{a[$1]=$2;next}{$2=a[$1];print}' file2 file1 ABC 3 cvb DEF 4 bnm 解释: awk '
ABC 1 cvb
DEF 2. bnm
ABC 3
DEF 4
文件2有以下数据
ABC 1 cvb
DEF 2. bnm
ABC 3
DEF 4
输出文件应该包含如下数据
ABC 3 cvb
DEF 4 bnm
使用awk:
$ awk 'NR==FNR{a[$1]=$2;next}{$2=a[$1];print}' file2 file1
ABC 3 cvb
DEF 4 bnm
解释:
awk ' # using awk
NR==FNR { # process first file
a[$1]=$2 # hash to a on first field
next # next record
}
{ # process second file
$2=a[$1] # update second field from a hash
print # output
}' file2 file1 # mind the order of files
使用awk:
$ awk 'NR==FNR{a[$1]=$2;next}{$2=a[$1];print}' file2 file1
ABC 3 cvb
DEF 4 bnm
解释:
awk ' # using awk
NR==FNR { # process first file
a[$1]=$2 # hash to a on first field
next # next record
}
{ # process second file
$2=a[$1] # update second field from a hash
print # output
}' file2 file1 # mind the order of files