Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
Source Link

How can i grep for multiple patterns, and print them on the same line?

I have a bunch of files that all follow the same pattern of data.

Let's say that this is the pattern i want to extract from:

First part of text...patternA......Second part of text.....patternB.....Third part of text....patternC.....End part of text

Currently i am using this:

grep -P -o ".{0,5}patternA|.{0,5}patternB.{0,5}|patternC.{0,5}" filename.txt

With this the output i am getting is :

1111 patternA 2222 patternB 2222 patternC 3333 

The output i actually want is :

1111 patternA 2222 patternB 2222 patternC 3333

I can't seem to figure out how to get rid of the newlines at the end of each pattern.

How can i accomplish this?