Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping slash "\" in grep

I have file with line:

"H:\Check\WP_20140511_029.mp4"

along with other lines. I want to remove such lines indicating directory at H:\Check. I tried

grep -v ".*H:\\Check.*" testout.txt > testout2.txt

But it did not delete those lines. Whats wrong with my regex .*H:\\Check.*. regex101 shows that my regex correctly matches the line.

like image 344
Mahesha999 Avatar asked May 17 '17 20:05

Mahesha999


1 Answers

You can use:

grep -v 'H:\\Check' testout.txt > testout2.txt

It is important to use single quotes to avoid excessive escaping of backslash.

Using double quotes equivalent command will be this:

grep -v "H:\\\Check" testout.txt > testout2.txt

EDIT:

\\ in double quotes is equivalent of a single backward slash due to shell expansion that happens in double quotes only. It is evident from these echo commands:

echo "H:\\Check"
H:\Check

echo 'H:\\Check'
H:\\Check
like image 116
anubhava Avatar answered Oct 05 '22 23:10

anubhava