首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较两个文本文件,如果第二个文件有一行,其中包含第一个文件的两个列,则删除该行

比较两个文本文件,如果第二个文件有一行,其中包含第一个文件的两个列,则删除该行
EN

Stack Overflow用户
提问于 2021-01-15 08:15:44
回答 3查看 101关注 0票数 1

我有两个文件,如下所示。file1有两列,file2有不同的列数,这取决于行。我想比较这两个文件,如果$1和$2都在file1的一行file2中,我想删除这一行。另外,file2是逗号分隔的。如何使用awk来完成这一任务?或者其他的文字处理工具?

file1

代码语言:javascript
复制
5052 5051  
4952 4951  

file2

代码语言:javascript
复制
         2001,       5052,       7001,       5051,       1000  
         2002,       5052,       7001,       1500,       2500  
         2003,       5051,       3500,       4500,       4952  
         2004,       4952,       4999,       4500,       4951  

预期输出:

代码语言:javascript
复制
         2002,       5052,       7001,       1500,       2500  
         2003,       5051,       3500,       4500,       4952  

我已经尝试了下面的awk代码,但没有运行。

代码语言:javascript
复制
awk 'NR==FNR{A[$1]=$1;A[$2]=$2; next} {if ($0=A[$1] && $0=A[$2]){next} else {print $0}' file1 file2 >> test.inp
EN

Stack Overflow用户

发布于 2021-01-15 09:09:14

代码语言:javascript
复制
awk 'NR==FNR { map[$1]="1";map1[$2]="1";next } {lin=gensub(" ","","g",$0);split(lin,map3,",");ok=1;for (i in map3) { if (map1[map3[i]]==1 || map[map3[i]]==1 ) { ok=0 } } if (ok==1) { print $0 } }' file1 file2

解释:

代码语言:javascript
复制
awk 'NR==FNR {                                   # Process the first file
               map[$1]="";                       # Set up two arrays, one for 
                                                   the first space delimited 
                                                   field and the other for 
                                                   the second
               map1[$2]="";
               next                              # Skip to the next record
              } 
              {
               lin=gensub(" ","","g",$0);        # Process the second file and remove 
                                                   all spaces from the the line 
                                                   putting the result in a variable 
                                                   lin
               split(lin,map3,",");              # Split the variable lin into the 
                                                   array map3 based on commas as the 
                                                   separator
               ok=1;                             # Initialise a variable
               for (i in map3) { 
                 if (map1[map3[i]] || map[map3[i]]) { 
                    ok=0                         # Loop through each entry in the map3 
                                                   array (on the line) and check if it 
                                                   exists in map1 or map. If it does 
                                                   exist, set ok to 0
                 } 
               } 
               if (ok==1) { 
                 print $0                         # Only if the variable ok is 1, 
                                                    print
               } 
              }' file1 file2
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65732601

复制
相关文章

相似问题

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