1
cat file.txt | grep -x "\d*"
grep: \Documents and Settings: Is a directory

I want to search file.txt for any lines that are numbers only but grep seems to be viewing \d* as a wildcard for files and not the pattern. How can I specify that it's the pattern and it should use stdin for what to grep over?

The file is full of lines of datetime stamps, some end with a letter, some don't.

20140110122200
20131208041510M
...

I'm trying to only get the lines that don't end in a letter.

EDIT: I've also tried setting the filename instead of piping it with cat. Not much different.

C:\long\path>grep -ex "\d*" -f file.txt
grep: \Dell: Is a directory
grep: \Documents and Settings: Is a directory
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
  • 1
    I wasn't sure if the manpages applied to gnuwin packages. Still, using `-e` or setting the filename to `-` makes no difference – Corey Ogburn Jan 10 '14 at 19:26
  • Look here: http://stackoverflow.com/questions/21051959/use-literal-in-grep-pattern-within-shell-script – Jotne Jan 10 '14 at 19:35
  • @Jotne Does that apply to windows? I thought double quote vs single quote rules for expansion where just *nix. – Corey Ogburn Jan 10 '14 at 19:40
  • 1
    Option `-e` tells `grep` to use the following text as pattern. When using `-e` with any other options, care must be taken. In your case, option `-x` was treated as the pattern to use thus the command is evaluated as `grep -e x "\d*"`. What you wanted is grep -xe "\d*". – alvits Jan 10 '14 at 20:09

1 Answers1

0

Why are you using cat to pass the file to grep? Why not just give grep the filename directly?

grep -x '\d*' file.txt

I think the actual problem you're seeing is that the * wildcard is being expanded. That's why grep is giving you errors that mention actual directories (beginning with 'd') on your system.

John Bartholomew
  • 6,428
  • 1
  • 30
  • 39