連続して繰り返されるすべての文字を含む行のみを印刷する

連続して繰り返されるすべての文字を含む行のみを印刷する

テキストファイルがあり、1文字が連続して複数回繰り返されるすべての行を印刷したいと思います。たとえば、次のように入力します。

11
AAA
555227777
BBhh@@222
baabbb
1112
212211
baa
22333445
322113

 出力は次のようになります。

11
AAA 
555227777
BBhh@@222

 この4行には連続して繰り返される文字のみが含まれているため、出力には4行だけが含まれます。

私はこのコードを試しました

grep '\(^\| \)\([   ])\2\1\($\| \)' INFILE

しかし、それは正しく動作しません。

ベストアンサー1

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

コメントがあります

sed -E -n '
    h            # store a copy of the line
    :a           # set label "a"
    s/^(.)\1+//  # from the start of the line, remove sequences of 2 or more repeated chars
    ta           # *if the pattern matched* jump to "a"
    /^$/ {       # if empty string:
        x        #   retrieve the original line
        p        #   and print it
    }
' file

おすすめ記事