I am trying to grep
a string from a file but grep returns nothing (even though the string is present in the file). It turned out that the file starts with a ÿþ
mark. If I remove it manually then grep works. How do I make grep work without manually removing the BOM?
Asked
Active
Viewed 1,238 times
2

Signal15
- 558
- 5
- 16

RegedUser00x
- 2,313
- 5
- 27
- 34
-
3Post an example of the file and the `grep` command, please. – fedorqui Oct 15 '13 at 12:13
-
The presence of the BOM sounds like an error; I'm not sure why removing it *isn't* the solution. – chepner Oct 15 '13 at 12:59
-
@anubhava - That questioner was asking how to find files with a BOM, not how to find text within such files. – Benj Oct 22 '13 at 11:40
-
@Benj: That's not the sense I get from OP's first statement `I am trying to grep a string from a file but grep returns nothing`. – anubhava Oct 22 '13 at 12:31
-
@anubhava - I'm not referring to this question, I'm referring to the one you said it was a dupe of. – Benj Oct 23 '13 at 15:08
-
@Benj: Accepted answer there has `sed '1s/^\xEF\xBB\xBF//'` which should be good enough to tell OP how to `grep` – anubhava Oct 23 '13 at 15:20
2 Answers
1
What about:
strings <file> | grep <pattern>
Alternatively check the man page of your grep
command. What's actually happening is that grep
is looking at the first few bytes of your file and deciding that it's a binary file and therefore not searchable. You can override this with:
--binary-files=text

Benj
- 31,668
- 17
- 78
- 127
-
1Caveat: Files may be coded in UTF-16 (-received from Windows system?). Then grep (and other methods) will fail on multi-character ascii patterns even if the BOM is removed. – rhoerbe Oct 27 '17 at 09:11
1
You can also use cat with the -v (visible) option:
cat -v file | grep pattern

Cole Tierney
- 9,571
- 1
- 27
- 35