replacing lines of code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing lines of code
# 1  
Old 08-04-2011
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 now using:
perl -i -p0e 's/start.*?keyword1.*?end/finalcode/s' file
perl -i -p0e 's/start.*?keyword2.*?end/finalcode/s' file
perl -i -p0e 's/start.*?keyword3.*?end/finalcode/s' file

but the problem is that the sets of keywords can occur in different order
and they have a similar start line

so if the lines with keyword2 occur first in the file, and there's also a keyword1 set, then it will take the start from keyword2, and wipe the important stuff in between

basically i want each set of code to be replaced by a line of code.
these sets of codes have similar start/ending lines

but i dont know the command to do this
like, when i do:
perl -i -p0e 's/start.*?keyword1.*?end/finalcode/s' file

the keyword should only be 1 line from start, not further down the file (which would mean its in a different line)

im not sure if im explaining this well
thanks for any help!!

---------- Post updated at 01:30 AM ---------- Previous update was at 01:13 AM ----------

or another way to check, when i check for start and keyword1 in the pattern, how can i check that they are close together (e.g., within 50 characters, or 2 lines)? otherwise it does not match the pattern and it should not do the replace
# 2  
Old 08-04-2011
What output do you expect from this?
Code:
start
keyword1
stuff
keyword2
end

important stuff in between

start
keyword1
stuff
keyword3
end

# 3  
Old 08-04-2011
Quote:
im not sure if im explaining this well
I'm not sure too. Smilie
Quote:
or another way to check, when i check for start and keyword1 in the pattern, how can i check that they are close together (e.g., within 50 characters, or 2 lines)? otherwise it does not match the pattern and it should not do the replace
Study this example:
Code:
perl -00 -ne '/start(.*?)keyword2.*end/s 
    && print length($1), " ", $1 =~ tr/\n//, "\n" 
' INPUTFILE

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

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: JABABA=$(if ; then echo afafafa afafafafaf afafafafe fdfdfdfadfadl fafafafkkkk fi) I would like to replace the above chunk with: (1 Reply)
Discussion started by: SkySmart
1 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

replacing specific lines in a file

Hi there I have a file which has the lines # Serial number for hostid EXP_SERIAL_="" These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with # Serial number for hostid $var1 EXP_SERIAL_$var1="$var2" Is there a quick and simple... (6 Replies)
Discussion started by: hcclnoodles
6 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