Hershizer33
Hershizer33

Reputation: 1246

grep command on multiple lines (not multiline grep)

Trying to figure out how to write a command across multiple lines because the real one is way to long for a prompt (4200 chars), so I tried this example:

Made a test file that contained the following 3 lines:

some thing
some
thing

When I do this grep:
grep "some thing" test

I get the expected result of:
some thing

But when I do this grep:
grep "some \
thing" test

I get the unexpected result of:
some thing
thing

Almost as if it ran the grep twice, once for "some " and once for "thing". Is there any way to properly use the \ to combine the 2 to where the result is like the first grep?

Upvotes: 0

Views: 1554

Answers (3)

JJoao
JJoao

Reputation: 5337

Using -zPo you can make grep behave multilingual:

grep -zPo 'some\nthing' test

Upvotes: 1

Hershizer33
Hershizer33

Reputation: 1246

So it looks like this did it:
grep "some \
|thing" test.txt

Produced: some thing

Same as: grep "some thing" test.txt

Upvotes: 0

Jacek Sokolowski
Jacek Sokolowski

Reputation: 597

Instead of grep, you have to use pcregrep, then you will be able to use new lines in pattern by -M option.

In your example it will be like:

pcregrep -M 'some\nthing' test

Upvotes: 3

Related Questions