4

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

  • Is the order matter? – cuonglm Jul 4 '14 at 3:27
  • Yes FW_6.0.0 should come first. – user3081935 Jul 4 '14 at 3:28
6

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.
  • 1
    I don't see the regex there, let alone the PCRE. So why grep -P instead of grep -F? – Hauke Laging Jul 4 '14 at 3:37
  • @HaukeLaging, updated – Networker Jul 4 '14 at 3:42
5

If you want to use awk:

awk '/FW_6\.0\.0/ && /SUCCESS/' file
  • you have to escape the dots, otherwise it matches FW_6f0f0 also. – Avinash Raj Jul 4 '14 at 6:00
4

Try:

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

We don't need -P option here.

2

Through sed,

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.