Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2003

    Question Unanswered: The grep command (HELP!!)


    I'm trying to output any paragraph instance in a text file that a * occurs,

    i've used the command cat filename.txt |grep -p '*'

    but as * is a wildcard it throws out every paragraph in the file, and not just the ones that contain a *

    Is there anyway of getting round this i.e by using a char code instead...?


    Last edited by MultiD; 07-31-03 at 11:35.

  2. #2
    Join Date
    Jun 2002
    Several lessons to be learnt here.

    Firstly '*' is not a wildcard in this context, it is used to mean 'match 0 or many of the preceding character'. Thus:

    a* would match a, aaaa or 'nothing'
    aa* would match a, aaaa but would not match 'nothing'
    [a-z]* would match a, zyxabc or 'nothing'

    Secondly, why are you using cat? Grep will take a file as an argument so why cat it first?

    grep "regex" yourFile

    Finally (and the answer you are looking for), to escape the special meaning of a metacharacter (e.g. *-$), use the backslash '\\'

    grep "\*" would match the '*' character
    grep "\**" would match *, ******* and 'nothing'

  3. #3
    Join Date
    Jun 2003
    Thanks Damian, I've learnt quite a bit from that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts