23

Consider I have the following stream of data:

BODY1
attrib1:  someval11
attrib2:  someval12
attrib3:  someval13

BODY2
attrib1:  someval21
attrib2:  someval22
attrib3:  someval23

BODY3
attrib1:  someval31
attrib2:  someval32
attrib3:  someval33

I want to extract only attrib1 and attrib3 for each BODY, i.e.

attrib1:  someval11
attrib3:  someval13
attrib1:  someval21
attrib3:  someval23
attrib1:  someval31
attrib3:  someval33

I tried

grep 'attrib1\|attrib3', according to this site but that returned nothing. grep attrib1 and grep attrib2 do return data but just for the single pattern specified.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • Not that it solves your problem, but it may be easier to follow and faster for grep to find if your regex is `attrib[13]`. Making the common part longer is usually better. – Andy Lester Nov 28 '12 at 17:17
  • what if there was no commong pattern between attrib 1 and 3? if they were named something completelty different? – amphibient Nov 28 '12 at 17:18
  • in the real example, my attrib1 is called foo and attrib3 is called bar... – amphibient Nov 28 '12 at 17:19
  • Then the `attrib[13]` trick won't work. – Andy Lester Nov 28 '12 at 18:04
  • I don't understand -- does it **have** to be grep? axiom showed how to do it, but why not just use awk? Am I the only one who finds it simpler for this kind of tasks? – loreb Nov 29 '12 at 21:38
  • i would assume because searching for a single pattern would most easily be done in `grep` that so would searching for multiple ones as a matter of natural progression. – amphibient Nov 29 '12 at 23:15
  • @amphibient If your question was answered, please accept one of the answers. – axiom Feb 03 '17 at 22:25

5 Answers5

29

grep -e 'attrib1' -e 'attrib3' file

From the man page :

-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (-e is specified by POSIX.)

Edit : Alternatively , you can save patterns in a file and use the -f option :

aman@aman-VPCEB14EN:~$ cat>patt
attrib1
attrib3

aman@aman-VPCEB14EN:~$ grep -f patt test
attrib1:  someval11
attrib3:  someval13
attrib1:  someval21
attrib3:  someval23
attrib1:  someval31
attrib3:  someval33
axiom
  • 8,765
  • 3
  • 36
  • 38
23

Very simple command:

 bash> grep  "attrib1\|attrib3" <file.name>
attrib1:  someval11
attrib3:  someval13
attrib1:  someval21
attrib3:  someval23
attrib1:  someval31
attrib3:  someval33
tripleee
  • 175,061
  • 34
  • 275
  • 318
SteveScm
  • 545
  • 1
  • 7
  • 15
19

Also egrep;

egrep "pattern1|pattern2|pattern3" file
Mike Makuch
  • 1,788
  • 12
  • 15
1

This works, with GNU grep 2.6.3

grep "attrib[13]"

or

 grep "^[^0-9]*[13]:"
Sirch
  • 407
  • 3
  • 17
  • what if there was no commong pattern between attrib 1 and 3? if they were named something completelty different? – amphibient Nov 28 '12 at 17:18
  • What are you looking for, a 1 or 3 on the end of the first word of the line and followed by a colon? – Sirch Nov 28 '12 at 17:21
  • how can I do the same if I had strings with more then one character? something like: very-long-attribute-with-Jon-in-the-middle very-long-attribute-with-Tony-in-the-middle very-long-attribute-with-Richard-in-the-middle so provide something that looks like: "very-long-attribute-with-[Tony|Richard|Jon]-in-the-middle" – Charbel Jun 13 '22 at 10:28
0

It depends on the shell you are into. grep -iw 'patter1\|patter2\|pattern3' works on bash shell where as it doesn't work on korn shell. For korn shell we might have to try grep -e pattern1 -e patter2 and so on.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Siva
  • 9
  • 1