Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep does not print filename of result for some files with special characters

Tags:

grep

unix

macos

I have this particular file with special characters:

​
^S^H^B^R^MLong is here
^X^M

When I search for it via grep -r "Long is here" . it will give me the following as result, not revealing its file path:

Long is here

However, if I remove the ^S^H^B^R^M, the file path will show up:

./CIQla:Long is here​

I'm using OSX.

What's going on here?


EDIT:

I tried it with both BSD grep (grep 2.5.1-FreeBSD) as GNU grep (ggrep 2.26)

like image 901
Long Hoang Avatar asked Nov 16 '25 11:11

Long Hoang


1 Answers

The ^M is a carriage return, which makes the terminal return to the beginning of the line. Essentially, it prints "./CIQla:", then several nonprinting characters, then ^M sends it to the beginning of the line, then it prints "Long is here" over the "./CIQla:".

If you want to be able to see the filename in the output, you need to pipe the result through something that'll make the ^M (and probably the other nonprinting characters) into something printable. Try these:

grep -r "Long is here" . | cat -v
grep -r "Long is here" . | more
like image 73
Gordon Davisson Avatar answered Nov 19 '25 08:11

Gordon Davisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!