John James
John James

Reputation: 31

How to explain this behavior of grep with a space?

I'm quite new in the Linux world and I wanted to use grep to find occurrences of print followed by a space. It appears in the file /path/script.py - you have to trust me here. I have been surprised to see that

grep --include=\script.py -rnwlF '/path/' -e 'print'

correctly outputs the file name, but

grep --include=\script.py -rnwlF '/path/' -e 'print '

does not. It is absolutely certain that the space exists in the file though.

What happens? Could it be some weird character encoding problem?

Upvotes: 2

Views: 57

Answers (1)

Georg
Georg

Reputation: 1098

Where did you get that line from? By using the -w option, you ask grep to only select lines that match the whole word. So removing it should output the file, if it does contain "print followed by space":

grep --include=\script.py -rnlF '/path/' -e 'print '

However, if you are only grep'ing through one file, it would be simpler to just write:

grep -lF '/path/script.py' -e 'print '

Upvotes: 1

Related Questions