0

I have file.txt

A2
RP FAULT

A2
RP FAULT

A2
CELL

A2
CELL

how I can just grep 2 words: A2 & RP FAULT, The result should be :

A2
RP FAULT
A2
RP FAULT

what I try :

cat file.txt | grep -E "A2|RP FAULT"

but the result like this

A2
RP FAULT
A2
RP FAULT
A2
A2

2 Answers 2

0

Your grep -E command does what you wrote in the subject, but I guess what you really want is to show the A2 lines with the "status" if the following line is RP FAULT. Right?

cat file.txt | grep -A 1 "A2" | grep -B 1 'RP FAULT'

The -A n shows n lines after the match, the -B n shows lines before.

1
  • you're right ..thanks bro , work like a charm Jul 31, 2018 at 7:37
0

Option 1: You can try pcregrep instead of regular grep

pcregrep  -M "A2\nRP FAULT" file
  • -M used to match across multiple lines so that we can search for newlines as \n.

Option 2: with regular grep

 grep -zoP "A2\nRP FAULT" file

Output:

A2
RP FAULT
A2
RP FAULT
2
  • ups ... pcregrep: command not found... need to install first Jul 31, 2018 at 7:36
  • @AnggoroSetiawan then try option 2.
    – Siva
    Jul 31, 2018 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .