LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2011, 10:43 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
sed help to insert multiple line


I'm writing a sed script to modify an Apache 2 httpd.conf file. I'm getting stumped with trying to insert:

Code:
<Location /cgis>
    Options +ExecCGI
    Order allow,deny
    Allow from all
</Location>
before
Code:
#
# Note that from
I'm trying
Code:
sed -i /^#$/{N # Note that from} i \
<Location \/cgis> \
   Options +ExecCGI\
   Order allow,deny\
   Allow from all\
<\/Location>\
\
' /etc/httpd/conf/httpd.conf
I get unknown command `<'. If I try to escape the '<', I get unterminated address regex. Can someone point me in the correct direction?

Thanks!
 
Old 11-03-2011, 11:01 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

try it this way:
Code:
sed '/^#$/{N; /# Note that from/ i \
<Location /cgis> \
   Options +ExecCGI\
   Order allow,deny\
   Allow from all\
</Location>\

}' file
As you can see, you do not need all the escape characters. Notice especially, that the last '\' is not needed. In your example it indicates that there is another line following which has to be inserted, too. Thus the
Code:
' /etc/httpd/conf/httpd.conf
is not being interpreted as part of the 'sed' command but as part that needs to be inserted. Also notice, that you need a second search pattern after your 'N' command.

Hope this helps.
 
1 members found this post helpful.
Old 11-03-2011, 12:39 PM   #3
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
Worked perfectly! Thank You!
 
  


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] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM
sed insert line action_owl Linux - Newbie 3 08-24-2009 12:06 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:22 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