Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.