LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 08:57 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
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 09:14 AM.
 
Old 02-14-2020, 09:19 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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, 09:41 AM   #3
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
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 09:46 AM.
 
Old 02-14-2020, 09:46 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

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

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

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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, 02:33 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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, 05:24 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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 05:25 PM.
 
1 members found this post helpful.
Old 02-17-2020, 06:03 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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, 01:49 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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, 03:23 AM   #11
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
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, 01:57 PM   #12
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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 02:10 PM. Reason: Added PS
 
1 members found this post helpful.
Old 02-18-2020, 03:25 PM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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 03:30 PM.
 
1 members found this post helpful.
Old 02-19-2020, 02:57 AM   #14
bradvan
Member
 
Registered: Mar 2009
Posts: 367

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


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 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 09:18 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

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

All times are GMT -5. The time now is 06:42 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
Open Source Consulting | Domain Registration