2016-04-01 26 views
-1

在我的Drupal安裝中,我正在搜索變量$headgrep的所有實例正在返回(絕大多數!)$head$header如何搜索使用Grep的確切字符串?

我可以將哪些參數或標誌傳遞給命令,以便grep只返回$head,而不是$header

+0

grep「$ head」是否工作? –

回答

4

您可以搜索使用-w選項確切的詞。 使用

grep -w string 
1

可以使用fgrep(或grep -F),從精細的手工:

-F 
--fixed-strings 
Interpret the pattern as a list of fixed strings 
(instead of regular expressions), separated by newlines, 
any of which is to be matched. (-F is specified by POSIX.) 
+1

這仍然會匹配'$ header'。 –

3

正如其他人所提到的,使用-wgrep。還要注意你需要使用單引號的事實;否則,值$head將被bash擴展爲這個變量的值。

grep -w '$head' file 
#  ^ ^
#  single quotes! 

您還可以使用sed

sed -n '/\$head\>/p' file 

這裏最重要的部分是用\>陳述文字操作的結束。

$ cat a 
hello $head is header blabla yea 
hello head is $header blabla yea 
$ sed -n '/\$head\>/p' a 
hello $head is header blabla yea 
相關問題