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

    Question Unanswered: The grep command (HELP!!)

    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...?

    TIA

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

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    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
    Posts
    8
    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
  •