LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-29-2009, 04:28 AM   #1
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Search multiple lines in sed or vi


Hi experts,

I have following code:
Code:
for (int i=0; i<DimSum ;i++)
{
   a[i] = new double [10];
}
for (int j=0; j<DimSum ;j++)
{
   b[j] = x+y;
}
I want to search all such for loops where a "new" is present. I want to replace DimSum from all such loops with SumDim.

To search, I tried in vi (or in sed):

Code:
/for.*DimSum.*\n.*\n.*new
This works for the given search. But if the code is
Code:
for (int i=0; i<DimSum ;i++)
{
   x = junk;//multiple lines before new
   a[i] = new double [10];
}
then I need to search for any number of new line characters.

basically, I want ".*\n.*" to be searched any number of times until a "new" is found (thus I want to have a * after it) or a "}" is encountered . But I dont know how to group "\n.*" for the following * to take effect and to stop search if "}" is found.

Can anyone help me with this mess?

Regards

Thanks in Advance!
 
Old 10-29-2009, 04:36 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ awk 'BEGIN{RS="}"}/new/{ gsub("DimSum","SumSum")}{print $0 RT}' file
for (int i=0; i<SumSum ;i++)
{
   a[i] = new double [10];
}

for (int j=0; j<DimSum ;j++)
{
   b[j] = x+y;
}
 
Old 10-29-2009, 04:45 AM   #3
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Hi ghostdog74

Thanks for the quick response. I am trying this out.

BTW, Can this be done in sed or vi also? If not then I think I should start learning awk!

Regards
 
Old 10-29-2009, 08:53 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Why do you only want to replace DimSum with SumDim in those loops? Surely you need to replace it everywhere, or your code won't work?
 
Old 10-30-2009, 12:35 AM   #5
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by chrism01 View Post
Why do you only want to replace DimSum with SumDim in those loops? Surely you need to replace it everywhere, or your code won't work?
I want to change only those for loops which allocate memory using "new".
Also which de-allocates using delete.

I want to search all for loops wherever new/delete is encountered and change the for loop.

Regards
 
Old 10-30-2009, 12:55 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Multi-line actions can be done in sed, but in general it's not particularly easy to do so. It usually involves adding multiple lines to the search buffer first, and maybe using the hold buffer as well.

Some useful links for sed and awk:

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html

http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/manual/gawk/html_node/index.html
 
Old 10-30-2009, 01:09 AM   #7
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by David the H. View Post
Multi-line actions can be done in sed, but in general it's not particularly easy to do so. It usually involves adding multiple lines to the search buffer first, and maybe using the hold buffer as well.

Some useful links for sed and awk:

http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html

http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/manual/gawk/html_node/index.html
I have read http://www.grymoire.com/Unix/Sed.html already.

I am able to read/search multiple lines using '\n' in my search pattern.

I am able to search and replace as well using back-referencing.

My only trouble is that sometimes there can be multiple and unknown number of '\n' in my search pattern.

Example1:

for.*DimSum.*\n.*new

will search
for(int i; i<DimSum; i++){
junk = new double;


Example2:
for.*DimSum.*\n.*\n.*new

will search
for(int i; i<DimSum; i++)
{
junk = new double;


But what to do when number of '\n' is not known?
 
Old 10-30-2009, 01:58 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vijay_babu1981 View Post
If not then I think I should start learning awk!

Regards
forget about sed or vi already.. start learning awk now...Or Python/Perl for more complicated tasks...!!
 
Old 10-30-2009, 11:02 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Use the right tool for the right job.

Sed is a line-based editing program. It mostly works on one line at a time. And while it can be used for multi-line editing, it's not truly suited for it. It's best for simple substitutions and extracting strings from text when the matching needs are not too complicated.

Awk is a field-based editor. It defaults to one word-one field, but you can redefine the fields as necessary, so you can also use it to work on lines, paragraphs, individual characters or whatever. It's also includes a more complex language syntax that makes it more flexible than sed for many uses. But it's perhaps still not always the best; its not the most user-friendly program, and awk scripts sometimes have to be rather long and complex.

I haven't really used perl much yet, but my impression is that it excels in text editing in general, so it's probably the best suited of all for complex matching needs like this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED multiple lines masterdam79 Linux - Newbie 6 10-29-2009 04:25 AM
Inserting Multiple Lines (with newline) using sed or awk hal8000b Programming 1 03-08-2009 05:21 PM
Problems defining SED pattern over multiple lines dj_bridges Programming 12 10-31-2007 08:06 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration