How is this awk command working across multiple lines?
I'm confused by a portion of the below command.
awk '/<Directory />/,/AllowOverride None/{sub("None", "All", $0)}{print}' /etc/apache2/apache2.conf > myfile.txt
The command replaces
<Directory />
AllowOverride None
</Directory>
with
<Directory />
AllowOverride All
</Directory>
in the Apache configuration, and writes the updated configuration to myfile.txt
.
I don't understand how the command is pattern matching across two lines of the file. None of the examples that I've been able to find work across lines. I'm guessing it has something to do with the comma in /<Directory />,/AllowOverride
, but I'd like to understand exactly what's going on. How is this pattern match being done across two lines of the file?
Also, why is {print}
necessary? Doesn't awk
print its results by default?
awk
New contributor
add a comment |
I'm confused by a portion of the below command.
awk '/<Directory />/,/AllowOverride None/{sub("None", "All", $0)}{print}' /etc/apache2/apache2.conf > myfile.txt
The command replaces
<Directory />
AllowOverride None
</Directory>
with
<Directory />
AllowOverride All
</Directory>
in the Apache configuration, and writes the updated configuration to myfile.txt
.
I don't understand how the command is pattern matching across two lines of the file. None of the examples that I've been able to find work across lines. I'm guessing it has something to do with the comma in /<Directory />,/AllowOverride
, but I'd like to understand exactly what's going on. How is this pattern match being done across two lines of the file?
Also, why is {print}
necessary? Doesn't awk
print its results by default?
awk
New contributor
Good catch. I updated the question.
– Ben Rubin
8 hours ago
Address ranges are described here for GNU awk
– glenn jackman
4 hours ago
add a comment |
I'm confused by a portion of the below command.
awk '/<Directory />/,/AllowOverride None/{sub("None", "All", $0)}{print}' /etc/apache2/apache2.conf > myfile.txt
The command replaces
<Directory />
AllowOverride None
</Directory>
with
<Directory />
AllowOverride All
</Directory>
in the Apache configuration, and writes the updated configuration to myfile.txt
.
I don't understand how the command is pattern matching across two lines of the file. None of the examples that I've been able to find work across lines. I'm guessing it has something to do with the comma in /<Directory />,/AllowOverride
, but I'd like to understand exactly what's going on. How is this pattern match being done across two lines of the file?
Also, why is {print}
necessary? Doesn't awk
print its results by default?
awk
New contributor
I'm confused by a portion of the below command.
awk '/<Directory />/,/AllowOverride None/{sub("None", "All", $0)}{print}' /etc/apache2/apache2.conf > myfile.txt
The command replaces
<Directory />
AllowOverride None
</Directory>
with
<Directory />
AllowOverride All
</Directory>
in the Apache configuration, and writes the updated configuration to myfile.txt
.
I don't understand how the command is pattern matching across two lines of the file. None of the examples that I've been able to find work across lines. I'm guessing it has something to do with the comma in /<Directory />,/AllowOverride
, but I'd like to understand exactly what's going on. How is this pattern match being done across two lines of the file?
Also, why is {print}
necessary? Doesn't awk
print its results by default?
awk
awk
New contributor
New contributor
edited 8 hours ago
New contributor
asked 8 hours ago
Ben Rubin
1085
1085
New contributor
New contributor
Good catch. I updated the question.
– Ben Rubin
8 hours ago
Address ranges are described here for GNU awk
– glenn jackman
4 hours ago
add a comment |
Good catch. I updated the question.
– Ben Rubin
8 hours ago
Address ranges are described here for GNU awk
– glenn jackman
4 hours ago
Good catch. I updated the question.
– Ben Rubin
8 hours ago
Good catch. I updated the question.
– Ben Rubin
8 hours ago
Address ranges are described here for GNU awk
– glenn jackman
4 hours ago
Address ranges are described here for GNU awk
– glenn jackman
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
- within
/<Directory />/,/AllowOverride None/
(between those lines)
sub( ... )
will substituteNone
forAll
- a
{print}
is needed for all lines (substituted or not)
Note that line(s) with AllowOverride None
will get a sub
and a print
, all other line will be printed only.
correct code for me is:
awk '/<Directory />/,/AllowOverride None/{ sub("None", "All", $0)}
{print}' /etc/apache2/apache2.conf > myfile.txt
about default, consider
... | awk 'NF==7'
which will by default print lines with 7 fields, however, in your case, sub function overwrite default, so no other action will be taken. That is why you need an explicit print.
Well, lines withAllowOverrive None
which follow a match for/<Directory />/
will getsub
bed. Since only lines which fall in those ranges will get looked at.
– DopeGhoti
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies thatAllowOverride None
lines that follow<Directory />
lines get substituted.
– Ben Rubin
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
3
Yes, it's saying for/foo/,/bar/ { stuff }
to dostuff
between a match for/foo/
and/bar/
.
– DopeGhoti
7 hours ago
1
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
|
show 2 more comments
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
});
}
});
Ben Rubin is a new contributor. Be nice, and check out our Code of Conduct.
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%2f492318%2fhow-is-this-awk-command-working-across-multiple-lines%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
- within
/<Directory />/,/AllowOverride None/
(between those lines)
sub( ... )
will substituteNone
forAll
- a
{print}
is needed for all lines (substituted or not)
Note that line(s) with AllowOverride None
will get a sub
and a print
, all other line will be printed only.
correct code for me is:
awk '/<Directory />/,/AllowOverride None/{ sub("None", "All", $0)}
{print}' /etc/apache2/apache2.conf > myfile.txt
about default, consider
... | awk 'NF==7'
which will by default print lines with 7 fields, however, in your case, sub function overwrite default, so no other action will be taken. That is why you need an explicit print.
Well, lines withAllowOverrive None
which follow a match for/<Directory />/
will getsub
bed. Since only lines which fall in those ranges will get looked at.
– DopeGhoti
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies thatAllowOverride None
lines that follow<Directory />
lines get substituted.
– Ben Rubin
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
3
Yes, it's saying for/foo/,/bar/ { stuff }
to dostuff
between a match for/foo/
and/bar/
.
– DopeGhoti
7 hours ago
1
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
|
show 2 more comments
- within
/<Directory />/,/AllowOverride None/
(between those lines)
sub( ... )
will substituteNone
forAll
- a
{print}
is needed for all lines (substituted or not)
Note that line(s) with AllowOverride None
will get a sub
and a print
, all other line will be printed only.
correct code for me is:
awk '/<Directory />/,/AllowOverride None/{ sub("None", "All", $0)}
{print}' /etc/apache2/apache2.conf > myfile.txt
about default, consider
... | awk 'NF==7'
which will by default print lines with 7 fields, however, in your case, sub function overwrite default, so no other action will be taken. That is why you need an explicit print.
Well, lines withAllowOverrive None
which follow a match for/<Directory />/
will getsub
bed. Since only lines which fall in those ranges will get looked at.
– DopeGhoti
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies thatAllowOverride None
lines that follow<Directory />
lines get substituted.
– Ben Rubin
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
3
Yes, it's saying for/foo/,/bar/ { stuff }
to dostuff
between a match for/foo/
and/bar/
.
– DopeGhoti
7 hours ago
1
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
|
show 2 more comments
- within
/<Directory />/,/AllowOverride None/
(between those lines)
sub( ... )
will substituteNone
forAll
- a
{print}
is needed for all lines (substituted or not)
Note that line(s) with AllowOverride None
will get a sub
and a print
, all other line will be printed only.
correct code for me is:
awk '/<Directory />/,/AllowOverride None/{ sub("None", "All", $0)}
{print}' /etc/apache2/apache2.conf > myfile.txt
about default, consider
... | awk 'NF==7'
which will by default print lines with 7 fields, however, in your case, sub function overwrite default, so no other action will be taken. That is why you need an explicit print.
- within
/<Directory />/,/AllowOverride None/
(between those lines)
sub( ... )
will substituteNone
forAll
- a
{print}
is needed for all lines (substituted or not)
Note that line(s) with AllowOverride None
will get a sub
and a print
, all other line will be printed only.
correct code for me is:
awk '/<Directory />/,/AllowOverride None/{ sub("None", "All", $0)}
{print}' /etc/apache2/apache2.conf > myfile.txt
about default, consider
... | awk 'NF==7'
which will by default print lines with 7 fields, however, in your case, sub function overwrite default, so no other action will be taken. That is why you need an explicit print.
edited 7 hours ago
Jeff Schaller
38.9k1053125
38.9k1053125
answered 8 hours ago
Archemar
19.6k93570
19.6k93570
Well, lines withAllowOverrive None
which follow a match for/<Directory />/
will getsub
bed. Since only lines which fall in those ranges will get looked at.
– DopeGhoti
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies thatAllowOverride None
lines that follow<Directory />
lines get substituted.
– Ben Rubin
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
3
Yes, it's saying for/foo/,/bar/ { stuff }
to dostuff
between a match for/foo/
and/bar/
.
– DopeGhoti
7 hours ago
1
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
|
show 2 more comments
Well, lines withAllowOverrive None
which follow a match for/<Directory />/
will getsub
bed. Since only lines which fall in those ranges will get looked at.
– DopeGhoti
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies thatAllowOverride None
lines that follow<Directory />
lines get substituted.
– Ben Rubin
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
3
Yes, it's saying for/foo/,/bar/ { stuff }
to dostuff
between a match for/foo/
and/bar/
.
– DopeGhoti
7 hours ago
1
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
Well, lines with
AllowOverrive None
which follow a match for /<Directory />/
will get sub
bed. Since only lines which fall in those ranges will get looked at.– DopeGhoti
8 hours ago
Well, lines with
AllowOverrive None
which follow a match for /<Directory />/
will get sub
bed. Since only lines which fall in those ranges will get looked at.– DopeGhoti
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies that
AllowOverride None
lines that follow <Directory />
lines get substituted.– Ben Rubin
8 hours ago
@DopeGhoti The part I don't understand is how the command specifies that
AllowOverride None
lines that follow <Directory />
lines get substituted.– Ben Rubin
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
@Ben Read the answer carefully: . . . lines between / ... /,/ ... / it means first regex is matched -> start of action in first { } braces, second regex is matched -> last line is processed. The second { } braces action takes efect for every lines.
– schweik
8 hours ago
3
3
Yes, it's saying for
/foo/,/bar/ { stuff }
to do stuff
between a match for /foo/
and /bar/
.– DopeGhoti
7 hours ago
Yes, it's saying for
/foo/,/bar/ { stuff }
to do stuff
between a match for /foo/
and /bar/
.– DopeGhoti
7 hours ago
1
1
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
From the man awk: "A range pattern, expr1,expr2 , matches every record between the match of expr1 and the match expr2 inclusively" ()
– schweik
7 hours ago
|
show 2 more comments
Ben Rubin is a new contributor. Be nice, and check out our Code of Conduct.
Ben Rubin is a new contributor. Be nice, and check out our Code of Conduct.
Ben Rubin is a new contributor. Be nice, and check out our Code of Conduct.
Ben Rubin is a new contributor. Be nice, and check out our Code of Conduct.
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f492318%2fhow-is-this-awk-command-working-across-multiple-lines%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
Good catch. I updated the question.
– Ben Rubin
8 hours ago
Address ranges are described here for GNU awk
– glenn jackman
4 hours ago