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.
- 12-09-2010 #1Just 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
linuxguy2011Last edited by linuxguy2011; 12-09-2010 at 09:36 AM.
- 12-09-2010 #2
So you're really looking for 3 patterns:
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?Code:pattern1: getData(xxx, yyy); pattern2: getData(xxx, pattern3: yyy);
- 12-09-2010 #3Just 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:
Using your examples, with something like this:Code:while ($whole_file =~ m#\<a\>(.*?)\<\/a\>#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.Code:while ($whole_file =~ m#(getData\(.*?\);)#sg) {
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
- 12-10-2010 #4Just 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
- 12-10-2010 #5Just 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
- 12-10-2010 #6
What about
With PCREs enabled, grep also matches newlines as whitespace characters.Code:grep -P 'getData\(xxx,\s*yyy\);'
Refining Linux Advent calendar: “24 Outstanding ZSH Gems”


Reply With Quote
