Welcome to the MacNN Forums.

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.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > grepping with AND without regex

grepping with AND without regex
Thread Tools
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Feb 2, 2005, 12:19 PM
 
This is kind of a weird question: In one of my scripts I need to grep for a certain string, $FILE, in a text file, $FILELIST.

In $FILELIST, I only want to find exact matches. For instance if $FILE is /Users/wataru/Music, and there is a line in $FILELIST that says /Users/wataru/Music/iTunes, I don't want to find that. To do that I used regex's $ to indicate the end of the line, as in "grep $FILE$ $FILELIST". Unfortunately, I just found out through trial and error that grep will interpret any regex metacharacters in $FILE as such, meaning that grepping this way for "MyApp1.0.1" will also match "MyApp1.0b1" because the period metacharacter matches any single character.

So what I need is either to prevent grep from intepreting $FILE as regex, but keep $ as regex so it can find the end of the line. Or I need a different way to find the end of the line. Any ideas?
     
Gavin
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Feb 3, 2005, 05:16 AM
 
hmm.. $FILE$ ought to do it.

I read that to be 'just this, followed by the end of the line'

it works from the command line grep Users/wataru/Music$ filelist.txt
maybe it has to do with the way the script/shell is interpreting the var

maybe try enclosing the patern in quotes
grep "$FILE$" $FILELIST

maybe also try adding the beginning of the line char ^
grep "^$FILE$" $FILELIST

finally try putting the whole pattern into a var then use that on the grep line
patern="^$FILE\$"
grep $patern $FILELIST
     
Curios Meerkat
Forum Regular
Join Date: Nov 2004
Location: Am�rica
Status: Offline
Reply With Quote
Feb 3, 2005, 07:38 PM
 
...or "$FILES"$

�somehow we find it hard to sell our values, namely that the rich should plunder the poor. - J. F. Dulles
     
sfederman
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Francisco, CA
Status: Offline
Reply With Quote
Feb 4, 2005, 04:54 AM
 
You should be able to use $ at the end of your regex to search for only exact matches, but you may want to add ^ to the beginning to ensure that your match won't have extra characters at the beginning that aren't in your search pattern.

Also, in order to search for a literal period character, you need to backslash it. So, you might do something like this:

grep "^MyApp1\.0\.1$"

-Scot


Originally posted by wataru:
This is kind of a weird question: In one of my scripts I need to grep for a certain string, $FILE, in a text file, $FILELIST.

In $FILELIST, I only want to find exact matches. For instance if $FILE is /Users/wataru/Music, and there is a line in $FILELIST that says /Users/wataru/Music/iTunes, I don't want to find that. To do that I used regex's $ to indicate the end of the line, as in "grep $FILE$ $FILELIST". Unfortunately, I just found out through trial and error that grep will interpret any regex metacharacters in $FILE as such, meaning that grepping this way for "MyApp1.0.1" will also match "MyApp1.0b1" because the period metacharacter matches any single character.

So what I need is either to prevent grep from intepreting $FILE as regex, but keep $ as regex so it can find the end of the line. Or I need a different way to find the end of the line. Any ideas?
     
   
Thread Tools
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:16 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,