Issue
I have variable whose value is set as AMB529\SPB_REPORTING and when i am trying to find a match with its value it is not working because of back slash. Have tried both double and single quote but no luck.
Below is example
var=AMB529\SPB_REPORTING cat text.txt | grep "$var"
It is taking value of $var as AMB529SPB_REPORTING and thus pattern match is not working.
Thanks in advance for your help.
Solution
grep
command can be used to display lines from the files: cat
is useless here.
there is a missing ;
, in order to define the var
variable in the current shell (not in the execution environment of the cat
command)
a regular expression containing a \
followed by any ordinary character will be used as if the \
character is not present (see man 7 regex
).
@Socowi was right, the value assigned to var
seems to be a filename (ordinary string) not a REGEX.
Moreover 2 '
(quote) should be used when assigning a value to var
in order to inhibit pathname expansion
Give a try to this:
var='AMB529\SPB_REPORTING' ; grep -F "${var}" text.txt
With bash builtin command read
there is the same behavior (see read --help
): the \
is removed when it is followed by an ordinary character.
-r do not allow backslashes to escape any characters
Give a try to this:
while IFS= read -r line ; do
grep -F "${line}" text.txt
# etc.
done
Answered By - Jay jargot Answer Checked By - Katrina (WPSolving Volunteer)