Script to add a block in a few hundred files
Clash Royale CLAN TAG#URR8PPP
I want to add the following lines
if ($http_user_agent ~* "somewebsite" )
return 444;
to all my vhosts in NGINX as follows
server
location /
want_to_add_those_3_lines_here
Any ideas?
linux text-processing sed nginx
add a comment |
I want to add the following lines
if ($http_user_agent ~* "somewebsite" )
return 444;
to all my vhosts in NGINX as follows
server
location /
want_to_add_those_3_lines_here
Any ideas?
linux text-processing sed nginx
Use aninclude
statement.
– Richard Smith
Feb 6 at 13:58
add a comment |
I want to add the following lines
if ($http_user_agent ~* "somewebsite" )
return 444;
to all my vhosts in NGINX as follows
server
location /
want_to_add_those_3_lines_here
Any ideas?
linux text-processing sed nginx
I want to add the following lines
if ($http_user_agent ~* "somewebsite" )
return 444;
to all my vhosts in NGINX as follows
server
location /
want_to_add_those_3_lines_here
Any ideas?
linux text-processing sed nginx
linux text-processing sed nginx
edited Feb 7 at 15:26
Jürgen
192110
192110
asked Feb 6 at 11:30
JodiL79JodiL79
111
111
Use aninclude
statement.
– Richard Smith
Feb 6 at 13:58
add a comment |
Use aninclude
statement.
– Richard Smith
Feb 6 at 13:58
Use an
include
statement.– Richard Smith
Feb 6 at 13:58
Use an
include
statement.– Richard Smith
Feb 6 at 13:58
add a comment |
1 Answer
1
active
oldest
votes
Using GNU sed
This task can be can be done with sed.
However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.
The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.
But the most helpful tool in this context is the -z
option that GNU sed offers and that we take great benefit of below.
You can tell whether your version of sed is a GNU implementation by running
sed --version
Turning a multi-line into a single-line problem
With the -z
option that GNU sed offers, you have a choice of what sed thinks is a line.
The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z
option, sed considers lines to be NULL byte terminated instead.
It is quite likely that sed -z
will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.
Solution
Let's chose sed-script
as the name of the following script:
#!/bin/bash
pattern="
(
server n
location / n
)(
n
)
"
replacement="
if ($http_user_agent ~* "somewebsite" ) n
return 444;n
n
"
sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"
If you make this script executable and call it like this:
./sed-script INFILE OUTFILE
it will process INFILE
looking for every occurrence of
server
location /
replaces it by
server
location /
if ($http_user_agent ~* "somewebsite" )
return 444;
and puts the result into OUTFILE
.
The only reason why we use a script here is to keep the structure of the sed command clear.
Since the search pattern contains a slash (/
), an exclamation mark (!
) is used to delimit the fields of the sed s command.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499023%2fscript-to-add-a-block-in-a-few-hundred-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using GNU sed
This task can be can be done with sed.
However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.
The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.
But the most helpful tool in this context is the -z
option that GNU sed offers and that we take great benefit of below.
You can tell whether your version of sed is a GNU implementation by running
sed --version
Turning a multi-line into a single-line problem
With the -z
option that GNU sed offers, you have a choice of what sed thinks is a line.
The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z
option, sed considers lines to be NULL byte terminated instead.
It is quite likely that sed -z
will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.
Solution
Let's chose sed-script
as the name of the following script:
#!/bin/bash
pattern="
(
server n
location / n
)(
n
)
"
replacement="
if ($http_user_agent ~* "somewebsite" ) n
return 444;n
n
"
sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"
If you make this script executable and call it like this:
./sed-script INFILE OUTFILE
it will process INFILE
looking for every occurrence of
server
location /
replaces it by
server
location /
if ($http_user_agent ~* "somewebsite" )
return 444;
and puts the result into OUTFILE
.
The only reason why we use a script here is to keep the structure of the sed command clear.
Since the search pattern contains a slash (/
), an exclamation mark (!
) is used to delimit the fields of the sed s command.
add a comment |
Using GNU sed
This task can be can be done with sed.
However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.
The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.
But the most helpful tool in this context is the -z
option that GNU sed offers and that we take great benefit of below.
You can tell whether your version of sed is a GNU implementation by running
sed --version
Turning a multi-line into a single-line problem
With the -z
option that GNU sed offers, you have a choice of what sed thinks is a line.
The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z
option, sed considers lines to be NULL byte terminated instead.
It is quite likely that sed -z
will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.
Solution
Let's chose sed-script
as the name of the following script:
#!/bin/bash
pattern="
(
server n
location / n
)(
n
)
"
replacement="
if ($http_user_agent ~* "somewebsite" ) n
return 444;n
n
"
sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"
If you make this script executable and call it like this:
./sed-script INFILE OUTFILE
it will process INFILE
looking for every occurrence of
server
location /
replaces it by
server
location /
if ($http_user_agent ~* "somewebsite" )
return 444;
and puts the result into OUTFILE
.
The only reason why we use a script here is to keep the structure of the sed command clear.
Since the search pattern contains a slash (/
), an exclamation mark (!
) is used to delimit the fields of the sed s command.
add a comment |
Using GNU sed
This task can be can be done with sed.
However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.
The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.
But the most helpful tool in this context is the -z
option that GNU sed offers and that we take great benefit of below.
You can tell whether your version of sed is a GNU implementation by running
sed --version
Turning a multi-line into a single-line problem
With the -z
option that GNU sed offers, you have a choice of what sed thinks is a line.
The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z
option, sed considers lines to be NULL byte terminated instead.
It is quite likely that sed -z
will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.
Solution
Let's chose sed-script
as the name of the following script:
#!/bin/bash
pattern="
(
server n
location / n
)(
n
)
"
replacement="
if ($http_user_agent ~* "somewebsite" ) n
return 444;n
n
"
sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"
If you make this script executable and call it like this:
./sed-script INFILE OUTFILE
it will process INFILE
looking for every occurrence of
server
location /
replaces it by
server
location /
if ($http_user_agent ~* "somewebsite" )
return 444;
and puts the result into OUTFILE
.
The only reason why we use a script here is to keep the structure of the sed command clear.
Since the search pattern contains a slash (/
), an exclamation mark (!
) is used to delimit the fields of the sed s command.
Using GNU sed
This task can be can be done with sed.
However, if you are restricted to the POSIX standard of sed, it is not so easy to handle this problem because you have to compare several lines with corresponding pattern to decide whether or not an insertion has to be done.
The GNU implementation of sed offers some extensions that simplify this kind of multi-line problems.
But the most helpful tool in this context is the -z
option that GNU sed offers and that we take great benefit of below.
You can tell whether your version of sed is a GNU implementation by running
sed --version
Turning a multi-line into a single-line problem
With the -z
option that GNU sed offers, you have a choice of what sed thinks is a line.
The usual perspective is that a file is split into lines right after each of the newline characters therein. In contrast, with the -z
option, sed considers lines to be NULL byte terminated instead.
It is quite likely that sed -z
will see only one single long line in each one of your files. Fortunately, GNU sed does not impose any limit on the line length that it is able to process.
Solution
Let's chose sed-script
as the name of the following script:
#!/bin/bash
pattern="
(
server n
location / n
)(
n
)
"
replacement="
if ($http_user_agent ~* "somewebsite" ) n
return 444;n
n
"
sed -z -e 's!'"$pattern"'!1'"$replacement"'2!g' "$1" > "$2"
If you make this script executable and call it like this:
./sed-script INFILE OUTFILE
it will process INFILE
looking for every occurrence of
server
location /
replaces it by
server
location /
if ($http_user_agent ~* "somewebsite" )
return 444;
and puts the result into OUTFILE
.
The only reason why we use a script here is to keep the structure of the sed command clear.
Since the search pattern contains a slash (/
), an exclamation mark (!
) is used to delimit the fields of the sed s command.
edited Feb 7 at 14:32
answered Feb 7 at 0:23
JürgenJürgen
192110
192110
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499023%2fscript-to-add-a-block-in-a-few-hundred-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use an
include
statement.– Richard Smith
Feb 6 at 13:58