|
|
grepping with AND without regex
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Nov 2004
Location: Am�rica
Status:
Offline
|
|
|
�somehow we find it hard to sell our values, namely that the rich should plunder the poor. - J. F. Dulles
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Francisco, CA
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|