Sed - Curly braces needed for a delete expression, not for a substitute one












1















In an SO question I answered these two sed lines:





  • First:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'s/^delay=.*$//' 
    -e 's/^delay=/ens_delay=/' your_file.txt



  • Second:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'{/^delay=.*$/d}' 
    -e 's/^delay=/ens_delay=/' your_file.txt



In the second line, I figured out I needed curly braces around /^delay=.*$/d to make it work. But it wasn't necessary for s/^delay=.*$// in the first line (though it works with).



Why this difference?










share|improve this question


















  • 1





    See unix.stackexchange.com/a/206908/279389

    – Wiktor Stribiżew
    Nov 15 '18 at 12:51











  • The nested sed calls to get a line number is a clear indication that you are doing it wrong. Whatever you are hoping to accomplish is probably trivial to rewrite in Awk, and will be much more efficient to boot.

    – tripleee
    Nov 15 '18 at 13:52











  • @triplee I call it a "monster" in my answer. You should see the related question. I was trying to answer with a "sed only" line. This answer was far better.

    – Amessihel
    Nov 15 '18 at 14:13


















1















In an SO question I answered these two sed lines:





  • First:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'s/^delay=.*$//' 
    -e 's/^delay=/ens_delay=/' your_file.txt



  • Second:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'{/^delay=.*$/d}' 
    -e 's/^delay=/ens_delay=/' your_file.txt



In the second line, I figured out I needed curly braces around /^delay=.*$/d to make it work. But it wasn't necessary for s/^delay=.*$// in the first line (though it works with).



Why this difference?










share|improve this question


















  • 1





    See unix.stackexchange.com/a/206908/279389

    – Wiktor Stribiżew
    Nov 15 '18 at 12:51











  • The nested sed calls to get a line number is a clear indication that you are doing it wrong. Whatever you are hoping to accomplish is probably trivial to rewrite in Awk, and will be much more efficient to boot.

    – tripleee
    Nov 15 '18 at 13:52











  • @triplee I call it a "monster" in my answer. You should see the related question. I was trying to answer with a "sed only" line. This answer was far better.

    – Amessihel
    Nov 15 '18 at 14:13
















1












1








1








In an SO question I answered these two sed lines:





  • First:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'s/^delay=.*$//' 
    -e 's/^delay=/ens_delay=/' your_file.txt



  • Second:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'{/^delay=.*$/d}' 
    -e 's/^delay=/ens_delay=/' your_file.txt



In the second line, I figured out I needed curly braces around /^delay=.*$/d to make it work. But it wasn't necessary for s/^delay=.*$// in the first line (though it works with).



Why this difference?










share|improve this question














In an SO question I answered these two sed lines:





  • First:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'s/^delay=.*$//' 
    -e 's/^delay=/ens_delay=/' your_file.txt



  • Second:



    sed -e "1,$(expr $(sed -n '/^delay=/=' your_file.txt | tail -1) - 1)"'{/^delay=.*$/d}' 
    -e 's/^delay=/ens_delay=/' your_file.txt



In the second line, I figured out I needed curly braces around /^delay=.*$/d to make it work. But it wasn't necessary for s/^delay=.*$// in the first line (though it works with).



Why this difference?







regex bash sed curly-braces






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 12:36









AmessihelAmessihel

2,6291824




2,6291824








  • 1





    See unix.stackexchange.com/a/206908/279389

    – Wiktor Stribiżew
    Nov 15 '18 at 12:51











  • The nested sed calls to get a line number is a clear indication that you are doing it wrong. Whatever you are hoping to accomplish is probably trivial to rewrite in Awk, and will be much more efficient to boot.

    – tripleee
    Nov 15 '18 at 13:52











  • @triplee I call it a "monster" in my answer. You should see the related question. I was trying to answer with a "sed only" line. This answer was far better.

    – Amessihel
    Nov 15 '18 at 14:13
















  • 1





    See unix.stackexchange.com/a/206908/279389

    – Wiktor Stribiżew
    Nov 15 '18 at 12:51











  • The nested sed calls to get a line number is a clear indication that you are doing it wrong. Whatever you are hoping to accomplish is probably trivial to rewrite in Awk, and will be much more efficient to boot.

    – tripleee
    Nov 15 '18 at 13:52











  • @triplee I call it a "monster" in my answer. You should see the related question. I was trying to answer with a "sed only" line. This answer was far better.

    – Amessihel
    Nov 15 '18 at 14:13










1




1





See unix.stackexchange.com/a/206908/279389

– Wiktor Stribiżew
Nov 15 '18 at 12:51





See unix.stackexchange.com/a/206908/279389

– Wiktor Stribiżew
Nov 15 '18 at 12:51













The nested sed calls to get a line number is a clear indication that you are doing it wrong. Whatever you are hoping to accomplish is probably trivial to rewrite in Awk, and will be much more efficient to boot.

– tripleee
Nov 15 '18 at 13:52





The nested sed calls to get a line number is a clear indication that you are doing it wrong. Whatever you are hoping to accomplish is probably trivial to rewrite in Awk, and will be much more efficient to boot.

– tripleee
Nov 15 '18 at 13:52













@triplee I call it a "monster" in my answer. You should see the related question. I was trying to answer with a "sed only" line. This answer was far better.

– Amessihel
Nov 15 '18 at 14:13







@triplee I call it a "monster" in my answer. You should see the related question. I was trying to answer with a "sed only" line. This answer was far better.

– Amessihel
Nov 15 '18 at 14:13














1 Answer
1






active

oldest

votes


















1














DISCLAIMER: The answer is a trimmed copy of Why are these curly braces necessary in sed?



Per the POSIX standard's page on sed:




The script shall consist of editing commands of the following form:



[address[,address]]function


where function represents a single-character command verb from the list in
Editing Commands in sed, followed by any applicable arguments.




So the first non-blank character after the address is taken as a command verb.



The braces are referenced further down:






[2addr] {editing command
editing command
...
}


    Execute a list of `sed` editing commands only when the pattern space is selected.  …



[2addr] is an indicator that the maximum number of permissible addresses is two.



To clarify a point made above, the Addresses section of sed(1) says:




Sed commands can be given with no addresses,
in which case the command will be executed for all input lines;
with one address, in which case the command will be executed only
for input lines which match that address;
or with two addresses, in which case the command will be executed
for all input lines which match the inclusive range of lines
starting from the first address and continuing to the second address. 
Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);

        … (and other stuff not relevant to this discussion)




The gnu info page (info sed) has a similar description of { and },
under "3.4 Often-Used Commands":




{ COMMANDS }


    A group of commands may be enclosed
    between { and } characters.
    This is particularly useful when you want a group of commands to be triggered
    by a single address (or address-range) match.



Otherwise said, braces are used to apply multiple commands at the same address or to nest addresses.

The standard isn't very explicit here1 but the left brace { is actually a command that starts a group of other sed commands (the group ends with a right brace }).



1:

though if you read the entire page it is mentioned that:
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by...






share|improve this answer
























  • So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

    – tripleee
    Nov 15 '18 at 14:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319664%2fsed-curly-braces-needed-for-a-delete-expression-not-for-a-substitute-one%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









1














DISCLAIMER: The answer is a trimmed copy of Why are these curly braces necessary in sed?



Per the POSIX standard's page on sed:




The script shall consist of editing commands of the following form:



[address[,address]]function


where function represents a single-character command verb from the list in
Editing Commands in sed, followed by any applicable arguments.




So the first non-blank character after the address is taken as a command verb.



The braces are referenced further down:






[2addr] {editing command
editing command
...
}


    Execute a list of `sed` editing commands only when the pattern space is selected.  …



[2addr] is an indicator that the maximum number of permissible addresses is two.



To clarify a point made above, the Addresses section of sed(1) says:




Sed commands can be given with no addresses,
in which case the command will be executed for all input lines;
with one address, in which case the command will be executed only
for input lines which match that address;
or with two addresses, in which case the command will be executed
for all input lines which match the inclusive range of lines
starting from the first address and continuing to the second address. 
Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);

        … (and other stuff not relevant to this discussion)




The gnu info page (info sed) has a similar description of { and },
under "3.4 Often-Used Commands":




{ COMMANDS }


    A group of commands may be enclosed
    between { and } characters.
    This is particularly useful when you want a group of commands to be triggered
    by a single address (or address-range) match.



Otherwise said, braces are used to apply multiple commands at the same address or to nest addresses.

The standard isn't very explicit here1 but the left brace { is actually a command that starts a group of other sed commands (the group ends with a right brace }).



1:

though if you read the entire page it is mentioned that:
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by...






share|improve this answer
























  • So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

    – tripleee
    Nov 15 '18 at 14:49
















1














DISCLAIMER: The answer is a trimmed copy of Why are these curly braces necessary in sed?



Per the POSIX standard's page on sed:




The script shall consist of editing commands of the following form:



[address[,address]]function


where function represents a single-character command verb from the list in
Editing Commands in sed, followed by any applicable arguments.




So the first non-blank character after the address is taken as a command verb.



The braces are referenced further down:






[2addr] {editing command
editing command
...
}


    Execute a list of `sed` editing commands only when the pattern space is selected.  …



[2addr] is an indicator that the maximum number of permissible addresses is two.



To clarify a point made above, the Addresses section of sed(1) says:




Sed commands can be given with no addresses,
in which case the command will be executed for all input lines;
with one address, in which case the command will be executed only
for input lines which match that address;
or with two addresses, in which case the command will be executed
for all input lines which match the inclusive range of lines
starting from the first address and continuing to the second address. 
Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);

        … (and other stuff not relevant to this discussion)




The gnu info page (info sed) has a similar description of { and },
under "3.4 Often-Used Commands":




{ COMMANDS }


    A group of commands may be enclosed
    between { and } characters.
    This is particularly useful when you want a group of commands to be triggered
    by a single address (or address-range) match.



Otherwise said, braces are used to apply multiple commands at the same address or to nest addresses.

The standard isn't very explicit here1 but the left brace { is actually a command that starts a group of other sed commands (the group ends with a right brace }).



1:

though if you read the entire page it is mentioned that:
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by...






share|improve this answer
























  • So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

    – tripleee
    Nov 15 '18 at 14:49














1












1








1







DISCLAIMER: The answer is a trimmed copy of Why are these curly braces necessary in sed?



Per the POSIX standard's page on sed:




The script shall consist of editing commands of the following form:



[address[,address]]function


where function represents a single-character command verb from the list in
Editing Commands in sed, followed by any applicable arguments.




So the first non-blank character after the address is taken as a command verb.



The braces are referenced further down:






[2addr] {editing command
editing command
...
}


    Execute a list of `sed` editing commands only when the pattern space is selected.  …



[2addr] is an indicator that the maximum number of permissible addresses is two.



To clarify a point made above, the Addresses section of sed(1) says:




Sed commands can be given with no addresses,
in which case the command will be executed for all input lines;
with one address, in which case the command will be executed only
for input lines which match that address;
or with two addresses, in which case the command will be executed
for all input lines which match the inclusive range of lines
starting from the first address and continuing to the second address. 
Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);

        … (and other stuff not relevant to this discussion)




The gnu info page (info sed) has a similar description of { and },
under "3.4 Often-Used Commands":




{ COMMANDS }


    A group of commands may be enclosed
    between { and } characters.
    This is particularly useful when you want a group of commands to be triggered
    by a single address (or address-range) match.



Otherwise said, braces are used to apply multiple commands at the same address or to nest addresses.

The standard isn't very explicit here1 but the left brace { is actually a command that starts a group of other sed commands (the group ends with a right brace }).



1:

though if you read the entire page it is mentioned that:
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by...






share|improve this answer













DISCLAIMER: The answer is a trimmed copy of Why are these curly braces necessary in sed?



Per the POSIX standard's page on sed:




The script shall consist of editing commands of the following form:



[address[,address]]function


where function represents a single-character command verb from the list in
Editing Commands in sed, followed by any applicable arguments.




So the first non-blank character after the address is taken as a command verb.



The braces are referenced further down:






[2addr] {editing command
editing command
...
}


    Execute a list of `sed` editing commands only when the pattern space is selected.  …



[2addr] is an indicator that the maximum number of permissible addresses is two.



To clarify a point made above, the Addresses section of sed(1) says:




Sed commands can be given with no addresses,
in which case the command will be executed for all input lines;
with one address, in which case the command will be executed only
for input lines which match that address;
or with two addresses, in which case the command will be executed
for all input lines which match the inclusive range of lines
starting from the first address and continuing to the second address. 
Three things to note about address ranges:
the syntax is addr1,addr2 (i.e., the addresses are separated by a comma);

        … (and other stuff not relevant to this discussion)




The gnu info page (info sed) has a similar description of { and },
under "3.4 Often-Used Commands":




{ COMMANDS }


    A group of commands may be enclosed
    between { and } characters.
    This is particularly useful when you want a group of commands to be triggered
    by a single address (or address-range) match.



Otherwise said, braces are used to apply multiple commands at the same address or to nest addresses.

The standard isn't very explicit here1 but the left brace { is actually a command that starts a group of other sed commands (the group ends with a right brace }).



1:

though if you read the entire page it is mentioned that:
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by...







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 13:40









Wiktor StribiżewWiktor Stribiżew

321k16142223




321k16142223













  • So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

    – tripleee
    Nov 15 '18 at 14:49



















  • So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

    – tripleee
    Nov 15 '18 at 14:49

















So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

– tripleee
Nov 15 '18 at 14:49





So the TL;LR is that's how you say "when this address matches and this address also matches", where in this particular case the first address is a range of line numbers, and the second address is a regex. Every address needs an action, but when the action is a block in braces, you can specify additiona address/action pairs, inside them, or even go recursive with another pair of braces.

– tripleee
Nov 15 '18 at 14:49




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f53319664%2fsed-curly-braces-needed-for-a-delete-expression-not-for-a-substitute-one%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python