Multiline Grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multiline Grep
# 1  
Old 03-12-2008
Multiline Grep

How does one do a search for a multiline regular experssion and output the results to a file.

I know this won't work since grep only searches single lines:
Code:
egrep '<a>.*?</a>' source.xml > output.xml


Here are some sample patterns I'd like to match and output to a single file:
Code:
 
<a>..............</a>

<a>
   ..................
</a>


<a>
   ..................
   ..................
   ..................
</a>

Do I need perl for this?

Many Thanks!
-Mark
# 2  
Old 03-12-2008
With Unix there is always more than one way to skin a cat. I am not good with regular expressions so I did this with awk:

#!/usr/bin/ksh

cat source.xml | awk 'BEGIN {x=0}
{
if ($0~"<a>") {x=1}
if (x==1) {print $0}
if ($0~"</a>") {x=0}
}' > output.xml
# 3  
Old 03-13-2008
try this Perl script:
Code:
#!/usr/local/bin/perl
# multiliner.pl
use strict;
my $filename = shift;
open (FILE, "<", $filename)  or  die "Failed to read file $filename : $! \n";
my $whole_file;
{
    local $/;
    $whole_file = <FILE>;
}
close(FILE);
while ($whole_file =~ m#\<a\>(.*?)\<\/a\>#g) {
    print $1 . "\n";
}

run this script as:
Code:
perl multiliner.pl source.xml  > output.xml

# 4  
Old 03-13-2008
Quote:
Originally Posted by Yogesh Sawant
Code:
while ($whole_file =~ m#\<a\>(.*?)\<\/a\>#g) {

I think you may need the s option to span newlines
Code:
while ($whole_file =~ m#\<a\>(.*?)\<\/a\>#sg) {


Last edited by kahuna; 03-14-2008 at 11:36 AM..
# 5  
Old 03-14-2008
Thanks for the help!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need grep regex to extract multiline text between two strings

I have a file conatining the below: --- 10.9.16.116: /tmp/5835113081224811756.jar: hash: e6df90d38fa86f0e289f73d79cd2cfd2a29954eb /tmp/4603745991442278706.jar: hash: e6df90d38fa86f0e289f73d79cd2cfd2a29954eb 10.9.14.126: /tmp/conf/extra/httpd-ssl.conf: hash:... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Multiline sed

Hi guys, I am fairly comfortable with using the sed command if the string to be replaced is all on a single line. I was wondering is it possible to use sed command in a multiline way ? Say for example I have the below string on 2 different lines: { "key": "brandNameA", ... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

Need to get multiline input

As per my requirement, I need to get a multiline input. It can be stored in a file, that's not a problem. User will be prompted to enter steps. he should able to enter the steps in multiple lines by pressing enter. All I know in read command that reads the input till we press enter. Can someone... (5 Replies)
Discussion started by: annamalaikasi
5 Replies

4. Shell Programming and Scripting

MultiLine Patterns

Experts, I am novice unix user. At my work, most of our DBA's work on creating DDL's to create new tables in production. At every week we need to validate the scripts (do peer review) and it takes a while and also it is not effective when we have like 150 tables created in the scripts. I am... (3 Replies)
Discussion started by: ysvsr1
3 Replies

5. UNIX for Dummies Questions & Answers

Need Multiline sed help!!

Hey everyone, I'm new to sed and I need to create a script for inserting one line of code at the beginning of every method in a Xcode project (over 6,000 methods). Each method Structure is (+ or -) (Various declarations-- could span multiple lines) ({) I've tried for days, any guidance would be... (2 Replies)
Discussion started by: jimmyz
2 Replies

6. Shell Programming and Scripting

multiline pattern matching

Hi, I have a file of the following from: Afghanistan gdpcapit|800 Akrotiri Albania gdpcapit|6000 now I want have the gdpcapit value next to the country when there is one like this: Afghanistan 800 gdpcapit|800 Akrotiri Albania 6000 gdpcapit|6000 How do I do this? I've... (4 Replies)
Discussion started by: KarelVH
4 Replies

7. Shell Programming and Scripting

grep command to replace multiline text from httpd.conf file on Fedora

Hi, I am a newbie to shell scripting and to Linux environment as well. In my project I am trying to search for following text from the httpd.conf file <Directory '/somedir/someinnerdir'> AllowOverride All </Directory> and then remove this text and again rewrite the same text. The... (1 Reply)
Discussion started by: bhushan
1 Replies

8. Shell Programming and Scripting

assigning a multiline grep output which has been piped through sed to a shell variabl

Hi, I wish to format the output of a grep command in such a way that sed will be able to handle the newline characters held in the output. Since sed does not allow newlines to be contained in a replacement pattern, that means adding a backslash '\' character to the end of each line from... (8 Replies)
Discussion started by: timculhane
8 Replies

9. Shell Programming and Scripting

Multiline replace problem

I have a data of the form 0.0117843924 0. 0. 0. 0. 0.011036017 0. 0. 0. 0. 0.0103351669 0. 0. 0. 0. 4839.41211 0. 0. 0. 0. 4532.08203 0. 0. 0. 0. I would like to insert a couple of blank lines before the 4839 line, every time it appears. The numbers in the... (2 Replies)
Discussion started by: mathis
2 Replies
Login or Register to Ask a Question