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

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 :)

1 Response
Add your response

5030

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

over 1 year ago ·