If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > The grep command (HELP!!)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-31-03, 11:26
MultiD MultiD is offline
Registered User
 
Join Date: Jun 2003
Posts: 8
Question 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 11:35.
Reply With Quote
  #2 (permalink)  
Old 07-31-03, 11:45
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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'
Reply With Quote
  #3 (permalink)  
Old 08-01-03, 07:39
MultiD MultiD is offline
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks Damian, I've learnt quite a bit from that
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On