如何从一行中排除指定字符?

时间:2018-10-24 12:31:56

标签: regex sed

我正在过滤具有数字和名称的文本文件,我拥有以下命令

sed -En 'h;:a;s/^(.)\1+//;ta;/^$/{x;p}' 

我试图编辑此命令并搜索了如何操作,但没有得到所需的结果。

如何将一行中的第一个或第二个字符排除在连续重复之外? 如果输入如下:

1122
133555
1366622
256651155
311144

输出应为:

133555 (only the first character is not consecutively repeated)
1366622 (only the first and the second characters are not consecutively repeated).
311144 (only the first or the second character is not consecutively repeated)

上面的命令是只打印连续两次或多次重复的行。

1 个答案:

答案 0 :(得分:0)

我不确定我是否确切了解您要做什么,但这将为您提供示例数据的预期结果。

/^(.)\1/  # matches lines starting with two identical characters
!{...}    # run this block when the preceding pattern does not match
/^(.|..)((.)\3+)+$/p   # consecutive duplicates (but first/second char doesn't have to belong to sequence    

说明

Foo