Egrep replacement for Or operator

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Egrep replacement for Or operator
# 1  
Old 07-17-2017
Egrep replacement for Or operator

I'm wondering if there is a replacement for using the | operator for an egrep expression for example say I wanted to match this pattern "out" or "outside" how can that be done without doing egrep "(out|side)" file
# 2  
Old 07-17-2017
You can use
Code:
grep -e 'foo' -e 'bar' file



---
out and outside may be tricky because out will also find matches for outside, so you would need to use something extra in the search to make a distinction..
# 3  
Old 07-17-2017
Code:
grep -F -e 'out' -e 'outside'  somefile

Fixed strings (-F) eliminates problems with characters like * or [ that can be taken as part of a regular expression. -F turns off regex interpretations and searches so you find exactly what you type inside the single quotes.

PS - the pipe character performs alternation (sort of like an or) and only works in regex mode. So
Code:
grep '\b(out|outside)\b' somefile

would be how to correctly use alternation for those two whole words.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 07-18-2017
Note that GNU grep uses the glibc RE engine that has got some perl extensions (PRE). \b is such an addition.
GNU grep needs an (also nonstandard) \| for OR while | is standard for ERE (egrep or grep -E).
So you can write either
Code:
grep -E '\b(out|outside)\b' somefile

or
Code:
grep '\b\(out\|outside\)\b' somefile

Both need GNU grep and glibc.
A bit more spread (but also no standard) are \< and \> (left and right word boundary) for grep (not egrep).
Standard (portable) are
Code:
grep -w -e "out" -e "outside"

Code:
grep -w "out
outside"

(multi-line)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test operator

In my script (currently running on Solaris ) I'm testing for zero size with wild character. There are mutilple files exist in the directory. if then filename=`ls -1tr ${fileformat}.${date}.? | tail -1` else ${BATCH_FATAL:-echo} "$0:ERROR:No file found ${source}/${fileformat}.${date}.?"... (5 Replies)
Discussion started by: gauravgoel83
5 Replies

2. UNIX for Dummies Questions & Answers

+= operator

im new to bash scripting and im just using online tutorials and trial and error. i wanted to write a script to read numbers from a file and find their sum: #!/bin/bash theSum=0 for line in $(cat numbers.txt) do let "theSum = theSum + $line" echo "$line" done echo "The sum is... (3 Replies)
Discussion started by: astrolux444
3 Replies

3. UNIX for Dummies Questions & Answers

su with << operator

All, THe below is my script , when i use this i am getting nothing . could any one help me to know what is the use of the << operator below su - $8 << supo echo "exportsph $2 $1 $3 $4" exportsph $2 $1 $3 $4 supo i also tried as individual command su - userid << supo , when i do... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

4. Shell Programming and Scripting

op of logical operator

Why the op of the following code is like this ???? i=4 j=-1 k=0 echo $? echo $? echo $? (5 Replies)
Discussion started by: lipun4u
5 Replies

5. Programming

new operator

Hi, Please clear the 2 questions, 2 Questions, 1) Why the new as a operator? Is there any special reason why it can't be a function like malloc? 2) How are we considering sizeof(),new are as a opearartors? I know + - * / -> , . etc.. are operators, which criteria satisfied by sizeof()... (4 Replies)
Discussion started by: Nagapandi
4 Replies

6. Shell Programming and Scripting

TEST operator help

Hi I want to group like this but syntactic is not right ... Thanks if Like this below does not work properly .. if then : else usage exit 1 fi (5 Replies)
Discussion started by: zam
5 Replies

7. UNIX for Dummies Questions & Answers

search ")" with egrep - egrep: syntax error

Hi Guys, we have a shell script which basically query the Database which retrieves huge data and use the data with "egrep" . Now there is some data which contains characters like "abc)" and the same is used like below : "egrep (.+\|GDPRAB16\|GDPR/11702 96 abc)\|$ temp.txt" now while... (7 Replies)
Discussion started by: sagarjani
7 Replies

8. UNIX for Dummies Questions & Answers

Egrep cheat sheet anywhere? Looking for meaning of egrep -c

Hi I've been searching google and have not found what egrep -c means. Does anyone know where I can get a cheat sheet or what that -c means? thanks, Linda (2 Replies)
Discussion started by: leelm
2 Replies

9. HP-UX

Or operator with if

hi, i was trying to club to test condition with if. if -o ; then it is giving me error message, i wanted to ask how can we check two condtions with one if. (1 Reply)
Discussion started by: babom
1 Replies

10. Shell Programming and Scripting

And operator

I am trying to check two variables and if both are blank I want to set a flag: the_f3_pid=`rsh $target ps -ef | grep "f3.eab" | awk '{print $2}'` the_f7_pid=`rsh $target ps -ef | grep "f7.eab" | awk '{print $2}'` if ; then y=1 fi I get an error: ./script_name: test: 0403-021 ]... (4 Replies)
Discussion started by: rcarnesiii
4 Replies
Login or Register to Ask a Question