I've been trying for about the past 30 minutes to get this to work properly. grep is not exactly the most difficult thing to use, so I'm somewhat baffled as to why this won't work.

The files I'm trying to use grep on are simple XHTML log files. Their names are in the format name@domain.com.html, though I don't think that should matter, and inside is simple XHTML.

I copied one such log file to be testfile so you can see the output of some commands and why it's baffling to me:

[~/.chatlogs_windows/dec] > whoami
[~/.chatlogs_windows/dec] > type grep
grep is /bin/grep
[~/.chatlogs_windows/dec] > uname -a
Linux reid-pc 2.6.35-22-generic #33-Ubuntu SMP Sun Sep 19 20:32:27 UTC 2010 x86_64 GNU/Linux
[~/.chatlogs_windows/dec] > cat /etc/issue
Linux Mint 10 Julia
[~/.chatlogs_windows/dec] > ls -lh testfile
-rw-r--r-- 1 reid reid  63K 2011-01-10 12:45 testfile
[~/.chatlogs_windows/dec] > tail -3 testfile 
[~/.chatlogs_windows/dec] > file testfile
testfile: XML document text
[~/.chatlogs_windows/dec] > grep html testfile 
[~/.chatlogs_windows/dec] > grep body testfile 
[~/.chatlogs_windows/dec] > grep "</html>" testfile 
[~/.chatlogs_windows/dec] > grep "</body>" testfile
[~/.chatlogs_windows/dec] > cat testfile | grep html
[~/.chatlogs_windows/dec] > cat testfile | wc -l
[~/.chatlogs_windows/dec] > cat testfile | tail -3
[~/.chatlogs_windows/dec] > chmod a+rw testfile && ls -lh | grep testfile
-rw-rw-rw- 1 reid reid  63K 2011-01-10 12:45 testfile
[~/.chatlogs_windows/dec] > grep html testfile

That's what I'm attempting to do. I want to just use grep -ri query . in ~/.chatlogs_windows, which normally works perfectly for me... but for some reason, it completely fails at going through these files.

If it matters, I copied these files off of my Windows 7 partition. But I chown'd them and gave myself all the appropriate permissions, and other programs (like cat) seem to read them just fine. I also copied testfile to testfile_unix and converted the line endings and tried that, but it didn't work either.

I'm using zsh, but I tried it on bash and that failed too. Also, grep works normally: I tried it out on my documents folder and it worked flawlessly.

If you need any more information, just let me know. I tried googling around, but I found no reason for grep to simply not work. Thanks in advance.

  • In bash, after you execute one of those failing grep calls, show the exit status: echo $? (aside: ls -Alh | grep testfile is more easily written/typed as ls -lh testfile) – Doug Harris Jan 10 '11 at 19:16
  • it doesn't make sense to me either but just to make sure: did you try: cat testfile | grep "html"? – yasouser Jan 10 '11 at 19:24
  • It gives "1" back as a result. I didn't even think about that. But according to grep's man page, unless I'm misreading it, 1 is the result when the lines aren't found (no matches). – Reid Jan 10 '11 at 19:26
  • @anand.arumug: Yes, I tried that and all sorts of variations. No matter what I do, nothing is ever given back. – Reid Jan 10 '11 at 19:26
  • Try creating a new file (touch ~/test.txt) and copy/paste the contents of testfile. And try grepping. See what happens. – yasouser Jan 10 '11 at 19:40

The grep tool doesn't recognise the UTF-16 file encoding.

  • 2
    Genius! For anyone else who may have this issue in the future, I used the command iconv -f UTF-16 -t UTF-8 testfile > testfile_enc to convert it and tested from there. It'd be nice if grep would inform you of that, though, instead of silently failing. – Reid Jan 10 '11 at 19:49
  • Great! learnt something interesting :) Thanks to @Reid and @RedGrittyBrick. – yasouser Jan 10 '11 at 20:16
  • Grep and Windows files have stolen a tiny bit more of my soul. Thanks @Reid – Will May 17 '16 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.