7abkrg
Last Updated: February 25, 2016
·
2.6K
· vitorbaptista
E87dd32c43e1b267f6456f76c014a8f3

Greping a text file that's detected as binary

I've downloaded a csv file created in Windows and encoded in UTF-16LE. I tried to grep through it, only to see that grep didn't work. At first I thought it was because it was UTF-16 and my locale is UTF-8. So I converted it, using

iconv -f utf-16le -t utf-8 weird-file.csv > not-so-weird-file.csv

But it still didn't work. There were some funny characters that made it be recognized as binary

$ file -i not-so-weird-file.csv 
not-so-weird-file.csv: application/octet-stream; charset=binary

So grep treats it as binary and don't gives me what I want. To force it to treat it as text, you simply need to use the --text parameter, as:

$ grep --text -c "ATIVO PERMANENTE" not-so-weird-file.csv
546224

Now, back to hacking :)

Say Thanks
Respond

1 Response
Add your response

5030
43ea08b4fc547e8a5da87c207bea2123

The --text switch with grep, I'll add it to my grep file

over 1 year ago ·
Filed Under