LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-25-2008, 07:19 AM   #1
sjreilly
Member
 
Registered: Oct 2007
Location: Edinburgh, Scotland
Distribution: Debian, CentOS, RHEL, Mint, Ubuntu
Posts: 79

Rep: Reputation: 15
Multiline Text Replacement Script


Hi,

I have a large number of files that contain data in a specific format;

cdfId->simInfo->test1 = '(nil
instParameters (m ne plug tvolt volt)
otherParameters (model)
)

cdfId->simInfo->test2 = '(nil
instParameters (m ne plug tvolt volt)
otherParameters (model)
)

cdfId->simInfo->test3 = '(nil
instParameters (m es ne plug tvolt tunique)
netlistProcedure new
)

I want to modify each file to add an extra term into the instParameters line BUT only on the lines preceded by cdfId->simInfo->test2
So that it looks like;

cdfId->simInfo->test1 = '(nil
instParameters (m ne plug tvolt volt)
otherParameters (model)
)

cdfId->simInfo->test2 = '(nil
instParameters (m ne plug tvolt volt extraterm)
otherParameters (model)
)

cdfId->simInfo->test3 = '(nil
instParameters (m es ne plug tvolt tunique)
netlistProcedure new
)

Any ideas?

TIA

Steve

Last edited by sjreilly; 04-25-2008 at 08:14 AM.
 
Old 04-25-2008, 07:51 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It is not clear to me if the line containing instParameters have to be immediately preceded by cdfId->simInfo->test2. I guess you mean it should be under the cdfId->simInfo->test2 section. In this case you can do something like this in awk:
Code:
/cdfId->simInfo->test2/ { 

# while not encounter instParameters print line and get next line
while ( $1 != "instParameters" ) {
   print
   getline
}

# line found: add extraterm before closing parenthesis
print substr($0,1,index($0,")")-1),"extraterm)"

# go to next line
getline

}

# print any other line not under "cdfId->simInfo->test2" section
1

Last edited by colucix; 04-25-2008 at 07:55 AM.
 
Old 04-25-2008, 08:04 AM   #3
sjreilly
Member
 
Registered: Oct 2007
Location: Edinburgh, Scotland
Distribution: Debian, CentOS, RHEL, Mint, Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 15
colucix,

Sorry for the confusion - (I've now corrected the original). Yes, instParameters should appear on the line below cdfId->simInfo->test2

How would you run awk with your code?

Steve
 
Old 04-25-2008, 08:17 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
With SED:

sed '/cdfId->simInfo->test2/{n; s/)$/ extraterm)/}'

Translation:
find a line containing the pattern "cdfId->simInfo->test2"
get the next line
replace the ")" at the end of the line with " extraterm)"
 
Old 04-25-2008, 08:21 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Ok. In that case we don't need the while loop, just check if the line below cdfId->simInfo->test2 starts with instParameters:
Code:
/cdfId->simInfo->test2/ {

# print this line and get the next one
print
getline

# if it contains instParameters add extraterm before closing parenthesis
if ( $1 == "instParameters" )
   print substr($0,1,index($0,")")-1),"extraterm)"
else
   print

# go to next line
getline

}

# print any other line not under "cdfId->simInfo->test2" section
1
To run the awk code, save it in a file (for example extraterm.awk) and run the following command
Code:
gawk -f extraterm.awk file
where file is the file to be processed. Unfortunately awk cannot edit the file in place, so you have to redirect the output to a temporary file and then overwrite the original one:
Code:
gawk -f extraterm.awk file > tmpfile
mv tmpfile file
but do this only when you have tested the code over a number of files, until you're satisfied with the result.
 
Old 04-25-2008, 08:23 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pixellany View Post
With SED:

sed '/cdfId->simInfo->test2/{n; s/)$/ extraterm)/}'
I was sure that some sed guru would have built a one-line command. Nice one, pixellany!
 
Old 04-25-2008, 09:34 AM   #7
ischi
Member
 
Registered: Apr 2008
Location: Tübingen
Distribution: Fedora 9 (Thinkpad T60), Debian 3.1 (Server)
Posts: 51

Rep: Reputation: 15
I really need to work on my sed skills ... i love wht it can do but im just to slow to figure out how ...
 
Old 04-25-2008, 09:50 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ischi View Post
I really need to work on my sed skills ... i love wht it can do but im just to slow to figure out how ...
http://www.grymoire.com/Unix/Sed.html
 
Old 04-28-2008, 04:02 AM   #9
sjreilly
Member
 
Registered: Oct 2007
Location: Edinburgh, Scotland
Distribution: Debian, CentOS, RHEL, Mint, Ubuntu
Posts: 79

Original Poster
Rep: Reputation: 15
Brilliant!
Many, many thanks!

Steve
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script text replacement... matthurne Programming 4 06-07-2011 06:46 PM
text search and replacement: bash scripting jettachamp26 Programming 35 02-25-2008 06:00 PM
Text replacement question: sed/awk/perl whatever BigRedBall Programming 6 02-05-2008 11:53 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
script help -- text replacement DavidPhillips Programming 22 09-05-2003 12:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:57 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