2016-11-30 20 views
1

我想使用egrep從文件執行多字符串搜索,但使用變量來存儲搜索字符串值。這將幫助我以編程方式更改搜索字符串。例如: -如何在egrep中使用varible作爲搜索字符串

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

input.file內容:

*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內容(預計):

*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,* 

請幫

回答

2

雙引號的變量,也可以使用grep -E超過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, 
相關問題