D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] grep and dreadful command line skills

 

On Mon, 23 Nov 2009, Neil Stone wrote:

Michael Mortimore wrote:
On Sun, 22 Nov 2009 17:42:15 -0000, Simon Robert <simon.robert@xxxxxxxxxxxxxxxxxxxxxx> wrote:
if I do "grep Transition *.yaml" I get

simon@frog2:~/.alexandria/My Library$ grep Transition *.yaml
grep: invalid option -- '.'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

but I've not tried the above yet.

OK, I tried it and it works great. Ta.
Do the yaml files have funny names or something? if I do "grep a *.txt" (since i happened to have a load of txt files in ~) it works as expected.

or maybe you're using a different shell to me? mine is bash.

Do this

grep Transition "*.yaml"

And this is the sort of thing that makes me jump up and down in anger!!!

Most shells have perfectly good filename expansion feature, but putting an argument in quotes turns this off, and leaves it up to the program to do the filename expansion - thus bloating the program and adding yet another layer of complexity...

However in this case, it won't work because grep, which has it's own filename expanding capability needs to be told to do it. Passing it *.yaml literally, presents that as the filename - which probably doesn't exist. (Having filenames with stars in them isn't generally a good idea)

If you want to test what happens when you expand filenames in the shell, try this:

  echo -- *.yaml

The -- will be part of the output, but it will prevent echo interpreting filenames that start with a - as a parameter o the echo command. A lot of command take -- as an indicator to stop looking for parameters on the command-line.

When grep is saying "invalid option" above, it means that you probably have a filename that starts with a - and probably -. (dash dot) then something else.

If you want to grep files in the current directory, where filenames may start with a - then use:

  grep -- Transition *.yaml

Gordon

--
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html