LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-20-2011, 02:59 PM   #1
Tech109
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Rep: Reputation: 0
grep behavior using a 'for' loop


Hi, I'm puzzled as to why grep behaves differently under these two circumstances:

>grep -e String_To_Find filename.txt

-Returns just the line with exact match for String_To_Find


>for i in stringsfile.txt; do grep -e filename.txt; done

-Returns results as if it's not obeying the '-e' flag.


What I'm trying to accomplish is use a text file containing a list of strings, come which contain spaces in between and some which do not, and feed them into a loop to search for each exact match of the string in another file. I have even tried listing the strings out of the strings file using echo $(cat stringsfile.txt) and then iterating through that list, but same result.

I've tried bounding the variable with ^ and $, tried variations of single quotes, double-quotes, no quotes, but nothing seems to work.
 
Old 10-20-2011, 03:09 PM   #2
Tech109
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Ok, so I had better results using this:

>grep -f stringsfile.txt filename.txt

But I still get some inaccurate matches.

The strings in stringsfile.txt are each on a separate line, with no single or double-quotes. The file contains 43 lines of strings, but the result I get returns 58 lines, because it is matching greedily, not exactly.
 
Old 10-20-2011, 03:12 PM   #3
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Is your quoted code complete? You are not using the $i variable. (Use [ CODE ] tags e.g via the # button around code snippets.)

As I understand it the -e parameter does not change the behaviour of grep - it's just a way to highlight the pattern when it would be ambiguous. But -E does change behaviour. Did you mean -E?
 
Old 10-20-2011, 03:14 PM   #4
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Cross-posted with your reply ... I think it would be useful if you can post a minimal (2 or 3 lines) example of patterns and a file that illustrates the problem.
 
Old 10-20-2011, 04:30 PM   #5
Tech109
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Sorry, I ommitted the $i variable...

Code:
for i in stringsfile.txt; do grep -e $i filename.txt; done
Or,

Code:
for i in ABCD HIJK LMNO PQRS TUVW; do grep -e $i filename.txt; done
So here's a hypothetical example of a string that I'm searching for an exact match on:

ABCD

I know this string exists in the master file that I'm grepping, but I need to see additional information about it (it's a database item that I need to configure).


So in the master file I'm grepping, there are many lines with strings that contain ABCD:

1234 ABCD char 100 # Description of what it is used for
1235 ABCD_History char 100 # Description of what it is used for
1236 Admin_ABCD char 100 # Description of what it is used for
1237 "ABCD Billing" char 100 # Description of what it is used for


Now the only one I'm interested in returning is the ABCD line, but my search using a loop will return all the lines listed above. I need to see this line because it tells me this is a character type (vs float) and it can hold 100 characters.

Hope this helps, and thanks for responding.

Last edited by Tech109; 10-20-2011 at 04:36 PM. Reason: Added CODE tags
 
Old 10-20-2011, 05:59 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,099

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
regex is always (?) greedy. You might be able to get away with using word bounding (probably not given that snippet), or better specify your regex to do what you want.
 
Old 10-21-2011, 02:49 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by syg00 View Post
regex is always (?) greedy.
AFAIK, that is true for grep and most other standard tools in Linux.
Exceptions I know of are perl RegEx and Java RegEx which allow you to additionally specify non-greedy RegEx.

http://www.troubleshooters.com/codec...reg.htm#Greedy
http://download.oracle.com/javase/tu...gex/quant.html
 
Old 10-21-2011, 04:30 AM   #8
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Code:
for i in ABCD HIJK LMNO PQRS TUVW; do
    grep -w "$i" filename.txt
done
The -w option (--word-regexp) matches only whole words.

If you don't want the search string inside double quotes as you post #5 seems to tell:
Code:
for i in ABCD HIJK LMNO PQRS TUVW; do
    grep -w "$i" filename.txt|grep -v "\".*${i}.*\""
done

Last edited by berbae; 10-21-2011 at 10:24 AM.
 
1 members found this post helpful.
Old 11-08-2011, 04:23 PM   #9
Tech109
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks everyone for responding, and to berbae, for providing the solution, although I stumbled upon it myself before coming back here.

What worked was the following:

Code:
for i in ABCD HIJK LMNO PQRS TUVW; do
    grep -w "$i" filename.txt
done
The key being the double-quotes around the variable.
 
  


Reply

Tags
egrep, grep, grep forloop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird behavior with grep - getting ^M in output when they are not in the file Stephanie Seney Programming 1 10-04-2011 12:21 PM
Using 'find' and 'grep' in a for loop linuxlastslonge Linux - General 4 03-23-2011 04:16 PM
Grep in a for loop. rose_bud4201 Programming 13 03-18-2009 04:02 AM
strange behavior of find or grep alenD Programming 4 09-21-2007 06:35 AM
Strange behavior: string within scope return 0 if not within a while loop. RHLinuxGUY Programming 2 08-05-2006 11:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration