Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2004
    Posts
    1

    Unanswered: 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

  2. #2
    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 09:52.

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •