grep 'fookey=>'
doesn't return any matches because this regex is not matched. Your example shows a record with single quotes around fookey
and a space before the =>
.
Also, you want to lose the useless use of cat
.
Because your regex contains literal single quotes, we instead use double quotes to protect the regex from the shell.
grep "'fookey' =>" file.php
If your goal is to extract the value inside single quotes after the =>
the simple standard solution is to use sed
instead of grep
. On a matching line, replace the surrounding text with nothing before printing the line.
sed "/.*'fookey' => '/!d;s///;s/'.*//" file.php
In some more detail,
/.*'fookey' => '/!d
skips any lines which do not match this regex;
s///
replaces the matched regex (which is implied when you pass in an empty regex) with nothing;
s/'.*//
replaces everything after the remaining single quote with nothing;
- and then
sed
prints the resulting line (because that's what it always does)
If you get "event not found" errors, you want to set +H
or (in the very unlikely event that you really want to use Csh history expansion) figure out how to escape the !
; see also echo "#!" fails -- "event not found"
Other than that, we are lucky that the script doesn't contain any characters which are special within double quotes; generally speaking, single quotes are much safer because they really preserve the text between them verbatim, whereas double quotes in the shell are weaker (you have to separately escape any dollar signs, backquotes, or backslashes).