1

GNU grep 2.6.3
Linux version 2.6.32-220.el6.i686 (Red Hat 4.4.6-3)

I have a .dat file that grep will not function on. It's plain text and can be read by all standard output commands, e.g. "less", "cat", etc, so I know what it contains.

If I grep a phrase I absolutely know is in there it does not return, as if it found nothing.

The oddity is if I simply rename it with a different extension, like file.dat.txt, now grep will work just fine. If I rename it back to file.dat, once again grep will not find anything.

I also created a .dat file simply with the echo command:
echo "grep this" > test.dat
And still, grep "grep" test.dat returned nothing.
But after mv test.dat test.dat.txt, now grep "grep" test.dat.txt worked just fine.

Easy enough workaround (rename the file) but just thought this was odd behavior.

http://imgur.com/yepLMWU

  • 1
    What is the output of command -v grep? – Fox Apr 25 '17 at 16:46
4

It is not normal behavior and I cannot reproduce the issue on my ubuntu nor centos machines.

It is very likely that GREP_OPTIONS is set in your bashrc (or bash_profile, profile or so on) to ignore *.dat.

To verify this, simply print the env variable:

 echo $GREP_OPTIONS

Somewhere in the output you should see -exclude=*.dat

EDIT: As suggested by Fox in the comments it could also be an alias, then command -v grep should show -exclude=*.dat somewhere in its output

  • Could be an --include set as well, suggest mentioning that. – derobert Apr 25 '17 at 16:49
  • From the documentation, on GREP_OPTIONS: "As this causes problems when writing portable scripts, this feature will be removed in a future release of grep, and grep warns if it is used" (emphasis added) – Fox Apr 25 '17 at 16:50
  • @Fox that warning isn't mentioned in the grep 2.5.1 docs, and 2.5.1 does not warn. Remember OP is running a very old grep. – derobert Apr 25 '17 at 16:52
  • I stand corrected: OP is using 2.6.3, which also mentions no warning – Fox Apr 25 '17 at 16:58
  • Oh boy, I feel silly. Thank you, yes, it was GREP_OPTIONS. – Mike Dannyboy Apr 25 '17 at 17:40

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.