Insert rows of text into a file after pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert rows of text into a file after pattern
# 1  
Old 07-21-2011
Insert rows of text into a file after pattern

SHELL=bash
OS=rhel

I have a file1 that contains text in sentences like so:

file1
Code:
this is a sentence
this is a sentence
this is a sentence

I also have a file2 like so:

file2
Code:
command="
here is some text
here is more text
again we have some text
"

I wish to echo the text from file1 into file2 after the string command="

desired result
file2
Code:
command="
this is a sentence
this is a sentence
this is a sentence
here is some text
here is more text
again we have some text
"

Any suggestions would be greatly appreciated.
# 2  
Old 07-21-2011
Code:
{ head -n1 file2; cat file1; tail -n+2 file2; }

# 3  
Old 07-21-2011
Sorry I will explain with a bit more detail.

There can be more rows of text above the string command=". So I need to match on the string command=" since this text block can be anywhere in file2. Thanks for your help.



Code:
this is another command 
so is this 


command=" here is some text 
here is more text 
again we have some text "

# 4  
Old 07-21-2011
Try:
Code:
sed '/^command="/r file1' file2

This User Gave Thanks to yazu For This Post:
# 5  
Old 07-21-2011
Thank you so much. I modified your first command to use the correct line numbers and it works.

SOLVED
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

3. Shell Programming and Scripting

Insert space or pattern between columns in a data file

I have a data file where three data sets are written in three columns. Can I increase the space between the columns without reading them? Also can I insert particular patterns, say comma between 1st and 2nd column and colon between 2nd and 3rd column? (13 Replies)
Discussion started by: hbar
13 Replies

4. Shell Programming and Scripting

Want to Insert few lines which are stored in some file before a pattern in another file

Hello, I have few lines to be inserted in file_lines_to_insert. In another file final_file, I have to add lines from above file file_lines_to_insert before a particular pattern. e.g. $ cat file_lines_to_insert => contents are abc def lkj In another file final_file, before a... (6 Replies)
Discussion started by: nehashine
6 Replies

5. Shell Programming and Scripting

sed insert text 2 lines above pattern

Hi I am trying to insert a block of text 2 lines above a pattern match using sed eg #Start of file entry { } #End of file entry new bit of text has to be put in just above the } eg #Start of file entry { New bit of text } #End of file entry (7 Replies)
Discussion started by: eeisken
7 Replies

6. Shell Programming and Scripting

How to insert/expand data/rows in a file, with rules??

Hi, I am rather new to Unix/Linus. I have this problem that I would like to solve using unix. Here is what I have start stop expression 1 5 15 2 6 10 I want a output like this position expression 1 15 2 25 3 ... (3 Replies)
Discussion started by: wanghlv
3 Replies

7. Shell Programming and Scripting

insert text into another file after matching pattern

i am not sure what i should be using but would like a simple command that is able to insert a certain block of text that i define or from another text file into a xml file after a certain match is done for e.g insert the text </servlet-mapping> <!-- beechac added - for epic post-->... (3 Replies)
Discussion started by: cookie23patel
3 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies
Login or Register to Ask a Question