0

Suppose I have a txt file that looks like the following:

pattern 1 
pattern 2
pattern 3
some information
pattern 1
pattern 2
pattern 3
some other information
.....

Is there a way to match pattern 1, pattern 2, and pattern 3 (which are in different lines) at the same time? The file contains other stuff. It has stuff at the beginning and the end that I don't want. I just want to extract the part of the file described as above.

2
  • Your question is unclear. Are you looking to match to find out if the lines are present, or extract the 'some information' that follows the three line pattern. The answer is yes it is possible, but the method will differ depending upon what you are looking to achieve. Commented Oct 1, 2013 at 23:25
  • The file contains other stuff. It has stuff at the beginning and the end that I don't want. I just want to extract the part of the file described as above. Sorry for being unclear. Commented Oct 1, 2013 at 23:28

3 Answers 3

1

consider using | to match multiple patterns at the same time

/pattern1|pattern2|pattern3/
Sign up to request clarification or add additional context in comments.

Comments

0

You can egrep with () for grouping pattern and the numbers.

egrep "(pattern 1|pattern 2|pattern 3)" file

pattern 1 
pattern 2
pattern 3
pattern 1
pattern 2
pattern 3

Comments

0
grep 'pattern 1\|pattern 2\|pattern 3' /your/file.txt

You need to escape the vertical-bar between patterns but that will print any line which matches any of those three patterns.

If you only want to match if those three patterns occur in that particular order on different lines, you have a slightly different (and more complicated) problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.