在我的Drupal安裝中,我正在搜索變量$head
和grep
的所有實例正在返回(絕大多數!)$head
和$header
。如何搜索使用Grep的確切字符串?
我可以將哪些參數或標誌傳遞給命令,以便grep只返回$head
,而不是$header
?
在我的Drupal安裝中,我正在搜索變量$head
和grep
的所有實例正在返回(絕大多數!)$head
和$header
。如何搜索使用Grep的確切字符串?
我可以將哪些參數或標誌傳遞給命令,以便grep只返回$head
,而不是$header
?
您可以搜索使用-w選項確切的詞。 使用
grep -w string
可以使用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.)
這仍然會匹配'$ header'。 –
正如其他人所提到的,使用-w
爲grep
。還要注意你需要使用單引號的事實;否則,值$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
grep「$ head」是否工作? –