在文件中查找以下部分重复的行

时间:2019-05-15 21:39:09

标签: awk grep

使用shell命令,打印文件中包含上一行的任何行。

示例文件:

i love potatoes
i love shoes
i love super shoes
i love shoe
i love shoes

命令必须打印:“我爱鞋子” 因为它是唯一包含先前行内容的行(因为“ i love shoes”包含“ i love shoe”)

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

输入:

$ cat input
i love potatoes
i love shoes
i love super shoes
i love apple
i love apples
i eat melon
i eat melons
i eat apples and i eat melons

命令:

awk '{if(NR>1 && length(previous)>0 && index($0,previous)>0){print};previous=$0;}' input

输出:

i love apples
i eat melons
i eat apples and i eat melons

说明:

{
  #skip the first line as by definition there is no line before it
  #if the previous line is not empty and if the current line contains the previous line 
  # (e.g. you can find an index >0 of the previous line string in the current line), 
  #print the current line
  if (NR>1 && length(previous) > 0 && index($0, previous) > 0) { 
     print $0
  }
  #assign the current line to previous line and continue the processing
  previous = $0
}