How is this awk command working across multiple lines?












1














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?










share|improve this question









New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Good catch. I updated the question.
    – Ben Rubin
    8 hours ago










  • Address ranges are described here for GNU awk
    – glenn jackman
    4 hours ago
















1














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?










share|improve this question









New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Good catch. I updated the question.
    – Ben Rubin
    8 hours ago










  • Address ranges are described here for GNU awk
    – glenn jackman
    4 hours ago














1












1








1







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?










share|improve this question









New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 8 hours ago





















New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 8 hours ago









Ben Rubin

1085




1085




New contributor




Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ben Rubin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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










1 Answer
1






active

oldest

votes


















2















  • within /<Directory />/,/AllowOverride None/ (between those lines)


  • sub( ... ) will substitute None for All

  • 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.






share|improve this answer























  • Well, lines with AllowOverrive None which follow a match for /<Directory />/ will get subbed. 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












  • @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 do stuff 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











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.










draft saved

draft discarded


















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









2















  • within /<Directory />/,/AllowOverride None/ (between those lines)


  • sub( ... ) will substitute None for All

  • 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.






share|improve this answer























  • Well, lines with AllowOverrive None which follow a match for /<Directory />/ will get subbed. 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












  • @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 do stuff 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
















2















  • within /<Directory />/,/AllowOverride None/ (between those lines)


  • sub( ... ) will substitute None for All

  • 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.






share|improve this answer























  • Well, lines with AllowOverrive None which follow a match for /<Directory />/ will get subbed. 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












  • @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 do stuff 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














2












2








2







  • within /<Directory />/,/AllowOverride None/ (between those lines)


  • sub( ... ) will substitute None for All

  • 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.






share|improve this answer















  • within /<Directory />/,/AllowOverride None/ (between those lines)


  • sub( ... ) will substitute None for All

  • 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 7 hours ago









Jeff Schaller

38.9k1053125




38.9k1053125










answered 8 hours ago









Archemar

19.6k93570




19.6k93570












  • Well, lines with AllowOverrive None which follow a match for /<Directory />/ will get subbed. 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












  • @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 do stuff 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 subbed. 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












  • @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 do stuff 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 subbed. 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 subbed. 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










Ben Rubin is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

CARDNET

Boot-repair Failure: Unable to locate package grub-common:i386

濃尾地震