Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

regex - 如何用grep排除一个单词?(How can I exclude one word with grep?)

我需要这样的东西:

grep ^"unwanted_word"XXXXXXXX
  ask by john translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can also do it using -v (for --invert-match ) option of grep as:

(你也可以使用grep的-v (for --invert-match )选项来--invert-match :)

grep -v "unwanted_word" file | grep XXXXXXXX

grep -v "unwanted_word" file will filter the lines that have the unwanted_word and grep XXXXXXXX will list only lines with pattern XXXXXXXX .

(grep -v "unwanted_word" file将过滤具有unwanted_word的行,而grep XXXXXXXX将仅列出模式为XXXXXXXX 。)

EDIT:

(编辑:)

From your comment it looks like you want to list all lines without the unwanted_word .

(从您的评论看起来您想要列出没有unwanted_word所有行。)

In that case all you need is:

(在这种情况下,您只需要:)

grep -v 'unwanted_word' file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...