Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 在unix中更新列_File_Unix_Replace - Fatal编程技术网

File 在unix中更新列

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 '

需要提供以下结果的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 '            # 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