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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to Insert few lines which are stored in some file before a pattern in another file
# 1  
Old 12-12-2012
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.
Code:
$ cat file_lines_to_insert => contents are
abc
def
lkj


In another file final_file, before a pattern "PIN (new)", I want to insert above file contents.

Pl. tell me how to do it with sed command if possible.

Last edited by Scrutinizer; 12-13-2012 at 03:28 AM.. Reason: code tags
# 2  
Old 12-12-2012
Unfortunately sed's r command only appends. If your pattern doesn't appear on the first line (in which case you can just cat the insert file before the final file):
Code:
x=$(sed '/PIN (new)/!d;=;d;q' final-file)
# check if x has value and not 1
# ...
sed "$((x-1))r inser-file" final-file

# 3  
Old 12-12-2012
Code:
awk 'NR==FNR{a=a==""?$0:a RS $0;next}
      /PIN/{print a}1' file_lines_to_insert final_file

# 4  
Old 12-13-2012
Hello Binlib,
I got your idea & now i am fetching line no and reducing that to 1 and then running sed command. its working fine...
thanks!!!

Hello dcwayx,
I could not get awk command posted by you, can you pl. explain??
# 5  
Old 12-13-2012
Alternate Sed..
Code:
sed -e '/PIN (new)/r file_lines_to_insert' -e 'x' final_file

# 6  
Old 12-15-2012
Hi Michael, can you please explain what it is doing specially section -e 'x' ??
# 7  
Old 12-15-2012
Code:
'/PIN (new)/r file_lines_to_insert'  => When this pattern matches lines from file_lines_to_insert in inserted.

Code:
'x' => This command helps to switch the content of pattern buffer with hold buffer. So when 1st line is read 'x' puts the 1st line to hold buffer 
       and pattern buffer takes a new-line char, after this switch what ever there is in pattern buffer is printed, that's why a new line is printed
       first in the output.
       Now 2nd line is read, 'x' puts 2nd line into hold buffer and line 1 which is already in hold buffer comes to pattern buffer and line 1 now gets printed. 
       This action repeats for all the remaining lines.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string with lines stored in a file

OS version: RHEL 6.7 Shell : Bash I have a file like below $ cat pattern.txt 'T_PKT_HEADER' 'T_ORD_ITM_LOYX' 'T_ORDERITM_TRMS' 'T_ORDER_ITEM' 'T_ORDER_ITM_PRI' 'T_ORDER_ITEM_OM' 'T_ORDER_ITEM_XA' 'T_ORDER_ATT' 'T_ORDER_ACTNSET' 'T_ORDER_XM' 'T_ORDER_X' 'T_ORDER_TNTX'... (7 Replies)
Discussion started by: kraljic
7 Replies

2. Shell Programming and Scripting

Insert content of a file right after pattern in another file

suppose i have original file: original.txt: hello how are you you are wonderful what time is it I went to the store last night. and some apple juice then i have another file: anotherfile.txt: with my friends mary, john and harry. We had a great time. We bought food Suppose... (1 Reply)
Discussion started by: SkySmart
1 Replies

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

4. Shell Programming and Scripting

Insert content of a file into another file before given pattern

I need to insert file x2 into x1 right before first BBB line. $ cat x1 AAA 1 AAA 2 AAA 3 BBB 1 BBB 2 BBB 3 $ cat x2 XXX - insert 1 XXX - insert 2 I need to get AAA 1 AAA 2 AAA 3 XXX - insert 1 XXX - insert 2 BBB 1 (2 Replies)
Discussion started by: migurus
2 Replies

5. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Removing md5sum lines stored in text file

Hello. I'm writing a script where every file you create will generate a md5sum and store it into a text file. Say I create 2 files, it'll look like this in the text file: d41d8cd98f00b204e9800998ecf8427e /helloworld/saystheman d41d8cd98f00b204e9800998ecf8427e /helloworld/test I... (3 Replies)
Discussion started by: batarangs_
3 Replies

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

8. UNIX for Dummies Questions & Answers

Searching for a pattern from filenames stored in a file

Hi, I have got some 10 filenames stored in a file or displayed in the console as a result of some query i made.. Now I need to open each of these files and search for a pattern in these 10 files.. Can someone help me with this? Thanks, Jean (9 Replies)
Discussion started by: jeanjkj
9 Replies

9. Shell Programming and Scripting

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 this is a sentence this is a sentence this is a sentence I also have a file2 like so: file2 command=" here is some text here is more text again we have some text " I wish to echo the text from... (4 Replies)
Discussion started by: jaysunn
4 Replies

10. 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
Login or Register to Ask a Question