Find lines in file that contain duplicate characters

偶尔善良 提交于 2019-12-10 17:17:08

问题


I need some help in locating lines in a text file that contain duplicate characters. I prefer using bash, but any other method will do fine :)

A small example just to make things clear:
file.txt:

1234
11234
abcd
12234
ab321
1233
zs11w
12w2

the desired output:

11234
12234
1233
zs11w
12w2

Thanks for all your help!


回答1:


grep '\(.\).*\1' file.txt

Matches any line that contains a character, any string, and then that same character, i.e. any line with duplicates.




回答2:


Perl:

perl -ne 'print if /(.)\1/' file.txt

awk:

awk -F "" '{for (i=1; i<NF; i++) if ($i == $(i+1)) {print; next}}' flie.txt


来源:https://stackoverflow.com/questions/14223350/find-lines-in-file-that-contain-duplicate-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!