Parsing the output of date with sed











up vote
1
down vote

favorite












I am trying to replace the whitespaces in the output of date with '_' with no success.



$date
Fri Sep 14 14:10:04 EDT 2012

$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012


As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?










share|improve this question


















  • 11




    Most versions of date let you specify the format directly instead so you DON'T have to massage it later with other tools.
    – jw013
    Sep 14 '12 at 18:14















up vote
1
down vote

favorite












I am trying to replace the whitespaces in the output of date with '_' with no success.



$date
Fri Sep 14 14:10:04 EDT 2012

$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012


As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?










share|improve this question


















  • 11




    Most versions of date let you specify the format directly instead so you DON'T have to massage it later with other tools.
    – jw013
    Sep 14 '12 at 18:14













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to replace the whitespaces in the output of date with '_' with no success.



$date
Fri Sep 14 14:10:04 EDT 2012

$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012


As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?










share|improve this question













I am trying to replace the whitespaces in the output of date with '_' with no success.



$date
Fri Sep 14 14:10:04 EDT 2012

$ date | sed 's/ /_/'
Fri_Sep 14 14:10:24 EDT 2012


As you can see, the last command only replaced the first whitespace by a _'. Why did it skip the other whitespaces?







shell sed date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 14 '12 at 18:12









Amelio Vazquez-Reina

12.2k52127228




12.2k52127228








  • 11




    Most versions of date let you specify the format directly instead so you DON'T have to massage it later with other tools.
    – jw013
    Sep 14 '12 at 18:14














  • 11




    Most versions of date let you specify the format directly instead so you DON'T have to massage it later with other tools.
    – jw013
    Sep 14 '12 at 18:14








11




11




Most versions of date let you specify the format directly instead so you DON'T have to massage it later with other tools.
– jw013
Sep 14 '12 at 18:14




Most versions of date let you specify the format directly instead so you DON'T have to massage it later with other tools.
– jw013
Sep 14 '12 at 18:14










2 Answers
2






active

oldest

votes

















up vote
8
down vote



accepted










Try:



$ date | tr ' ' '_'


or



$ date | sed 's/ /_/g'


Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.






share|improve this answer



















  • 1




    In a case like this, tr is faster than a regexp substitution.
    – JRFerguson
    Sep 14 '12 at 19:36


















up vote
6
down vote













Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.



date +%Y-%m-%dT%T


which on some systems you may even write as



date +%FT%T


Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.






share|improve this answer























    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',
    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%2f48209%2fparsing-the-output-of-date-with-sed%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    8
    down vote



    accepted










    Try:



    $ date | tr ' ' '_'


    or



    $ date | sed 's/ /_/g'


    Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.






    share|improve this answer



















    • 1




      In a case like this, tr is faster than a regexp substitution.
      – JRFerguson
      Sep 14 '12 at 19:36















    up vote
    8
    down vote



    accepted










    Try:



    $ date | tr ' ' '_'


    or



    $ date | sed 's/ /_/g'


    Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.






    share|improve this answer



















    • 1




      In a case like this, tr is faster than a regexp substitution.
      – JRFerguson
      Sep 14 '12 at 19:36













    up vote
    8
    down vote



    accepted







    up vote
    8
    down vote



    accepted






    Try:



    $ date | tr ' ' '_'


    or



    $ date | sed 's/ /_/g'


    Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.






    share|improve this answer














    Try:



    $ date | tr ' ' '_'


    or



    $ date | sed 's/ /_/g'


    Your command only replaced the first matching instance from the date input because that is the default behaviour of sed. By adding the g (global replacement) option to the end of the command, sed will instead match and replace all occurrences of the expression.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 14 '12 at 18:52

























    answered Sep 14 '12 at 18:14









    tojrobinson

    59637




    59637








    • 1




      In a case like this, tr is faster than a regexp substitution.
      – JRFerguson
      Sep 14 '12 at 19:36














    • 1




      In a case like this, tr is faster than a regexp substitution.
      – JRFerguson
      Sep 14 '12 at 19:36








    1




    1




    In a case like this, tr is faster than a regexp substitution.
    – JRFerguson
    Sep 14 '12 at 19:36




    In a case like this, tr is faster than a regexp substitution.
    – JRFerguson
    Sep 14 '12 at 19:36












    up vote
    6
    down vote













    Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.



    date +%Y-%m-%dT%T


    which on some systems you may even write as



    date +%FT%T


    Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.






    share|improve this answer



























      up vote
      6
      down vote













      Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.



      date +%Y-%m-%dT%T


      which on some systems you may even write as



      date +%FT%T


      Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.






      share|improve this answer

























        up vote
        6
        down vote










        up vote
        6
        down vote









        Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.



        date +%Y-%m-%dT%T


        which on some systems you may even write as



        date +%FT%T


        Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.






        share|improve this answer














        Just my 2 cents. If you want a date as a single word, a good format is the ISO 8601 standard 2012-09-14T21:08:12. It is terse, unambiguous, and its chronological order is the same as its alphabetical order.



        date +%Y-%m-%dT%T


        which on some systems you may even write as



        date +%FT%T


        Beware though that in time zones that implement DSTs, that's still ambiguous. Adding %z removes the ambiguity, but doesn't help with sorting. Another option is to display the UTC time (with -u) or the Unix epoch time (%s is some implementations) which is independent of time zones.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 at 12:30

























        answered Sep 14 '12 at 20:17









        Stéphane Chazelas

        296k54559903




        296k54559903






























            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%2f48209%2fparsing-the-output-of-date-with-sed%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

            List directoties down one level, excluding some named directories and files

            list processes belonging to a network namespace

            list systemd RuntimeDirectory mounts