Add line after AND before two patterns like patch do


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add line after AND before two patterns like patch do
# 1  
Old 11-04-2012
Add line after AND before two patterns like patch do

Hi all, I kwow there are many ways to insert string into a files all over the forum, but I need to make that process like patch do because in the same file I can have the same pattern in many locations, for that I need the send two patterns, one for "before" and another for the "after".

I cannot be capable to do it with patch and diff because the original file to be patched can be modified by other scripts after doing the patch file with diff.

So that is the example:

I have "file" with that content:
Code:
hello
hello
second
hello
patern
hello
hello
pattern
second
hello
hello
second

And I want to add the content of another file called "toadd" with that content:

Code:
ADDED

And I need the instert between the word "pattern" and "second". But only between them and have that result in the "file":
Code:
hello
hello
second
hello
patern
hello
hello
pattern
ADDED
second
hello
hello
second

Thanks for help

Last edited by neutralTTY; 11-04-2012 at 08:04 AM.. Reason: typos
# 2  
Old 11-04-2012
Hi

Code:
sed  '/pattern/{N;/\nsecond/s/\n/&ADDED&/}' file

Guru.
# 3  
Old 11-04-2012
Thanks, it's just working fine.

And using ed instead of sed?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

2. Shell Programming and Scripting

Matched multiple patterns that could be in a same line

Hi, I need help to match pattern started with "RW" in file 1 and with pattern in $1 in file 2 as follows:- File 1 BH /TOTAL=466(423); /POSITIVE=300(257); /UNKNOWN=25(25); BH /F_P=141(141); /F_N=136; /P=4; CC /TAX=!?; /MAX-R=2; CC /VER=2; RW P9610, AR_BSU , T; PAE25, AE_E57... (10 Replies)
Discussion started by: redse171
10 Replies

3. Shell Programming and Scripting

Extract line between two patterns

Hi All, I need a script to extract a lines between two patterns.I have done this using grep,cut,tail and head.But its very slow, because my input file contain more than a lakh. COMMAND:XXXXXXXXXXXXXXXXXXXX yyyyy zzzzzz REQUESTSTRING:aaaaaaaaaaaaaaa;11111 222222 333333... (4 Replies)
Discussion started by: rajamohan
4 Replies

4. Shell Programming and Scripting

comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude. File contents. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 Replies

5. Shell Programming and Scripting

print the next line by searching with different patterns

Hi, I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search. Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which... (5 Replies)
Discussion started by: tejastrikez
5 Replies

6. Shell Programming and Scripting

Help with extracting a part of a line between two patterns

Hello All, I have a text file with contents as below: contents of error.txt: message1="Reason for error code1" message2="Reason for error code2" message3="Reason for error code3. To solve this, you may try doing restart" I have a requirement where in I have to... (4 Replies)
Discussion started by: asterisk-ix_use
4 Replies

7. Shell Programming and Scripting

Grep between 2 patterns in a line

Hi I have a file containing 1000+ lines of netlist data. I need to search for text between two key words on each line for e.g my input file has "ABC.ABC__312.deftr_0.X143.Xntys_0.\Xetabc__ABC_test_tz .X1023" "ABC.ABC__312.asjartwtsj"... (16 Replies)
Discussion started by: naveen@
16 Replies

8. Shell Programming and Scripting

awk gsub with 2 patterns on one line

I want to substitue 2 patterns with awk in one line. The line looks like this: <option> Nick Gilder - Hot child in the city </option> I want to remove the <option> </option> fields I try this, but it removes only the first pattern: awk '{gsub(/^+|+$/,"");print}' nowplaying.txt How... (2 Replies)
Discussion started by: sdohn
2 Replies

9. UNIX for Advanced & Expert Users

3 patterns in one line

hello, I want to write a script to find all the files that contain 3 specific patterns. example: shows the files containing any line that contain pattern1, pattern2 and pattern3, but the patterns can be in any order as long as they exist in the line. can I do that with grep? thank you (1 Reply)
Discussion started by: bashuser
1 Replies

10. UNIX for Dummies Questions & Answers

searching for two or more patterns in a line

how can I search for two or more patterns in one line using grep? for example if I want to show only the lines that have patterns "abc" and "123"? and what if I want to show only the lines that have either "abc" or "123" in them? any hint apprecited (4 Replies)
Discussion started by: metalwarrior
4 Replies
Login or Register to Ask a Question