3

I'm completing a command line game/challenge that involves solving a murder mystery.

One of the steps is to search for a suspect by using grep to search for multiple keywords in one file.

However that file contains thousands of lines of text that has the following format:

License Plate: L337ZR9 Make: Honda Color: Red Owner: Katie Park Height: 6'2" 

I have tried using grep in the following ways:

cat vehicles | grep -i 'L337' | grep -i 'honda' ls | grep -i 'honda\|blue\|L337' 

But as i understand it these commands will give me any result that matches any one of my three search terms.

What command do i need to search the vehicle file and display matches only match for Blue Honda with license plate of L337 - in other words what command allows grep to find and display results of multiple matching search terms?

0

2 Answers 2

2

Use GNU grep.

Example

Source

License Plate: L337ZR9 Make: Honda Color: Blue Owner: Katie Park Height: 6'2" License Plate: L338ZR9 Make: Honda Color: Black Owner: James Park Height: 6'0" 

Command

grep -Pzo '\w+:\s*L337ZR9\n\w+:\s+Honda\n\w+:\s*Blue' file 

Result

Plate: L337ZR9 Make: Honda Color: Blue 

Explanation

From grep man:

 -z, --null-data Treat the input as a set of lines, 

NOTE: grep (GNU grep) 2.20 tested

Sign up to request clarification or add additional context in comments.

Comments

1

First realize that you are grepping records, not lines.

So merge the 5 line records into a single line:

cat licenseplates | paste - - - - - 

Now it is suddenly easy to grep:

cat licenseplates | paste - - - - - | grep -i 'L337' | grep -i 'honda' | grep -i blue 

Finally you need to fold the matching lines back into 5 line records:

cat licenseplates | paste - - - - - | grep -i 'L337' | grep -i 'honda' | grep -i blue | sed 's/\t/\n/g' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.