Replace text in a file

Reply

Join Date: Sep 2006
Posts: 3
Reputation: toztech is an unknown quantity at this point 
Solved Threads: 1
toztech toztech is offline Offline
Newbie Poster

Replace text in a file

 
0
  #1
Sep 7th, 2006
Greetings,

Hey guys, I am trying to make a shell script that will scan for a certain 5 lines of text in a file, modify it by adding two lines in the middle and replacing that section of text in the file.

For example read rc.conf for:
Line1
Line2
Line3
Line4
Line5
That match the exact lines

and replace that section or match with

Line1
Line2
INSERTLINE
Line3
INSERTLINE
Line4
Line5

Mainly i am trying to add a new case statment inside an if loop automatically using a shell script. I have tried reading info on awk usage but cannot find anything that matches whta I am trying to accomplish. Any help would be greatly appreciated.

Regards,

S Tompkins
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Replace text in a file

 
0
  #2
Sep 7th, 2006
Here's one way of approaching your problem (pseduo-code):
Shell Scripting Syntax (Toggle Plain Text)
  1. while !eof {
  2. increment fileline
  3.  
  4. while (stillgoing) {
  5. increment index
  6. read line fileline from file
  7. if (compare string to one in database = FALSE)
  8. stillgoing = FALSE
  9. if (index > 5)
  10. insert lines in file
  11. } end while
  12. } end while

Just off the top of my head. Don't take it too literally.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Replace text in a file

 
0
  #3
Sep 8th, 2006
awk works for this pretty well -
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{
  2. print $0
  3. if($0=="Line2") {print "INSERTLINE2"}
  4. if($0=="Line3") {print "INSERTLINE3"}
  5. }' file > newfile
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 30
Reputation: sut is an unknown quantity at this point 
Solved Threads: 1
sut sut is offline Offline
Light Poster

Re: Replace text in a file

 
0
  #4
Sep 11th, 2006
Shell Scripting Syntax (Toggle Plain Text)
  1. sed '
  2. /Line1/{
  3. N;N;N;N
  4. s/^Line1\nLine2\nLine3\nLine4\nLine5$/Line1\
  5. Line2\
  6. INSERTLINE1\
  7. Line3\
  8. INSERTLINE2\
  9. Line4\
  10. Line5\
  11. /
  12. }' rc.conf
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC