Replacing certain lines with another set of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing certain lines with another set of lines
# 1  
Old 04-13-2012
Replacing certain lines with another set of lines

i have a huge script that i need to replace a bunch of lines in.

in that script, i have a chunk similar to:

Code:
JABABA=$(if [ afaf = fafaf ]; then

echo afafafa

afafafafaf

afafafafe

fdfdfdfadfadl

fafafafkkkk

fi)

I would like to replace the above chunk with:

Code:
JABABA=$(if [ afaf = sweef ]; then

echo blah blach

echo blah

echo blah

echo blah

echo blah

fi)

i attempted to do this manually but there are just too many instances of the the chunk i want to replace.

any ideas?
# 2  
Old 04-13-2012
It would be better so see the example you are working on. For me personally, this is just a bit abstract. By all means, sanitise it to remove anything sensitive, but it would be useful to know.

If you are simply looking for a vi command to replace strings in a specific range of rows in a file (i.e. your script) then if you place the cursor at the start line of the range and press CNTL-G, you will get the line number displayed, e.g. line 123. Do the same at the end and let's say that tells you it's line 456. You can then issue something like:-
Code:
:123,456 s/searchstring/replacestring/

This will replace between lines 123 to 456 inclusive the first occurence on each line. If you want every occurrence on each line, then it's:-
Code:
:123,456 s/searchstring/replacestring/g


I hope that this helps, but if I've missed the point, please explain more of what you want to acheive and show us the source.

Is the script beyond a re-design where you write this as a function and pass in the parameters to test/respond with?


Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 04-13-2012 at 10:44 AM.. Reason: Include the re-design question
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

2. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

3. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

4. Shell Programming and Scripting

Replacing specific lines with another lines

Hi, I have a file with many lines, then i have following list of lines(line number 5,12,19,5,and 28) i need to replace these lines of a file with another lines as shown below these text contains special charecter like= (/:;){} Line_number Text to replace with 5 abc... (1 Reply)
Discussion started by: MILAN KUMAR
1 Replies

5. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. Shell Programming and Scripting

replacing lines of code

I have a file that has a lot of sets of a few lines of code I need to replace start keyword1 stuff keyword2 end important stuff in between start keyword1 stuff keyword3 end I want to replace each set of code (from start, to end, with a single line, call this finalcode) I am... (2 Replies)
Discussion started by: vanessafan99
2 Replies

8. Shell Programming and Scripting

replacing blank lines

Hi i am trying to replace blank lines with a number 0. I tried the following code awk '{print NF ? $0: blankrow}' blankrow = "0" file1.prg>file2.prg however i get the following error: fatal: cannot open file `blankrow' for reading (No such file or directory) file example: 1 2 3 5 6... (11 Replies)
Discussion started by: rockiefx
11 Replies

9. Shell Programming and Scripting

replacing new lines in all files of a directory containing old lines

Hi all, I am trying to replace a few lines with other lines of all files in a directory which contain those few lines. say - there are some 10 files in a dir having the same 4 lines as 1.txt at the starting 1.txt line 1 line 2 line 3 line 4 ....................................... (1 Reply)
Discussion started by: rooster005
1 Replies

10. Shell Programming and Scripting

replacing multiple lines

i have a file : sample1.txt OBJECT="POINT" ACTION="REDEFINE" POINT_NAME="ABCD001G " GHYT_POPRIORITY_1="1" GHYT_POPRIORITY_2="1" GHYT_POPRIORITY_3="1" GHYT_POPRIORITY_4="1" GHYT_POPRIORITY_USER="1" HIGH_ALARM_PRIORITY_1="1" HIGH_ALARM_PRIORITY_2="1" HIGH_ALARM_PRIORITY_3="1" ... (1 Reply)
Discussion started by: ajnabi
1 Replies
Login or Register to Ask a Question