unix-在2文本文件中查找不匹配的行

unix-在2文本文件中查找不匹配的行,unix,awk,grep,diff,comm,Unix,Awk,Grep,Diff,Comm,我试着做比赛和非比赛练习..我试过grep和diff。。。但它只适用于整条生产线。。。。是否可以匹配以下文件中的行 如果我有2个文件: 文件1: cat_cute green_apple_sour red_bean_big red_pepper_three ginger_yellow 文件2: cat green_apple red_pepper papaya 输出: (file1) red_bean_big ginger_ye

我试着做比赛和非比赛练习..我试过grep和diff。。。但它只适用于整条生产线。。。。是否可以匹配以下文件中的行

如果我有2个文件:

文件1:

cat_cute    
green_apple_sour    
red_bean_big    
red_pepper_three    
ginger_yellow
文件2:

cat    
green_apple    
red_pepper    
papaya
输出:

(file1)
red_bean_big    
ginger_yellow            
(file2)    
papaya
在我发布这个问题之前,我成功地尝试了下面的方法。我很抱歉没有在我的第一篇文章中列出这一点。 我将文件2设置为目标匹配。对于文件1,我使用TCL删除所有不需要的形容词。然后我得到新的文件1

新文件1:

cat    
green_apple    
red_bean    
red_pepper    
ginger
然后我申请:

grep -Fxvf newfile1 file2
我得到了我想要的输出

我只是想知道是否有其他方法可以只使用grep命令而不使用TCL regsub进程。我试过awk,comm和grep。只有当整条线100%匹配时才匹配

谢谢。

在awk:

$ awk '
NR==FNR {                  # first file
    a[$1]                  # hash entries to a
    next                   # next record
}
{                          # second file
    for(i in a)            # for each record go thru the whole of a
        if(i~$1||$1~i) {   # see if there is a match
            delete a[i]    # del if
            next           # and skip to next record
        }                  
    b[$1]                  # else store entry to b
}
END {                      # in the end
    print "(file1)" 
    for(i in a) 
        print i            # output a and
    print "(file2)"
    for(i in b) 
        print i            # b entries
}' file1 file2
(file1)
ginger_yellow
red_bean_big
(file2)
papaya

我没有完全理解你的问题。顺便说一句,您可以通过
awk
轻松完成

awk -F'_' 'FILENAME=="file2.txt" {a[$1$2]=$1$2} FILENAME=="file1.txt" {if(!(a[$1] || a[$1$2])) {print}}' file2.txt file1.txt
它将打印:

red_bean_big    
ginger_yellow
papaya
对于文件2,您可以颠倒顺序(只需稍作更改)

上述命令将打印:

red_bean_big    
ginger_yellow
papaya

我希望我能清楚地理解你的问题。如果您需要其他内容,请在下面进行评论

如果在文件1中有一行
dogs\u eat\u papaya
,那么这行应该出现在输出中吗?
grep-v-f文件2文件1
似乎就是您所需要的全部。不。。。只需与前面完全匹配。因此,假设后面可以是任何数字。dog1 dog56 dog135在我的文件1中;狗排成一排。是否可能有输出:全部匹配?我投票结束这个问题,因为它似乎是一个对工具或解决方案的建议请求,而不是对您自己的代码的帮助请求。这使您的问题脱离StackOverflow的主题。如果这个评估是错误的,并且你确实需要帮助编写你自己的代码,那么请允许我收回我的投票。