Egrep help needed?

Can anyone advise how to use grep to locate lines containing these patterns?
i) SIGSTOP or SIGTSIP
ii) SIGTTIN or SIGTTOU
iii) harris or harrison

Say the file contents are
$ cat reg_exp
SIGSTOP
SIGTSIP
SIGTTIN
SIGTTOU
harris
harrison

I tried using, but did not worked
$ grep -E 'SIG[STOP|TSIP]' reg_exp
SIGSTOP
SIGTSIP
SIGTTIN
SIGTTOU

Thanks in advance,

grep  "^[SIG]\{3\}\(STOP\|TSIP\)" reg_exp

OR

grep  "^SIG\(STOP\|TSIP\)" reg_exp

Could you please advise why you have used \ in following?

grep  "^SIG\(STOP\|TSIP\)" reg_exp

Thanks in advance,

---------- Post updated at 11:30 AM ---------- Previous update was at 11:24 AM ----------

Tried using -E and it worked...
$ grep -E 'harris(|on)' reg_exp
harris
harrison

From grep manual page:

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the back-slashed versions \?, \+, \{, \|, \(, and \).

Also used egrep for other pattern:

$ grep -E 'SIG(STOP|TSIP)' reg_exp
SIGSTOP
SIGTSIP

Yes.

-E option interprets PATTERN as an extended regular expression. So you don't have to use the back-slashed versions. Since I did not use this option I had to use back-slashed versions.

I hope you understood.

Yes, I am clear.

Thanks for you help and time :slight_smile:

It isn't clear to me whether tinku91 wants one expression that will match all six words or three different patterns each matching two words. But the following script should give you what you want:

echo 'i, ii, and iii'
grep -E '(harris|SIG(STOP|TSIP|TTIN|TTOU))' reg_exp

echo 'i'
grep -E 'SIG(STOP|TSIP)' reg_exp

echo 'ii'
grep -E 'SIG(TTIN|TTOU)' reg_exp

echo 'iii'
grep -E 'harris' reg_exp

I suspect that SIGTSIP is a typo and that what is really wanted is SIGTSTP, but this script looks for what was requested. Note also that any line that contains harrison also contains harris so there is no need to check for both harris and harrison .

Note to bipinajith:

grep  "^[SIG]\{3\}\(STOP\|TSIP\)" reg_exp

is not portable; many systems don't allow backslash escaped ERE syntax in BREs. The request didn't say anything about only matching at the start of a line. And, [SIG]{3} in an ERE will match GGG, III, SSS, GGi, GGS, etc. in additional to SIG.

2 Likes

Don, I understood. That repetition operator {3} was a mistake. Thank you for pointing that out.