| | |
Replace text in a file
![]() |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 1
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
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
Here's one way of approaching your problem (pseduo-code):
Just off the top of my head. Don't take it too literally.
Shell Scripting Syntax (Toggle Plain Text)
while !eof { increment fileline while (stillgoing) { increment index read line fileline from file if (compare string to one in database = FALSE) stillgoing = FALSE if (index > 5) insert lines in file } end while } 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."
•
•
Join Date: May 2004
Posts: 177
Reputation:
Solved Threads: 10
awk works for this pretty well -
Shell Scripting Syntax (Toggle Plain Text)
awk '{ print $0 if($0=="Line2") {print "INSERTLINE2"} if($0=="Line3") {print "INSERTLINE3"} }' file > newfile
•
•
Join Date: Sep 2006
Posts: 30
Reputation:
Solved Threads: 1
Shell Scripting Syntax (Toggle Plain Text)
sed ' /Line1/{ N;N;N;N s/^Line1\nLine2\nLine3\nLine4\nLine5$/Line1\ Line2\ INSERTLINE1\ Line3\ INSERTLINE2\ Line4\ Line5\ / }' rc.conf
![]() |
Similar Threads
- Replace words in a file (Python)
- connect to text file database (Visual Basic 4 / 5 / 6)
- Please anybody to help to read string value in text file using C# (C#)
- write to text file using fprintf() (C)
- Store Bluetooth remote address to a text file (C++)
- Read and write to an ASCII Text file (Java)
Other Threads in the Shell Scripting Forum
- Previous Thread: String Match Comparison
- Next Thread: Mail command
| Thread Tools | Search this Thread |






