【问题标题】:removing duplicate lines that occur every certain number of lines using sed or other使用 sed 或其他方法删除每隔一定数量的行出现的重复行
【发布时间】:2014-01-03 20:21:17
【问题描述】:

我有一个巨大的文件,我想删除仅每 3 行出现一次的重复行。 是否可以使用 sed 或任何类似的命令?

我的文件如下所示:

this is text

1234

1234

this is another text

5678

5678

第二个数字是第一个数字的副本,我想删除文件每 3 行的第二个数字(第三行)。我不使用较少文件名的原因 | uniq 是数字可能会在文件中重复(在 3 行范围之外),然后我不希望它们被删除。

谢谢

【问题讨论】:

  • 您的数据到底是什么样的?
  • 是的;请用一些示例输入和所需的输出更新您的问题;让snswer变得更容易:-)

标签: linux sed


【解决方案1】:

这能解决您的问题吗?

$ awk 'NR%3!=0' input
this is text


1234
this is another text


5678

使用 sed:

$ sed '0~3d' input
this is text


1234
this is another text


5678

Perl:

$ perl -n -e '$.%3!=0&&print' input
this is text


1234
this is another text


5678

但是,话说回来,我可能误解了这个问题......

【讨论】:

    【解决方案2】:

    uniq 实用程序仅过滤掉相邻的行(您的输入是否真的在每行之间有一个空白行?)。否则可以使用:

    this is text
    1234
    1234
    this is another text
    1234
    1234
    

    uniq input.txt 给出:

    this is text
    1234
    this is another text
    1234
    

    【讨论】:

      【解决方案3】:

      这可能对你有用(GNU sed):

      sed -r 'n;$!N;s/^([^\n]*)\n\1$/\1/' file
      

      打印三个中的第一行,如果第三行与第二行重复,则删除第三行。

      【讨论】:

        【解决方案4】:

        假设你的输入是什么以及你想输出什么:

        awk 'NR%3 == 2 {val=$0} NR%3 == 0 && $0 == val {next} 1' <<END
        this is text
        1234
        1234
        this is another text
        5678
        5678
        foo
        bar
        qux
        END
        
        this is text
        1234
        this is another text
        5678
        foo
        bar
        qux
        

        【讨论】:

          猜你喜欢
          • 2018-11-15
          • 2012-02-10
          • 2014-04-26
          • 1970-01-01
          • 2013-09-13
          • 1970-01-01
          • 2012-10-21
          • 1970-01-01
          • 2019-12-11
          相关资源
          最近更新 更多