Match two words that is on the same line

The command that I have prints out two of the lines whenever FW_6.0.0 is found, below is the code:

grep -oP 'FW_6.0.0, (.*)$' file

Below is the output, both has the same value of FW_6.0.0

FW_6.0.0, SUCCESS
FW_6.0.0, OK

I would like to match two words, that is FW_6.0.0 and SUCCESS that can be found on the same line so that it prints this out:

FW_6.0.0, SUCCESS and eliminates FW_6.0.0, OK

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

try using double quotes "":

grep -oP "FW_6.0.0, SUCCESS" file

OR (Because it is a fixed string, not a pattern):

grep -oF "FW_6.0.0, SUCCESS" file

from grep man page:

-F, --fixed-strings
          Interpret  PATTERN  as  a  list  of  fixed strings, separated by
          newlines, any of which is to be matched.  (-F  is  specified  by
          POSIX.)
-P, --perl-regexp
          Interpret PATTERN as a Perl regular expression.  This is  highly
          experimental and grep -P may warn of unimplemented features.

Method 2

If you want to use awk:

awk '/FW_6.0.0/ && /SUCCESS/' file

Method 3

Try:

grep -o 'FW_6.0.0.*SUCCESS' file

We don’t need -P option here.

Method 4

Through sed,

$ sed -n '/FW_6.0.0.*SUCCESS/p' file
FW_6.0.0, SUCCESS


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x