Hello, On Sun, 08 Jul 2012, lynn wrote:
On 08/07/12 19:11, David Haller wrote:
grep '\[7a\]' file
Sorry guys. I may have got the \'s after an hour or so but the single quotes would have, ahem, escaped me.
Anyway, you have saved me countless hours of the FM.
There are a few simple rules in shell-programming, first and IMO by far the most important: * quote! With '' unless you _need_ to have something to expand inside, with "" in other cases. If in doubt, mix quoting: prog 'foo'"$bar"'baz' * use 'set -x' to see _how_ a program is actually called. Case in point: $ set -x $ grep [7a] /dev/null + grep '[7a]' /dev/null $ grep \[7a\] /dev/null + grep '[7a]' /dev/null ==== Excursion! ==== BTW: above is only because there's no matching files in the current directory. $ touch a 7 + touch a 7 $ grep [7a] /dev/null + grep 7 a /dev/null ### this is NOT what you want, eh? ### grep for '7' in files 'a' and /dev/null. $ grep \[7a\] /dev/null + grep '[7a]' /dev/null ### this is ;) But quoting is easier ### and more robust. $ rm a 7 Understand this "excursion"! It's quite important to understand in respect to why quoting is so important. ==== $ grep '[7a]' /dev/null + grep '[7a]' /dev/null $ grep '\[7a\]' /dev/null + grep '\[7a\]' /dev/null $ grep \\\[7a\\\] /dev/null + grep '\[7a\]' /dev/null $ Now, once you've figured that out, how the shell calls grep, you'll need to look at 'man 7 regex' to find out what you need to use for grep to do what you want ;) The cases where you can't quote at all are _very_ rare and far between and use those only when you know exactly what and _why_ you're doing. Read up on "special characters" in the shell though. And for grep, you _will_ need to know the regexp special characters, man 7 regex is a bit harsh an entry, but there's plenty of "nice" regex introductions on the web (I'd guess the Wikipedia links to at least one ;). Also, try digging through this lists archive, articles by me containing a keyword like "quote", "quoting", "regex", "bash", often also answering "Carlos" should yield some results. See http://lists.opensuse.org or use google with "site:lists.opensuse.org" for a different search engine. Feel free to ask more, but preferably you should do that on opensuse-programming. HTH, -dnh -- Do you have feelings of inadequacy? Do you suffer from shyness? Do you sometimes wish you were more assertive? If you answered "yes" to any of these questions, ask your doctor or pharmacist about Tequila. -- asr -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org