Insert text after the first occurance of searched string entry in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Insert text after the first occurance of searched string entry in a file
# 1  
Old 10-30-2019
Insert text after the first occurance of searched string entry in a file

My server xml file has huge data part of which i'm sharing below.

Quote:
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/local-rsa-cert.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->

<!-- Define an AJP 1.3 Connector on port 8009 -->
......
......
I wish to add the below text held by variable "addthisline" after the closing braces i.e --> once the first </Connector> tag is found.
Code:
addthisline="I need to be inserted after the comments"

Thus my searchstring is "</Connector>" and i need keep looking for the next "-->" entry post which I need to add the line contained in $addthisline

This injection should happen only for the first instance of " </Connector>" string found in the file.

Expected Output:

Quote:
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/local-rsa-cert.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->

I need to be inserted after the comments

<!-- Define an AJP 1.3 Connector on port 8009 -->
......
......
Unfortunately, I just know how to add the text after the string is found using sed '/pattern/a some text here' filename

I'm using bash shell script on Redhat Linux 7.

Requesting suggestions.
# 2  
Old 10-30-2019
Difficult to achieve with sed as it can't keep the state of processing. This would insert the string after all occurrences:
Code:
sed -e "/<\/Connector/,/-->/{/-->/a $addthisline" -e'}' file

# 3  
Old 10-30-2019
Quote:
Originally Posted by RudiC
Difficult to achieve with sed as it can't keep the state of processing. This would insert the string after all occurrences:
Code:
sed -e "/<\/Connector/,/-->/{/-->/a $addthisline" -e'}' file

But, my requirement is to add only after the first occurance. A non sed solution will do.
# 4  
Old 10-30-2019
Try this awk code snippet, then:
Code:
awk -v"IT=${addthisline}" '/<\/Connector/,/-->/ {if (/-->/ && !L) {print; $0 = IT; L = 1}} 1' file


And, playing around with sed's hold space, I found this which might do as well:

Code:
sed -e "/<\/Connector/,/-->/{/-->/{x; s/XXX/&/; x; t; a $addthisline" -e'; x; s/.*/XXX/; x;} }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Insert folder name in text string

Hi, I am trying to organize a badly constructed web server. Basically every file is in public_html which is horrendous. There's about 50 images and 20 html files (not counting the css files and pdf docs). I am trying to update each .html file using sed based on: Where string contains *.jpg... (9 Replies)
Discussion started by: enginama
9 Replies

2. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Removing last occurance of string in text

I have text file as follows and would like to remove the last occurance of "UNION ALL" string and replace @@ with single quote ('). Input text in file is with temp as ( ( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL ( select... (9 Replies)
Discussion started by: Vaddadi
9 Replies

5. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

6. Shell Programming and Scripting

How to insert string at particular 4th line in text file

I just want to add a particular string in text file using shell script text file format 1 columns-10 2 text=89 3 no<> 4 5 test-9876 6 size=9 string need to insert in 4th line <switch IP='158.195.2.567' port='5900' user='testc' password='asdfrp' Code='8'> After inserting the... (8 Replies)
Discussion started by: puneet.goel
8 Replies

7. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

8. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. UNIX for Dummies Questions & Answers

count string occurance in a file hourly

Hi, I file that has all the status for one day (24hours). Now what I want to do is to count the occurence of a string in its output hourly like for example count occurance of successful or asynchronous clear destinon for every hour and redirect it to file. Please see sample file below. Please... (2 Replies)
Discussion started by: ayhanne
2 Replies
Login or Register to Ask a Question