linux - awk 脚本在某些条件下在两个文件的基础上查找、匹配和复制值

标签 linux shell awk scripting

需要您的帮助以 awk 为以下查询编写代码。
有两个文件。 如果column1(documenttype)中的值为INV,supplytype(column2)为CAN,则从column3(documentnumber)中取出对应的值, 并在 column2(follon doc) 的 file2 中找到该值(documentnumber),如果找到,则根据该 documentnumber(follon doc) 从 file2 中获取 column1(predecessr) 的值,并将从 column1(predecessr) 中找到的值粘贴到 file2在文件 1 的 column4(originaldocumentnumber) 中

对于前

如果文件 1 中的文件编号为 420075416,文件类型为 INV,供应类型为 CAN,那么我们可以在文件 2 的第 4 列和第 2 列(follon doc)中看到文件编号(420075416),然后根据该文件编号选择值(430071501) (420075416) 来自 column1(predecessr)(如果在 file2 中可用)并替换或粘贴到 file1 的 column4

文件1

documenttype    supplytype  documentnumber  originaldocumentnumber
INV              CAN        420075416        656565665
INV              CAN        429842808   
INV              BRB        429842808         85858585
INV              CAN        430071605   
RER              CAN        430071609

文件2

Predecessr  FollOn doc
420075200   430071605
429842808   430071609
429842807   429842808
430071501   420075416
429842807   429842808

输出文件

documenttype    supplytype  documentnumber  originaldocumentnumber
INV              CAN       420075416            430071501
INV              CAN       429842808            429842807
INV              BRB       429842808            85858585
INV              CAN       430071605            420075200
RER              CAN       430071609

只能写下面的代码,但是搞糊涂了没法继续

if '(NR==FNR)&&(FNR>1)&&($1==INV && $2==CAN){ar[$3]=NR;next}
    {for(i in ar){if

最佳答案

好吧,除了吸引眼球的间距之外,这应该可以解决问题:

$ awk '
NR==FNR {                              # process first file
    a[$2]=$1                           # hash, 2nd column as key, 1st its value
    next                               # next record
}                                      # second file:
$1=="INV" && $2=="CAN" && ($3 in a) {  # INV, CAN and 3rd column in hash
    $4=a[$3]                           # set 4th column value from the hash
    # flag=1                           # see comment
}
{
    $1=$1                              # rebuild the record 
    print                              # output
}
#END {
#    if(!flag) 
#        system("cat " FILENAME)
# }
' file2 file1                         # mind the order
documenttype supplytype documentnumber originaldocumentnumber
INV CAN 420075416 430071501
INV CAN 429842808 429842807
INV BRB 429842808 85858585
INV CAN 430071605 420075200
RER CAN 430071609

您可以将其通过管道传输到 column -t 以使其看起来更好:

$ awk ... | column -t 
documenttype  supplytype  documentnumber  originaldocumentnumber
INV           CAN         420075416       430071501
...

关于linux - awk 脚本在某些条件下在两个文件的基础上查找、匹配和复制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49519179/

相关文章:

regex - PERL 从配置文件中提取值

bash - 将不同长度的多列组合成BASH中的一列

bash - 快速帮助 bash 脚本删除大文件的行

php - 在 100 个文件中的 </body> 标签前批量插入代码

linux - AWK/Last Linux 计数登录

linux - 使用shell脚本删除前10个最大的常规文件

C - 套接字编程 - 我的服务器无法绑定(bind)到地址

python - 如何使用Paramiko exec_command获取每个依赖的命令执行输出

bash - unix - 如何从多个输出列表中创建单个字符串

linux - 增加一个环境变量