Vijesh Kk Vijesh Kk - 4 days ago 5
Bash Question

How to use varible as search string in egrep

I want perform multi-string search from a file using egrep, but use variable to store the search string values. this will help me to programmatically change the search strings. example:-

SEARCHSTRING='typesetting industry|printer took|specimen'
cat /tmp/input.file|egrep $SEARCHSTRING > /tmp/output.file


input.file content:

*Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the
1500s, when an unknown printer took a galley of type and scrambled it to
make a type specimen book. It has survived not only five centuries,
but also the leap into electronic*


output.file content (expected):

*Lorem Ipsum is simply dummy text of the printing and typesetting industry.
1500s, when an unknown printer took a galley of type and scrambled it to
make a type specimen book. It has survived not only five centuries,*


Please help

PS. PS.
Answer

double quote the variable, also use grep -E over egrep

egrep  "$SEARCHSTRING" inputfile
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
1500s, when an unknown printer took a galley of type and scrambled it to
make a type specimen book. It has survived not only five centuries,
Comments