25

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.

7
  • 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. Commented Nov 28, 2012 at 17:17
  • what if there was no commong pattern between attrib 1 and 3? if they were named something completelty different? Commented Nov 28, 2012 at 17:18
  • in the real example, my attrib1 is called foo and attrib3 is called bar... Commented Nov 28, 2012 at 17:19
  • Then the attrib[13] trick won't work. Commented Nov 28, 2012 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? Commented Nov 29, 2012 at 21:38

6 Answers 6

36

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
Sign up to request clarification or add additional context in comments.

2 Comments

grep: illegal option -- e. i am using SunOS
@foampile You may try and see if -f option is of any use. Also note that for your example, grep 'attrib1\|attrib3' should also work fine.
23

Very simple command:

 bash> grep  "attrib1\|attrib3" <file.name>
attrib1:  someval11
attrib3:  someval13
attrib1:  someval21
attrib3:  someval23
attrib1:  someval31
attrib3:  someval33

Comments

21

Also egrep;

egrep "pattern1|pattern2|pattern3" file

1 Comment

Or grep -E "pattern1|pattern2|pattern3" file
1

This works, with GNU grep 2.6.3

grep "attrib[13]"

or

 grep "^[^0-9]*[13]:"

3 Comments

what if there was no commong pattern between attrib 1 and 3? if they were named something completelty different?
What are you looking for, a 1 or 3 on the end of the first word of the line and followed by a colon?
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"
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.

1 Comment

Which shell you use has no direct bearing on what parameters the grep external command supports.
0

Also, if there are multiple patterns be sure to put e option as the last options. For example,

grep -iwe 'the' -e 'that' -e 'then' -e 'those' text

OR

grep -iwe 'the' -iwe 'that' -e 'then' -e 'those' text OR grep -wie 'the' -wie 'that' -wie 'then' -wie 'those' text

works! But, if you put e first, like

grep -eiw 'the' -e 'that' -e 'then' -e 'those' text

it returns an error.

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.