首页 > 解决方案 > 在文件中查找以下部分重复的行

问题描述

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

示例文件:

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

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

任何想法 ?

标签: awkgrep

解决方案


输入:

$ 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
}

推荐阅读