Replacing text between two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text between two patterns
# 1  
Old 05-28-2012
Replacing text between two patterns

I would like to replace ], with ]]], between /* SECTION2-BEGIN */ and /* SECTION2-END */ in my file. My file contains the following information:

Code:
/* SECTION1-BEGIN */
                      ['example',    'example.txt'],
/* SECTION1-END */

/* SECTION2-BEGIN */
                      ['test1',      'show.txt'],
/* SECTION2-END */

/* SECTION3-BEGIN */
                      ['random',     'gone.html'],
/* SECTION3-END */

The new changed text would look like:
Code:
/* SECTION2-BEGIN */
                      ['test1',      'show.txt']]],
/* SECTION2-END */

# 2  
Old 05-28-2012
Code:
perl -pe 'if(/SECTION2-BEGIN/../SECTION2-END/){s/\]/]]]/g}' file

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 05-28-2012
How about:
Code:
sed '/SECTION2-BEGIN/,/SECTION2-END/s/]/]]]/g' infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-28-2012
Now I can't figure out how to replace the last ], with ]]],

Code:
/* SECTION1-BEGIN */
                      ['example',    'example.txt'],
/* SECTION1-END */

/* SECTION2-BEGIN */
                      ['test1',      'show.txt'],
/* SECTION2-END */

/* SECTION3-BEGIN */
                      ['random',     'gone.html'],
/* SECTION3-END */

The changed code should look like this:

Code:
/* SECTION1-BEGIN */
                      ['example',    'example.txt'],
/* SECTION1-END */

/* SECTION2-BEGIN */
                      ['test1',      'show.txt'],
/* SECTION2-END */

/* SECTION3-BEGIN */
                      ['random',     'gone.html']]],
/* SECTION3-END */

# 5  
Old 05-28-2012
How about 2 -> 3
# 6  
Old 05-29-2012
** Removed ** See issue below

---------- Post updated at 04:23 AM ---------- Previous update was at 04:21 AM ----------

So this works well for changing a single closing bracket to three closing brackets between 2 specified strings in a file:

Code:
sed -i test -e '/TOOLS-BEGIN/,/TOOLS-END/s/]/]]]/g'

I have a new issue once again. What I would like to do now is change the 5th occurrence of a single closing bracket to three closing brackets. Starting from for example SECTION2-END until lets say SECTION15-END. So the 5th bracket would be changed to ]]]

Thank you for any help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

2. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

3. Shell Programming and Scripting

Replace multiple patterns together with retaining the text in between

Hi Team I have the following text in one of the file j1738-abc-system_id(in.value1)-2838 G566-deF-system_id(in.value2)-7489 I want to remove system_id(...) combination completely The output should look like this j1738-abc-in.value1-2838 G566-deF-in.value2-7489 Any help is appreciated... (4 Replies)
Discussion started by: Thierry Henry
4 Replies

4. Shell Programming and Scripting

Find patterns and filter the text

I need to filter the text in between two patterns and output that to a different file. Please help me how to do it. Ex: ............. <some random text> ............. Pattern_1 <Few lines that need to be output to different file> Pattern_2 ................ ............... <more text in... (4 Replies)
Discussion started by: metturr
4 Replies

5. Shell Programming and Scripting

Need to extract text repetitively between two patterns

Hi All, I want to extract the text between some pattern which occurs repeatedly in a file. For example my input is like, /home/..... ..........java:25: cannot find symbol ............ /home/...... /home/....... I want to display... (2 Replies)
Discussion started by: Vignesh58
2 Replies

6. Shell Programming and Scripting

Extracting a set of patterns from the text file

Hi experts, I need a help in extracting a set of patterns from the text file. Below is my scenario. Input file: I need to extract the data between My output should be as Thanks, Kalai (7 Replies)
Discussion started by: kalpeer
7 Replies

7. Shell Programming and Scripting

Replacing patterns in a file

Hi Everybody, Can we replace the contents of a file without using a temporary file. For ex: I need to relpace the value "old" with "new", I can do it using the sed replace pattern, but I do not want to use a temporary file. Also, from the questions posted in the past I could see a... (3 Replies)
Discussion started by: mr_manii
3 Replies

8. UNIX for Dummies Questions & Answers

Copying Text between two unique text patterns

Dear Colleagues: I have .rtf files of a collection of newspaper articles. Each newspaper article starts with a variation of the phrase "Document * of 20" and is separated from the next article with the character string "===================" I would like to be able to take the text composing... (3 Replies)
Discussion started by: spindoctor
3 Replies

9. Shell Programming and Scripting

Replacing Text in Text file

Hi Guys, I am needing some help writing a shell script to replace the following in a text file /opt/was/apps/was61 with some other path eg /usr/blan/blah/blah. I know that i can do it using sed or perl but just having difficulty writing the escape characters for it All Help... (3 Replies)
Discussion started by: cgilchrist
3 Replies
Login or Register to Ask a Question