是否有任何UNIX/Linux命令可以检查两行是否连续 例如,考虑一个文件句子。 This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence X This is sentence Y This is sentence Y

是否有任何UNIX/Linux命令可以检查两行是否连续 例如,考虑一个文件句子。 This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence Y This is sentence X This is sentence X This is sentence Y This is sentence Y,linux,bash,unix,text,awk,Linux,Bash,Unix,Text,Awk,我们首先看到,这是一个句子X,这是一个句子Y。 是否有命令检查两行是否连续 这是第X句,后面是第X句或 这是句子Y,后面是句子Y。在第11行和第12行中,我们看到2行被重复 您甚至不需要为此使用awk! 您可以简单地使用uniq命令 说明: uniq是一个非常方便的命令,它可以打印文件中的连续重复项,对它们进行计数等。在这里,我使用选项-d来打印重复的连续行 奖金: 如果要添加找到重复项的行,则可以使用以下命令: $ cat -n sentences.txt 1 This is se

我们首先看到,这是一个句子X,这是一个句子Y。 是否有命令检查两行是否连续 这是第X句,后面是第X句或
这是句子Y,后面是句子Y。在第11行和第12行中,我们看到2行被重复

您甚至不需要为此使用awk! 您可以简单地使用uniq命令

说明:

uniq是一个非常方便的命令,它可以打印文件中的连续重复项,对它们进行计数等。在这里,我使用选项-d来打印重复的连续行

奖金:

如果要添加找到重复项的行,则可以使用以下命令:

$ cat -n sentences.txt
     1  This is sentence Y
     2  This is sentence X
     3  This is sentence Y
     4  This is sentence X
     5  This is sentence Y
     6  This is sentence X
     7  This is sentence Y
     8  This is sentence X
     9  This is sentence X
    10  This is sentence Y
    11  This is sentence Y
$ cat -n sentences.txt | uniq -f1 -d
     8  This is sentence X
    10  This is sentence Y
其中-f1用于忽略行编号的第一个字段

最后但并非最不重要的一点是,如果要打印所有副本,请使用-D选项


您甚至不需要为此使用awk! 您可以简单地使用uniq命令

说明:

uniq是一个非常方便的命令,它可以打印文件中的连续重复项,对它们进行计数等。在这里,我使用选项-d来打印重复的连续行

奖金:

如果要添加找到重复项的行,则可以使用以下命令:

$ cat -n sentences.txt
     1  This is sentence Y
     2  This is sentence X
     3  This is sentence Y
     4  This is sentence X
     5  This is sentence Y
     6  This is sentence X
     7  This is sentence Y
     8  This is sentence X
     9  This is sentence X
    10  This is sentence Y
    11  This is sentence Y
$ cat -n sentences.txt | uniq -f1 -d
     8  This is sentence X
    10  This is sentence Y
其中-f1用于忽略行编号的第一个字段

最后但并非最不重要的一点是,如果要打印所有副本,请使用-D选项

救命啊

$ awk 'p==$0{print NR, $0} {p=$0}' file
将打印具有行号的重复行

12 This is sentence X
14 This is sentence Y
$ awk 'p==$0; {p=$0}' file
如果你不需要行号

12 This is sentence X
14 This is sentence Y
$ awk 'p==$0; {p=$0}' file
够了

另一种吸引注意力的方法

$ awk 'p==$0{printf "%s", "==DUP==> "} 1; {p=$0}'


This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
==DUP==> This is sentence X
This is sentence Y
==DUP==> This is sentence Y
救命啊

$ awk 'p==$0{print NR, $0} {p=$0}' file
将打印具有行号的重复行

12 This is sentence X
14 This is sentence Y
$ awk 'p==$0; {p=$0}' file
如果你不需要行号

12 This is sentence X
14 This is sentence Y
$ awk 'p==$0; {p=$0}' file
够了

另一种吸引注意力的方法

$ awk 'p==$0{printf "%s", "==DUP==> "} 1; {p=$0}'


This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
This is sentence Y
This is sentence X
==DUP==> This is sentence X
This is sentence Y
==DUP==> This is sentence Y
使用一些小脚本使用一些小脚本