Multiple line match using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple line match using sed
# 1  
Old 03-26-2008
Question Multiple line match using sed

Please help!

Input pattern, where ... could be any number of lines
struct A {
Blah1
Blah2
Blah3
...
} B;

output pattern
struct AB {
Blah1
Blah2
Blah3
...
};

I need help in extracting everything between { and }
if it would have been on a single line { \(.*\)} should have worked.
# 2  
Old 03-27-2008
Code:
perl -0777 -pe 's/\A[^\{]*\{//s; s/\}.*?\{/\n/sg; s/\}[^\}]*\Z//s'

... assuming there is no nesting of braces. Put in a better separator than just a newline if you like. The \A and \Z patterns match beginning of file and end of file, respectively; the middle substitution is the meat of the program.
# 3  
Old 03-27-2008
Thanks era, but I am not seeing expected output using perl command

$ perl -0777 -pe 's/\A[^\{]*\{//s; s/\}.*?\{/\n/sg; s/\}[^\}]*\Z//s' test

Blah1
Blah2
Blah3
...


So it looks like that your script is extracting everything between { and }


Is there any way that this can be cooked in sed?

I was thinking on the line of
sed -e "/^struct.*{.*/,/.*}.*/s/^struct[[:blank:]]*\(.*\)[[:blank:]]*{\(.*\)}\(.*\);/struct \1\3 {\2};/g

This is where I see problem in extracting multiline "\2" and "\3"

I know I am not an advance sed user, and it needs some N H kinda magic Smilie

Last edited by SiftinDotCom; 03-27-2008 at 04:52 AM..
# 4  
Old 03-27-2008
Quote:
Originally Posted by SiftinDotCom
I need help in extracting everything between { and }
Quote:
Originally Posted by SiftinDotCom
Thanks era, but I am not seeing expected output using perl command /.../
So it looks like that your script is extracting everything between { and }
You are contradicting yourself. If that's not what you want, then what do you want?

Quote:
Is there any way that this can be cooked in sed?
Probably it could, if you are handy with sed, but I would not go there. (I have, more times than I care to remember, but it's so much easier in Perl.)

As an aside, you don't need to specify leading and trailing .* wildcards; sed will find the requested pattern anywhere on the line anyway (and in fact you are making it a bit harder for it).
# 5  
Old 03-27-2008
Code:
sed -n '/{/,/}/{
s/^[^{]*{//
s/}[^}]*$//
p
}'

But you said this is not what you want, so you will need to explain what you do want.
# 6  
Old 03-27-2008
... or maybe

Code:
sed -n '/{/,/}/{
/{/d
/}/d
p
}'

would be more to your liking. Feel free to replace either { or } with some sort of separator, again.
# 7  
Old 03-27-2008
era, thanks for these scripts.
Sorry about the confusing first comment.
I was tryingto get output pattern
struct AB {
Blah1
Blah2
Blah3
...
};

and in this process I was unable to extract and replace multiline pattern from input
struct A {
Blah1
Blah2
Blah3
...
} B;

even if I tweak my sed command to include \n
sed -e "/^struct.*{.*/,/.*}.*/s/^struct[[:blank:]]*\(.*\)[[:blank:]]*{\(.*\)}\(.*\);/struct \1\3 {\2};/g

As you can see that I am not proficient with multiline pattern handling, thats why I see sed experts help in this forum
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

2. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

3. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

4. Shell Programming and Scripting

SED - Match a line from a File

Hi, I want to match a line which exists in a file. I have written a test script similar to below - The content of the file file.txt would be like this - /usr/bin/1234.xcf /usr/bin/3456.xcf /usr/bin/7897.xcf /usr/bin/2345.xcf out=`sed -n '\/usr\/bin\/7897.xcf/p' file.txt 2>&1`... (3 Replies)
Discussion started by: angshuman_ag
3 Replies

5. Shell Programming and Scripting

SED, match a line block

Hello, I want to do a simple substitution using sed but I can't find a solution. Basically, from a Apache conf file, I would like to remove everything included between the <VirtualHost> and </VirtualHost> e.g SSLMutex file:/var/run/ssl_mutex <VirtualHost _default_:443> # A lot of config that... (5 Replies)
Discussion started by: RobertFord
5 Replies

6. Shell Programming and Scripting

SED - adding blank line after each Range Match

the following range matching works great but i wish to add a blank line after each range result set... which i've tried and researched to no avail MY INPUT DATA: CURRENT CODE I'M USING: sed -n '/*$/,/;/p' $INPUT_FILE RESULTS I'M GETTING: RESULT I looking to... (5 Replies)
Discussion started by: danmauer
5 Replies

7. Shell Programming and Scripting

sed: find match and delete the line above

I am searching a dhcpd.conf to find the hardware ethernet match, then once the match is found delete just the line above it. For example: testmachine.example { hardware ethernet 00:00:00:00:00:00; fixed address 192.168.1.100; next-server 192.168.1.101; filename "linux-install/pxelinux.0"; }... (3 Replies)
Discussion started by: cstovall
3 Replies

8. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies

9. Shell Programming and Scripting

Concatenating multiple lines to one line if match pattern

Hi all, I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file: Severity............: MAJORWARNING Summary: System temperature is out of normal range. Severity............: MAJORWARNING... (13 Replies)
Discussion started by: phixsius
13 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question