Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

linux - grep, howto combine regular and invert match when invert pattern is part of regular pattern

I'm facing the task of extracting all records that either contains the string "no issue" or does not contain the string "issue" and wonder if this can be achieved with a single call to grep. I know that this can be done using two calls, e.g.

grep "no issue" FILE > NEWFILE
grep -v "issue" FILE >> NEWFILE

However that will mess up the original order of the records so not an option as the order is of importance.

question from:https://stackoverflow.com/questions/66061528/grep-howto-combine-regular-and-invert-match-when-invert-pattern-is-part-of-regu

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

1 Answer

0 votes
by (71.8m points)

Using gnu grep:

grep -Pv '(?<!no )issue' file

RegEx Details:

  • (?<!no ): Negative lookbehind that asserts a failure if we have word no followed by a space before current position
  • issue: Match word issue

Or using gnu awk:

awk '/<no issue>/ || !/<issue>/' file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...