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 > problem in pattern matching in shell script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-04, 06:52
amitb82 amitb82 is offline
Registered User
 
Join Date: Jul 2004
Posts: 1
problem in pattern matching in shell script

need some help regarding shell scripts...
what i'm doing is finding the occurence of a string in a directory(recursively)
to get the count of files..i'm giving
count="`grep -r "$string" * | wc -l`"
and for getting the file names i'm giving
for file in "`grep -irn "$string" *`"


I just want to ask can i get the count as well as the file names in one command .so that i don't have to use grep twice and get both the variables(count & filename).

And one more thing..i want to find a string like
"function(" not like ".function(" .
eg. if i have 2 lines
a.getdata();
getdata();
So after grep it should return me only the second line.
How should i go 'bout it??

Thanks in advance.
-Amit
Reply With Quote
  #2 (permalink)  
Old 07-14-04, 08:50
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
I'm not sure I fully understand your problem but if you're asking how you can set more than one variable in a single command, use read.
Code:
grep -c "$string" * | while IFS=: read fileName count
do
  echo "File $fileName has $count lines containing $string"
done
As for your second question, can you elaborate a little because I really can't grasp what you might be asking.

Damian

Last edited by Damian Ibbotson; 07-14-04 at 08:52.
Reply With Quote
  #3 (permalink)  
Old 07-18-04, 21:18
saurav101 saurav101 is offline
Registered User
 
Join Date: Oct 2003
Posts: 19
Amit
Did you try using grep -x . I guess that should help e.g. as per your file
grep -x "getdata()" <inputfile> should return the second line .
Let me know if this works
Best Regards
Saurav
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