Multiple line match using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple line match using sed
# 8  
Old 03-27-2008
So the beef is the B after the closing brace, and you want that lifted up there before the opening brace? Or what?

Code:
perl -0777 -pe 's/(\s*\{.*?\})\s*(\S+);/\2\1;/sg'

# 9  
Old 03-27-2008
Thats right.
I have to check before and after the { } and combine A and B (and leave rest of stuff the file intact)

Thanks a lot for your perl script, it works! I will try to make it work with actual header file
# 10  
Old 03-28-2008
Good Morning ERA,
Your perl magic works fine. It needs a bit of tweaking in my actual test case
perl -0777 -pe 's/(\s*\{.*?\})\s*(\S*);/\2\1;/sg'

Also, irrespective of
struct A{
...
}B;
or enum A{
...
}B;
It moves B up. Could you please help me with making it work for specifically struct scenario. Thanks
# 11  
Old 03-28-2008
Tighten up the regular expression for the opening brace. The \s* means whitespace so you'd want the "struct" keyword there in front of that.
# 12  
Old 03-28-2008
perl -0777 -pe 's/struct(\s*\{.*?\})\s*(\S*);/struct \2\1;/sg' $file worked only for strtuct leaving enum intact
# 13  
Old 03-28-2008
You can lift the struct inside the parens to reduce the duplication, too.
# 14  
Old 03-28-2008
Thanks era. Good tips and Great help.

One more twist, say
if A exists(non-null) don't move B, just remove it.

Logic:
if A = "";
than
move B before {
else
delete B

Reason for doing this is to handle scnario like
struct A{
...
}A_T;
Your earlier conversion script is able to handle
typedef struct{
...
}A;
and
struct A{
...
};
output has to be
struct A{
...
};
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