首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >awk根据字段中的匹配将特定字段添加到文件中

awk根据字段中的匹配将特定字段添加到文件中
EN

Stack Overflow用户
提问于 2017-07-06 12:39:29
回答 2查看 342关注 0票数 0

我试图使用awk$4$5$6字段和tab-delimeted file2中的标头添加到file2 $2中在file1中有一个匹配的$3值的行中。我在每一行中都添加了评论,以及我对正在发生的事情的理解。谢谢:)。

file1 tab-delimeted

代码语言:javascript
运行
复制
ID  Name    Number
0-0 A,A 123456
2-2 B,B 789123
4-4 C,C 456789

file2 tab-delimeted

代码语言:javascript
运行
复制
ID  Number  Name    Info1   Info2   Info3   Info4
0-0 123456  A,A aaaaa   bbbbb   ccccc   eeeee
1-1 111111  Z,Z aaa bbb ccc eee
2-2 789123  B,B aaaaa   bb,bbb  ccccc   eeeee
3-3 222222  Y,Y aaa bb,bb   cc  e
4-4 456789  C,C aaa bb  ccc eeee

期望输出 tab-delimeted

代码语言:javascript
运行
复制
ID  Name    Number  Info1   Info2   Info3
0-0 A,A 123456  aaaaa   bbbbb   ccccc
2-2 B,B 789123  aaaaa   bb,bbb  ccccc
4-4 C,C 456789  aaa bb  ccc

awk

代码语言:javascript
运行
复制
awk -F"\t" '$3 in a{  # read $3 value of file1 into array a
 a[$3]=a[$2];   # match $3 array a from file1 with $2 value in file2
  next   # process next line
 }  # close block
  { print $1,$2,a[$2],$4,$5,$6  # print desired output
 }  # close block
    END {  # start block
 for ( i in a) {   # create for loop i to print
     print a[i]  # print for each matching line in i
  }  # close block
}' file1 file2
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-06 13:19:19

代码语言:javascript
运行
复制
$ awk -v OFS="\t" 'NR==FNR{a[$3]=$0;next}$2 in a{print a[$2],$4,$5,$6}' file1 file2
ID      Name    Number  Info1   Info2   Info3
0-0     A,A     123456  aaaaa   bbbbb   ccccc
2-2     B,B     789123  aaaaa   bb,bbb  ccccc
4-4     C,C     456789  aaa     bb      ccc

解释:

代码语言:javascript
运行
复制
$ awk -v OFS="\t" '         # tab as OFS also
NR==FNR{                    # for file1
    a[$3]=$0                # hash $0 to a using $3 as key
    next                    # no further processing for this record
}
$2 in a {                   # if $2 found in a
    print a[$2],$4,$5,$6    # output as requested
}' file1 file2              # mind the file order
票数 2
EN

Stack Overflow用户

发布于 2017-07-06 13:43:13

尝试:另一种方法是先阅读file2,然后阅读file1。

代码语言:javascript
运行
复制
awk -F"\t" 'FNR==NR{a[$1,$3,$2]=$4 OFS $5 OFS $6;next} (($1,$2,$3) in a){print $1,$2,$3,a[$1,$2,$3]}' OFS="\t" file2 file1

会在几分钟内增加解释。

编辑:添加非一元线性形式的解决方案和解释。

代码语言:javascript
运行
复制
awk -F"\t" 'FNR==NR{                              ####Checking condition FNR==NR which will be only true when first file named file2 is being read. Because FNR and NR both represent the number of lines for a Input_file, only difference is FNR value will be RESET whenever it is starting to read next Input_file and NR value will be keep on increasing till all the Input_files are being read.
                a[$1,$3,$2]=$4 OFS $5 OFS $6;     ####Creating an array named a whose index is $1,$3 and $2 and value is $4,$5 and $6. Where OFS is output field separator, whose default value is space.
                next                              ####next is awk built-in keyword which will NOT allow cursor to go further and will skip all next statements.
            }
     (($1,$2,$3) in a){                           ####Checking a condition which will be only checked when 2nd Input_file is being read. So checking here if $1, $2 and $3 is present in array a, then do following.
                        print $1,$2,$3,a[$1,$2,$3]####print the value of $1, $2,$3 and array a value whose index is $1,$2 and $3.
                      }
    ' OFS="\t" file2 file1                        ####Mentioning the Input_files here.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44949147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档