Sed Explanation: sed '/./,$!d' file












5














Could someone please explain this code that deletes all leading blank lines at the top of a file:



sed '/./,$!d' file


I understand that it is a regex, matching only the first character, but then don't understand the ,$!d part. Is this what it's being replaced by, or are they options for the match?



Is this even a search command if it does not start with 's/'...?



Code source (from another question)










share|improve this question





























    5














    Could someone please explain this code that deletes all leading blank lines at the top of a file:



    sed '/./,$!d' file


    I understand that it is a regex, matching only the first character, but then don't understand the ,$!d part. Is this what it's being replaced by, or are they options for the match?



    Is this even a search command if it does not start with 's/'...?



    Code source (from another question)










    share|improve this question



























      5












      5








      5


      1





      Could someone please explain this code that deletes all leading blank lines at the top of a file:



      sed '/./,$!d' file


      I understand that it is a regex, matching only the first character, but then don't understand the ,$!d part. Is this what it's being replaced by, or are they options for the match?



      Is this even a search command if it does not start with 's/'...?



      Code source (from another question)










      share|improve this question















      Could someone please explain this code that deletes all leading blank lines at the top of a file:



      sed '/./,$!d' file


      I understand that it is a regex, matching only the first character, but then don't understand the ,$!d part. Is this what it's being replaced by, or are they options for the match?



      Is this even a search command if it does not start with 's/'...?



      Code source (from another question)







      sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 15 at 22:22









      Rui F Ribeiro

      38.9k1479129




      38.9k1479129










      asked Nov 17 '16 at 9:44









      Emerson Peters

      3716




      3716






















          1 Answer
          1






          active

          oldest

          votes


















          10














          sed '/./,$!d'


          From the first line which contains a character (blank or not) to the end of the file - negate (which then means from the beginning of the file to the line before the first line which contains a character) - delete.



          This deletes leading empty lines, not blank lines. To delete leading blank lines (lines which are empty or contain only whitespace characters) say '/S/,$!d'.



          Read "Sed, an introduction and tutorial" at http://www.grymoire.com/Unix/Sed.html. Then read the reference manual at https://www.gnu.org/software/sed/manual/sed.html.



          In short:




          • The general form of a sed command is [selector][negation]command[flags] (square brackets indicate optional parts)


          • The selector, if present, selects the lines on which the command applies


          • If ! appears it negates the selector, that is, makes the command apply to the lines which do not match the selector.


          • If no selector is present the command applies to all lines.


          • A selector can select one line (by number) or a set of lines (by regular expression), or the lines between a start line (by number or regular expression) and an end line (by number or regular expression).


          • In our case the selector is /./,$ which means from the first line found which matches /./ (that is, contains at least one character) to the end of the file ($ is used as a line number and means the last line in the file).


          • It is negated by !, so that the command applies to the lines from the beginning of the file to the line before the first line matching /./.


          • The command d deletes the selected lines.







          share|improve this answer























          • Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
            – Emerson Peters
            Nov 17 '16 at 10:01










          • Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
            – Emerson Peters
            Nov 17 '16 at 10:02










          • I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
            – Emerson Peters
            Nov 17 '16 at 10:18











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f323948%2fsed-explanation-sed-d-file%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









          10














          sed '/./,$!d'


          From the first line which contains a character (blank or not) to the end of the file - negate (which then means from the beginning of the file to the line before the first line which contains a character) - delete.



          This deletes leading empty lines, not blank lines. To delete leading blank lines (lines which are empty or contain only whitespace characters) say '/S/,$!d'.



          Read "Sed, an introduction and tutorial" at http://www.grymoire.com/Unix/Sed.html. Then read the reference manual at https://www.gnu.org/software/sed/manual/sed.html.



          In short:




          • The general form of a sed command is [selector][negation]command[flags] (square brackets indicate optional parts)


          • The selector, if present, selects the lines on which the command applies


          • If ! appears it negates the selector, that is, makes the command apply to the lines which do not match the selector.


          • If no selector is present the command applies to all lines.


          • A selector can select one line (by number) or a set of lines (by regular expression), or the lines between a start line (by number or regular expression) and an end line (by number or regular expression).


          • In our case the selector is /./,$ which means from the first line found which matches /./ (that is, contains at least one character) to the end of the file ($ is used as a line number and means the last line in the file).


          • It is negated by !, so that the command applies to the lines from the beginning of the file to the line before the first line matching /./.


          • The command d deletes the selected lines.







          share|improve this answer























          • Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
            – Emerson Peters
            Nov 17 '16 at 10:01










          • Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
            – Emerson Peters
            Nov 17 '16 at 10:02










          • I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
            – Emerson Peters
            Nov 17 '16 at 10:18
















          10














          sed '/./,$!d'


          From the first line which contains a character (blank or not) to the end of the file - negate (which then means from the beginning of the file to the line before the first line which contains a character) - delete.



          This deletes leading empty lines, not blank lines. To delete leading blank lines (lines which are empty or contain only whitespace characters) say '/S/,$!d'.



          Read "Sed, an introduction and tutorial" at http://www.grymoire.com/Unix/Sed.html. Then read the reference manual at https://www.gnu.org/software/sed/manual/sed.html.



          In short:




          • The general form of a sed command is [selector][negation]command[flags] (square brackets indicate optional parts)


          • The selector, if present, selects the lines on which the command applies


          • If ! appears it negates the selector, that is, makes the command apply to the lines which do not match the selector.


          • If no selector is present the command applies to all lines.


          • A selector can select one line (by number) or a set of lines (by regular expression), or the lines between a start line (by number or regular expression) and an end line (by number or regular expression).


          • In our case the selector is /./,$ which means from the first line found which matches /./ (that is, contains at least one character) to the end of the file ($ is used as a line number and means the last line in the file).


          • It is negated by !, so that the command applies to the lines from the beginning of the file to the line before the first line matching /./.


          • The command d deletes the selected lines.







          share|improve this answer























          • Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
            – Emerson Peters
            Nov 17 '16 at 10:01










          • Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
            – Emerson Peters
            Nov 17 '16 at 10:02










          • I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
            – Emerson Peters
            Nov 17 '16 at 10:18














          10












          10








          10






          sed '/./,$!d'


          From the first line which contains a character (blank or not) to the end of the file - negate (which then means from the beginning of the file to the line before the first line which contains a character) - delete.



          This deletes leading empty lines, not blank lines. To delete leading blank lines (lines which are empty or contain only whitespace characters) say '/S/,$!d'.



          Read "Sed, an introduction and tutorial" at http://www.grymoire.com/Unix/Sed.html. Then read the reference manual at https://www.gnu.org/software/sed/manual/sed.html.



          In short:




          • The general form of a sed command is [selector][negation]command[flags] (square brackets indicate optional parts)


          • The selector, if present, selects the lines on which the command applies


          • If ! appears it negates the selector, that is, makes the command apply to the lines which do not match the selector.


          • If no selector is present the command applies to all lines.


          • A selector can select one line (by number) or a set of lines (by regular expression), or the lines between a start line (by number or regular expression) and an end line (by number or regular expression).


          • In our case the selector is /./,$ which means from the first line found which matches /./ (that is, contains at least one character) to the end of the file ($ is used as a line number and means the last line in the file).


          • It is negated by !, so that the command applies to the lines from the beginning of the file to the line before the first line matching /./.


          • The command d deletes the selected lines.







          share|improve this answer














          sed '/./,$!d'


          From the first line which contains a character (blank or not) to the end of the file - negate (which then means from the beginning of the file to the line before the first line which contains a character) - delete.



          This deletes leading empty lines, not blank lines. To delete leading blank lines (lines which are empty or contain only whitespace characters) say '/S/,$!d'.



          Read "Sed, an introduction and tutorial" at http://www.grymoire.com/Unix/Sed.html. Then read the reference manual at https://www.gnu.org/software/sed/manual/sed.html.



          In short:




          • The general form of a sed command is [selector][negation]command[flags] (square brackets indicate optional parts)


          • The selector, if present, selects the lines on which the command applies


          • If ! appears it negates the selector, that is, makes the command apply to the lines which do not match the selector.


          • If no selector is present the command applies to all lines.


          • A selector can select one line (by number) or a set of lines (by regular expression), or the lines between a start line (by number or regular expression) and an end line (by number or regular expression).


          • In our case the selector is /./,$ which means from the first line found which matches /./ (that is, contains at least one character) to the end of the file ($ is used as a line number and means the last line in the file).


          • It is negated by !, so that the command applies to the lines from the beginning of the file to the line before the first line matching /./.


          • The command d deletes the selected lines.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 '16 at 10:14

























          answered Nov 17 '16 at 9:50









          AlexP

          7,0991125




          7,0991125












          • Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
            – Emerson Peters
            Nov 17 '16 at 10:01










          • Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
            – Emerson Peters
            Nov 17 '16 at 10:02










          • I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
            – Emerson Peters
            Nov 17 '16 at 10:18


















          • Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
            – Emerson Peters
            Nov 17 '16 at 10:01










          • Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
            – Emerson Peters
            Nov 17 '16 at 10:02










          • I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
            – Emerson Peters
            Nov 17 '16 at 10:18
















          Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
          – Emerson Peters
          Nov 17 '16 at 10:01




          Could you please explain each thing individually?What does the comma do? I know the $ means the end of the string. I'm guessing the ! means the negate? How did it get to the point of defining the "beginning of the file to the line before the first line which contains a character" point?
          – Emerson Peters
          Nov 17 '16 at 10:01












          Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
          – Emerson Peters
          Nov 17 '16 at 10:02




          Are the ,$!d part of the replace section, or are they options like where the g for a global search would go?
          – Emerson Peters
          Nov 17 '16 at 10:02












          I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
          – Emerson Peters
          Nov 17 '16 at 10:18




          I had both of those tabs open, but couldn't find a reference to these options/commands in the table of contents, or through a quick skim, so I didn't know where to look. It looks like I just need a more global understanding of sed first. Thank you for going into detail. I will use this as reference when studying later. :)
          – Emerson Peters
          Nov 17 '16 at 10:18


















          draft saved

          draft discarded




















































          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%2f323948%2fsed-explanation-sed-d-file%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

          Morgemoulin

          Scott Moir

          Souastre