13

So when I run this in Fedora I'm seeing this:

$ ls hmm_data/indivA12_AATAAG/refs/par1/
2R-orths.alleles  2R-ref.alleles
$ ls hmm_data/indivA12_AATAAG/refs/par1/ | grep -F '-ref.alleles'
2R-ref.alleles

But when I run on Ubuntu (same data) I don't get any results from the grep:

$ ls hmm_data/indivA12_AATAAG/refs/par1/
2R-orths.alleles  2R-ref.alleles
$ ls hmm_data/indivA12_AATAAG/refs/par1/ | grep -F '-ref.alleles'

Any ideas what could be going on? How can I come up with something that will work the same on both systems?

  • How about ls *-ref.alleles ? – glenn jackman Oct 2 '14 at 19:29
  • @glennjackman, I thought of that but someone did exactly that and commented that line out in the bash script I'm trying to fix :-) I can only assume they had a reason ... – Greg_the_Ant Oct 2 '14 at 19:39
30
grep -F '-ref.alleles'

is equivalent to:

grep -F -ref.alleles

(none of the characters between the apostrophes are shell metacharacters, so quoting them has no effect.)

This is in turn equivalent to:

grep -F -r -e f.alleles

by normal parsing of - prefixed options. The -e option takes an argument, but -F and -r don't.

Since you didn't specify any files to grep, the default behavior is to act on stdin... except that the -r option makes no sense so it defaults to searching . (the current directory) recursively instead and ignores stdin. In some versions.

You need to use the -- "no more options" indicator before a regexp that starts with - as in

grep -F -- -ref.alleles

I tracked down the point where the behavior of -r with no file arguments changed. It was in version 2.11, released March 2, 2012. See the release announcement.

The git commits which affected the behavior are this one and this one.

If you run grep --version on your two machines, I'm sure you'll find that one of them is on the wrong side of 2.11

  • Great answer, thanks for the research all the way to the version where the behaviour changed. – richardneish Oct 3 '14 at 8:33
  • 2
    wow thanks. You're right, I've got grep 2.6.3 vs grep 2.14. – Greg_the_Ant Oct 3 '14 at 14:07
6

The leading - is the problem. To get same results add --:

grep -F -- '-ref.alleles'

From man bash:

A -- signals the end of options and disables further option
processing. Any arguments after the -- are treated as filenames
and arguments.
  • 1
    Why does it differ on Ubuntu and Fedora? Shouldn't it give the same negative result on both of them? – Glueon Oct 2 '14 at 19:34
  • Thanks. If this command is to be piped into another command, would the -- cause any problems there? Or does -- just apply to the command where it shows up? – Greg_the_Ant Oct 2 '14 at 19:37
  • 4
    The bash man page isn't really relevant. It explains the treatment of -- on bash's own command line, which is independent from grep's understanding of --. They work the same way because it's a common convention, but in general your shell's man page doesn't tell you anything about the meaning of arguments to other programs. – Wumpus Q. Wumbley Oct 2 '14 at 19:41
1

Check .bashrc if there are any aliases on your grep command which override it's behaviour. Maybe it is the issue. Also try grep without the "-F" param.

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.