LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-14-2020, 09:57 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 347

Rep: Reputation: 60
sed question


I want to append a line after a particular set of lines in httpd.conf. There are multiple lines that match the one string, but if I add in the previous line, I can find the correct location with:
Code:
sed -nE '{N; /\s+#\s+Require all granted/p }' httpd.conf
gives me:
Code:
    #
    Require all granted
Now I want to add some lines after that, but not getting it to work. Any suggestions? I want to add: AllowMethods GET

Last edited by bradvan; 02-14-2020 at 10:14 AM.
 
Old 02-14-2020, 10:19 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,845

Rep: Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629
Quote:
Originally Posted by bradvan View Post
I want to append a line after a particular set of lines in httpd.conf. There are multiple lines that match the one string, but if I add in the previous line, I can find the correct location with:
Code:
sed -nE '{N; /\s+#\s+Require all granted/{=;p} ; D}' httpd.conf
gives me:
Code:
156
    #
    Require all granted
Now I want to add some lines after that, but not getting it to work. Any suggestions? I want to add: AllowMethods GET
You can use a to append what you want:
Code:
sed -nr 'N; /\s+#\s+Require all granted/{=;p;a AllowMethods GET
};D' conf
Notice, that everything that comes after a will be appended, i.e., you need a new line to continue the sed script.

On a side note, I do not know your exact requirement, but your script will only matches the first 'Require ...' in this snippet:
Code:
    #
    Require all granted

    #
    Require other granted

    #
    Require yet another granted
 
2 members found this post helpful.
Old 02-14-2020, 10:41 AM   #3
bradvan
Member
 
Registered: Mar 2009
Posts: 347

Original Poster
Rep: Reputation: 60
Thanks! In the httpd.conf file that I have, of the three matches, only one is preceded by a blank comment and that is the one I wish to append to. So, got this to work:
Code:
sed -i -r 'N; /\s+#\s+Require all granted/ a \
    AllowedMethods GET ' httpd.conf
which results in:
Code:
    #
    Require all granted
    AllowedMethods GET
which is what I want. Thanks!

Last edited by bradvan; 02-14-2020 at 10:46 AM.
 
Old 02-14-2020, 10:46 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,622

Rep: Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369
just need to check somehow if that line was already there.
 
Old 02-15-2020, 10:56 AM   #5
bradvan
Member
 
Registered: Mar 2009
Posts: 347

Original Poster
Rep: Reputation: 60
A simple grep for AllowedMethods. I only need it in once.
 
Old 02-15-2020, 01:13 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,845

Rep: Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629
Quote:
Originally Posted by bradvan View Post
Thanks! In the httpd.conf file that I have, of the three matches, only one is preceded by a blank comment and that is the one I wish to append to. So, got this to work:
Code:
sed -i -r 'N; /\s+#\s+Require all granted/ a \
    AllowedMethods GET ' httpd.conf
which results in:
Code:
    #
    Require all granted
    AllowedMethods GET
which is what I want. Thanks!
I just noticed that you removed the command 'D' from your sed. If you do this then you will only match on even lines, i.e., this will match:
Code:
$ cat sample.conf
    #
    Require all granted # line number 2
This, however will not match because the 'Require ...' part is on an odd line:
Code:
$ cat sample.conf
    An empty line
    #
    Require all granted # line number 3
So you may want to add back the command 'D' to delete up to the first newline.
 
2 members found this post helpful.
Old 02-16-2020, 03:33 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,622

Rep: Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369
Quote:
Originally Posted by bradvan View Post
A simple grep for AllowedMethods. I only need it in once.
And what if There were multiple lines that may match that string
 
Old 02-17-2020, 06:24 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,294

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
The O/P said it can only occur once.

Regarding the D command, this is a good point. But it deletes (up to an embedded \n) so it should be rather P;D
Print then delete!

Last edited by MadeInGermany; 02-17-2020 at 06:25 PM.
 
1 members found this post helpful.
Old 02-17-2020, 07:03 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,845

Rep: Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629
Quote:
Originally Posted by MadeInGermany View Post
Regarding the D command, this is a good point. But it deletes (up to an embedded \n) so it should be rather P;D
Print then delete!
Good catch! I only tested with a hastily improvised dataset, focused on identifying the line in question and totally missed that.
 
Old 02-18-2020, 02:49 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,622

Rep: Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369
Quote:
Originally Posted by MadeInGermany View Post
The O/P said it can only occur once.
As long as the script/sed was not executed.
I would rather suggest a solution which can be executed several times without problems...
 
Old 02-18-2020, 04:23 AM   #11
bradvan
Member
 
Registered: Mar 2009
Posts: 347

Original Poster
Rep: Reputation: 60
Argh! It doesn't work if I remove a blank line above it! Note: this is a script/set of instructions for setting up a kickstart server. So, this will only get run at the time of setting it up. The stock httpd.conf file does not have any AllowMethods statements in it. So a simple grep for that phrase will suffice to test whether or not to run the sed. Now, I can't quite get the sed to run with that blank line removed. Can't quite figure out how to drop the line from the pattern space. This works for the odd number of lines:
Code:
sed -i -r 'N; /\s+#\s+Require all granted/ a \
    AllowedMethods GET ' httpd.conf
(Also note that I am trying to edit the line; earlier I was just doing a search to test out the matching.) If it is easier to not edit in place, I can always send to a dummy file then move it back.
 
Old 02-18-2020, 02:57 PM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,845

Rep: Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629Reputation: 629
Quote:
Originally Posted by bradvan View Post
Argh! It doesn't work if I remove a blank line above it! Note: this is a script/set of instructions for setting up a kickstart server. So, this will only get run at the time of setting it up. The stock httpd.conf file does not have any AllowMethods statements in it. So a simple grep for that phrase will suffice to test whether or not to run the sed. Now, I can't quite get the sed to run with that blank line removed. Can't quite figure out how to drop the line from the pattern space. This works for the odd number of lines:
Code:
sed -i -r 'N; /\s+#\s+Require all granted/ a \
    AllowedMethods GET ' httpd.conf
(Also note that I am trying to edit the line; earlier I was just doing a search to test out the matching.) If it is easier to not edit in place, I can always send to a dummy file then move it back.
Yes, that is what I meant in my post #6. Try this:
Code:
sed -r 'N; /\s+#\s+Require all granted/{a \
    AllowMethods GET
b;};P;D' conf
Remember, that only a newline can terminate the a command inside the sed script.

PS: The -i switch should not affect the result, so just adding once you are satisfied should be fine.

Last edited by crts; 02-18-2020 at 03:10 PM. Reason: Added PS
 
1 members found this post helpful.
Old 02-18-2020, 04:25 PM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,294

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
Another solution: only run N command when the first line was met:
Code:
sed -r '/\s+#\s*$/{
    N; s/(\n\s*)Require all granted/\1AllowedMethods GET&/
  }' httpd.conf
The 2nd line is done by an s command. It allows to "inherit" the leading spaces.
The & is what matched before. So you can easily switch the order:
Code:
sed -r '/\s+#\s*$/{
    N; s/(\n\s*)Require all granted/&\1AllowedMethods GET/
  }' httpd.conf

Last edited by MadeInGermany; 02-18-2020 at 04:30 PM.
 
1 members found this post helpful.
Old 02-19-2020, 03:57 AM   #14
bradvan
Member
 
Registered: Mar 2009
Posts: 347

Original Poster
Rep: Reputation: 60
Thanks! That worked great! I think I'm all set.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] head -n -1 (sed -n -e '1,$-1p' is incorrect) sed question, as stupid as it could be. kaz2100 Programming 1 12-27-2011 10:18 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:20 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration