admin管理员组文章数量:1123230
I have a text file with the following output.
good,bad,ugly
good,good,ugly
good,good,good,bad,ugly
good,bad,bad
bad,bad,bad,bad,good
bad,ugly,good
bad,good,bad
good,good,good,good,bad
ugly,bad,good
bad,bad,bad,good,ugly
I only want to list lines that have a single occurance of ugly and bad. Any line with multiple bads need to be excluded.
I've tried to use the following but it's still listing lines with multiple bads. grep -E "bad|ugly" file.txt | grep -v "('bad').*\1"
I have a text file with the following output.
good,bad,ugly
good,good,ugly
good,good,good,bad,ugly
good,bad,bad
bad,bad,bad,bad,good
bad,ugly,good
bad,good,bad
good,good,good,good,bad
ugly,bad,good
bad,bad,bad,good,ugly
I only want to list lines that have a single occurance of ugly and bad. Any line with multiple bads need to be excluded.
I've tried to use the following but it's still listing lines with multiple bads. grep -E "bad|ugly" file.txt | grep -v "('bad').*\1"
Share Improve this question edited 8 hours ago Paolo 25.8k8 gold badges48 silver badges85 bronze badges asked 8 hours ago Chase BrandChase Brand 1 New contributor Chase Brand is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2 |2 Answers
Reset to default 2Your current approach using grep -E "bad|ugly" matches any line with either "bad" OR "ugly", and the back-reference attempt isn't quite working.
grep -E 'bad.*ugly|ugly.*bad' file.txt | grep -v 'bad.*bad'
This will give you:
good,bad,ugly
good,good,ugly,bad
ugly,bad,good
You have to use -P
(for Perl-compatible regular expressions) for back-references.
grep -E "bad|ugly" file.txt | grep -Pv "(bad).*\1"
本文标签: linuxHow to exclude lines with duplicate strings using grepStack Overflow
版权声明:本文标题:linux - How to exclude lines with duplicate strings using grep - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736556590a1944586.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
grep
is the wrong tool. By insisting on usinggrep
, you restrict the value of the answers you will get. – William Pursell Commented 8 hours ago