Issue
This the following
$ echo '
- key: val
baz: foo
' | grep -F "$(
cat <<EOM
- key: val
foo: baz
EOM
)"
The output is a partial match
- key: val
but I would like to get nothing if the whole given heredoc string is fully found and nothing otherwise.
Is it possible to grep for full raw multi-line string (not a regular expression)? If so, then how should it be done?
Solution
First thing is that to get exact match in grep
you will need to use -x
option.
However, as I suspected initially grep -x
will fail for even the full match due to presence of line breaks.
One trick is to use tr
to strip all line breaks from original and pattern strings and use -x
like this:
echo '
- key: val
baz: foo
' | tr -d '\n' | grep -q -xF -- "$(
cat <<EOM | tr -d '\n'
- key: val
foo: baz
EOM
)" && echo "match" || echo "nope"
echo '
- key: val
baz: foo
' | tr -d '\n' | grep -q -xF -- "$(
cat <<EOM | tr -d '\n'
- key: val
baz: foo
EOM
)" && echo "match" || echo "nope"
Output:
nope
match
Note use of -q
to suppress normal output of grep
and use only the exit code. Also used --
to separate options from pattern.
Answered By - anubhava Answer Checked By - David Marino (WPSolving Volunteer)