insert text into another file after matching pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert text into another file after matching pattern
# 1  
Old 03-29-2009
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
Code:
insert the text 

</servlet-mapping>
<!-- beechac added - for epic post-->
<servlet>
<servlet-name>EpicUpload</servlet-name>
<servlet-class>com.practicallaw.intranet.epic.EpicUploadedFileHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EpicUpload</servlet-name>
<url-pattern>/epicUpload</url-pattern>
</servlet-mapping>

into file web.xml at the beginning or end of the tag <url-pattern> (see below)

<servlet-name>Inventory</servlet-name>
<url-pattern>/Inventory/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Satellite</servlet-name>
<url-pattern>/Satellite/*</url-pattern>
</servlet-mapping>

<!-- beechac added - for epic post-->
<servlet>
<servlet-name>EpicUpload</servlet-name>
<servlet-class>com.practicallaw.intranet.epic.EpicUploadedFileHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EpicUpload</servlet-name>
<url-pattern>/epicUpload</url-pattern>
</servlet-mapping>

<mime-mapping>
<extension>chm</extension>
<mime-type>application/octet-stream</mime-type>
</mime-mapping>

# 2  
Old 03-29-2009
Keep the text to ad in a seperate file, and then perhaps something like:
Code:
if [ <condition> ]; then 
    cat <file with text> >> <destination>;
fi

# 3  
Old 03-29-2009
Else do somethign like:
Code:
sed 's/<!-- beechac added - for epic post-->/\
<servlet>\n\
<servlet-name>EpicUpload<\/servlet-name>\n\
<servlet-class>com.practicallaw.intranet.epic.EpicUploadedFileHandler<\/servlet-class>\n\
</servlet>\n\
<servlet-mapping>\n\
<servlet-name>EpicUpload<\/servlet-name>\n\
<url-pattern>/epicUpload<\/url-pattern>\n\
<\/servlet-mapping>/g'

on the file
# 4  
Old 03-30-2009
the last command is a global replace, i'm looking to do an insert after the first patterm match of </servlet-mapping> after which i insert a block of text.
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 add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

Insert value of column based on file name matching

At the top of the XYZ file, I need to insert the ABC data value of column 2 only when ABC column 1 matches the prefix XYZ file name (not the ".txt"). Is there an awk solution for this? ABC Data 0101 0.54 0102 0.48 0103 1.63 XYZ File Name 0101.txt 0102.txt 0103.txt ... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

4. Shell Programming and Scripting

Find all matching words in text according to pattern

Hello dear Unix shell professionals, I am desperately trying to get a seemingly simple logic to work. I need to extract words from a text line and save them in an array. The text can look anything like that: aaaaaaa${important}xxxxxxxx${important2}ooooooo${importantstring3}...I am handicapped... (5 Replies)
Discussion started by: Grünspanix
5 Replies

5. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

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

7. Shell Programming and Scripting

Pattern Matching and text deletion using VI

Can someone please assist me, I'm trying to get vi to remove all the occurences of the text in a file i.e. "DEVICE=/dev/mt??". The "??" represents a number variable. Is there a globel search and delete command that I can use? Thank You in Advance. (3 Replies)
Discussion started by: roadrunner
3 Replies

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

9. UNIX for Advanced & Expert Users

pattern matching with comma delimited text

Hi, I have two files that I need to match patterns with and the second file has comma delimited rows of data that match but I'm having trouble getting a script to work that gives me the match output to these sets : file 1: PADG_05255 PADG_06803 PADG_07148 PADG_02849 PADG_02886... (8 Replies)
Discussion started by: greptastic
8 Replies

10. Shell Programming and Scripting

text manipulation and pattern matching

Hi guys, I need help: I started receiving automatic emails containing download information. The problem is that these emails are coming in a rich format (I have no control of this) so the important information is buried under a bunch of mumbo-jumbo. To complicated things even further I need to... (10 Replies)
Discussion started by: caprica13
10 Replies
Login or Register to Ask a Question