使用命令行在两个文本文件中查找重复的单词

时间:2015-04-21 13:21:56

标签: unix awk command compare

我有两个文本文件:

f1.txt

boom Boom pow
Lazy dog runs.
The Grass is Green
This is TEST
Welcome

f2.txt

Welcome
I am lazy
Welcome, Green
This is my room
Welcome
bye

在Ubuntu命令行中我正在尝试:

awk 'BEGIN {RS=" "}FNR==NR {a[$1]=NR; next} $1 in a' f1.txt f2.txt

获得输出:

Green
This
is

我想要的输出是:

lazy
Green
This is
Welcome

说明:我想逐行比较两个txt文件。然后我想输出所有重复的单词。匹配应不区分大小写。此外,逐行比较将更好,而不是在整个f2.txt文件中查找来自f1.txt的匹配。例如,如果在第2行而不是f2.txt中的第5行

,则“欢迎”一词不应该在所需的输出中

1 个答案:

答案 0 :(得分:2)

那么,好吧。用awk:

awk 'NR == FNR { for(i = 1; i <= NF; ++i) { a[NR,tolower($i)] = 1 }; next } { flag = 0; for(i = 1; i <= NF; ++i) { if(a[FNR,tolower($i)]) { printf("%s%s", flag ? OFS : "", $i); flag = 1 } } if(flag) print "" }' f1.txt f2.txt

其工作原理如下:

NR == FNR {                                 # While processing the first file:
  for(i = 1; i <= NF; ++i) {                # Remember which fields were in
    a[NR,tolower($i)] = 1                   # each line (lower-cased)
  }
  next                                      # Do nothing else.
}
{                                           # After that (when processing the
                                            # second file)
  flag = 0                                  # reset flag so we know we haven't
                                            # printed anything yet
  for(i = 1; i <= NF; ++i) {                # wade through fields (words)
    if(a[FNR,tolower($i)]) {                # if this field was in the
                                            # corresponding line in the first
                                            # file, then
      printf("%s%s", flag ? OFS : "", $i)   # print it (with a separator if it
                                            # isn't the first)
      flag = 1                              # raise flag
    }
  }
  if(flag) {                                # and if we printed anything
    print ""                                # add a newline at the end.
  }
}