Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I need to grep a pattern which can be present in one line or could be split in 2 lines... Example getData(xxx, yyy); getData(xxx, yyy); Normal grep wont work ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    5

    how to grep a pattern which is split in 2 lines.


    Hi,

    I need to grep a pattern which can be present in one line or could be split in 2 lines...

    Example

    getData(xxx, yyy);

    getData(xxx,
    yyy);

    Normal grep wont work in this case. Can anyone please help on this?

    There are 100's of files in which i need to search for this pattern so time is also a constrain.

    Thanks in advance

    linuxguy2011
    Last edited by linuxguy2011; 12-09-2010 at 09:36 AM.

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    So you're really looking for 3 patterns:
    Code:
    pattern1: getData(xxx, yyy);
    pattern2: getData(xxx,
    pattern3: yyy);
    Perl would perhaps be better suited to this task. If pattern1 then print else if pattern2 get next line and concatenate with previous line and print?

  3. #3
    Just Joined!
    Join Date
    Jun 2010
    Posts
    6

    Found this Perl script

    I Googled for "multi line grep":

    www . unix . com/unix-dummies-questions-answers/56703-multiline-grep.html

    You'll want to replace the regular expression within the #'s with your search string, e.g. replace this:
    Code:
    while ($whole_file =~ m#\<a\>(.*?)\<\/a\>#sg) {
    Using your examples, with something like this:
    Code:
    while ($whole_file =~ m#(getData\(.*?\);)#sg) {
    You can modify this script to take a search string as a parameter instead of the hard-coded "getData". Use $ARGV[0] in place of getData.

    This also looks promising: www . commandlinefu . com/commands/view/5087/multi-line-grep

    Sorry about the links - can't post URL's until I have 15 posts!

    Go go Perl!

    -dufftime

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jul 2005
    Posts
    1

    field grepping

    You can awk command to grep the two columns.

    If you want to grep only first column of a file for certain string. You use following cmd,

    awk -F: '{print $1} <filename> |grep <string>

    ~Venky

  6. #5
    Just Joined!
    Join Date
    Dec 2010
    Posts
    5
    Hi Guys,

    Thanks for your replies...I solved the problem using a different way

    I used this approch since there were so many files and that too in different directories so i needed a quick way to search

    Take the below as example

    pattern1: getData(xxx, yyy);
    pattern2: getData(xxx,
    pattern3: yyy);

    i searched for xxx (using grep -n) and appended the output to a file say xxxout

    then i searched for yyy (using grep -n) and appeneded the output to a different file say yyyout

    once the search went through all the files i can match the lines in xxxout with yyyout line by line.

    If the line in xxxout matches with a line in yyyout that means pattern1: getData(xxx, yyy); is found.

    if the line does not match then get the line number and file name using some sed operation (sorry i dont have the code at hand now...using my pda) increment the line number by 1 and search for "$filename:$lineNo" in yyyout

    if the pattern is found that means the

    pattern2: getData(xxx,
    pattern3: yyy)

    is found.

    Reject everything else.

    This saves a lot of time when a large number of files are to be searched in lots of directories

    I hope i made it clear.

    linuxguy2011

  7. #6
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    What about
    Code:
    grep -P 'getData\(xxx,\s*yyy\);'
    With PCREs enabled, grep also matches newlines as whitespace characters.
    Refining Linux Advent calendar: 24 Outstanding ZSH Gems

Posting Permissions

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