LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2015, 07:01 AM   #1
shajay12
LQ Newbie
 
Registered: Jan 2009
Posts: 28

Rep: Reputation: 0
Linux append lines in a file after matched lines are found


I want to append lines after a match in a file.

##file name is ssl.conf
##match is this

<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>

after above line i need to append these lines

<Directory "/">
SSLRenegBufferSize 26215000
</Directory>

so final results should be like this

<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>

<Directory "/">
SSLRenegBufferSize 26215000
</Directory>

######Thank You in Advance
 
Old 02-24-2015, 07:39 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
This can be done with sed
Code:
sed 's:</Directory>$:&\n\n<Directory "/">\nSSLRenegBufferSize 26215000\n</Directory>\n:g' inputfile
As it may not be obvious, here is some explanation.
The substitute option to sed is delimited with colons to avoid having to escape the forward slashes.
The </Directory>$ specifies the line to be matched has </Directory> at the end of the line.
The &\n\n<Directory "/">\nSSLRenegBufferSize 26215000\n</Directory>\n specifies that the replacement string is that which was matched (&) followed by your required replacement interspersed with new lines (\n) as necessary.
The trailing g specifies a global replacement in the file.

If this is what you want, redirect the output from the command to a file.
 
Old 02-24-2015, 11:31 PM   #3
shajay12
LQ Newbie
 
Registered: Jan 2009
Posts: 28

Original Poster
Rep: Reputation: 0
Thank You for the reply.

I only want to append , I do not want to substitute.

so final output in file should be

<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>

<Directory "/">
SSLRenegBufferSize 26215000
</Directory>

####################################################

Quote:
Originally Posted by allend View Post
This can be done with sed
Code:
sed 's:</Directory>$:&\n\n<Directory "/">\nSSLRenegBufferSize 26215000\n</Directory>\n:g' inputfile
As it may not be obvious, here is some explanation.
The substitute option to sed is delimited with colons to avoid having to escape the forward slashes.
The </Directory>$ specifies the line to be matched has </Directory> at the end of the line.
The &\n\n<Directory "/">\nSSLRenegBufferSize 26215000\n</Directory>\n specifies that the replacement string is that which was matched (&) followed by your required replacement interspersed with new lines (\n) as necessary.
The trailing g specifies a global replacement in the file.

If this is what you want, redirect the output from the command to a file.
 
Old 02-25-2015, 04:08 AM   #4
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,504

Rep: Reputation: Disabled
Use an editor such as vi, which will let you search for the line you want, then add in what you need to.

Edit: See man vi

Last edited by fatmac; 02-25-2015 at 04:09 AM.
 
Old 02-25-2015, 06:59 AM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
If the suggested sed solution does not suit you, then this will do a simple append.
Code:
echo -e "\n<Directory \"/\">\nSSLRenegBufferSize 26215000\n</Directory>\n" >> ssl.conf
Note the use of the -e option to echo and the escaping of the double quote characters.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Remove all the matched lines from second file Almaz Linux - Newbie 3 07-28-2014 01:52 PM
SED append to a specific range of lines of a file Gener@l Programming 6 04-24-2013 06:56 PM
[SOLVED] Shell Script to replace specific columns on matched lines axl718 Programming 18 01-31-2013 06:12 PM
[SOLVED] sed and how to remove all lines after matched pattern transmutated Programming 5 06-13-2012 07:54 AM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration