Use Sed Regex Capture Group in Replace Section Method

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I have a list of timestamped filenames in this following format:



cat files


  • ..201807010112.log

  • ..201807020112.log

  • ..201807022359.log

  • ..201807030112.log

  • ..201807010412.log

I need to get a certain range and in order to do this, I'd like to use SED.



sed -n '/201807010112/,/201807030112/p' 


Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.



I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.



cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"


My problem is that I cannot use the result of the match 1 in the date conversion block.



Is there a way to do this or better yet a better way to get the range of dates?










share|improve this question



















  • 3




    This is a very good example of a XY question...
    – don_crissti
    Aug 6 at 22:23










  • cat 2018070[1-2],301
    – user1133275
    Aug 6 at 22:49






  • 1




    like... sycamore?
    – mikeserv
    Aug 7 at 0:52






  • 1




    Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
    – Proximo
    Aug 7 at 1:47










  • Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
    – Kusalananda
    Aug 7 at 6:14















up vote
2
down vote

favorite












I have a list of timestamped filenames in this following format:



cat files


  • ..201807010112.log

  • ..201807020112.log

  • ..201807022359.log

  • ..201807030112.log

  • ..201807010412.log

I need to get a certain range and in order to do this, I'd like to use SED.



sed -n '/201807010112/,/201807030112/p' 


Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.



I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.



cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"


My problem is that I cannot use the result of the match 1 in the date conversion block.



Is there a way to do this or better yet a better way to get the range of dates?










share|improve this question



















  • 3




    This is a very good example of a XY question...
    – don_crissti
    Aug 6 at 22:23










  • cat 2018070[1-2],301
    – user1133275
    Aug 6 at 22:49






  • 1




    like... sycamore?
    – mikeserv
    Aug 7 at 0:52






  • 1




    Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
    – Proximo
    Aug 7 at 1:47










  • Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
    – Kusalananda
    Aug 7 at 6:14













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a list of timestamped filenames in this following format:



cat files


  • ..201807010112.log

  • ..201807020112.log

  • ..201807022359.log

  • ..201807030112.log

  • ..201807010412.log

I need to get a certain range and in order to do this, I'd like to use SED.



sed -n '/201807010112/,/201807030112/p' 


Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.



I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.



cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"


My problem is that I cannot use the result of the match 1 in the date conversion block.



Is there a way to do this or better yet a better way to get the range of dates?










share|improve this question















I have a list of timestamped filenames in this following format:



cat files


  • ..201807010112.log

  • ..201807020112.log

  • ..201807022359.log

  • ..201807030112.log

  • ..201807010412.log

I need to get a certain range and in order to do this, I'd like to use SED.



sed -n '/201807010112/,/201807030112/p' 


Was my first attempt but sed treats the hour and minute strangely, and to make a long story short it's unable to understand this format correctly.



I may be getting in the weeds but in order to solve this, I've decided to convert the format to something SED understands.



cat files | sed -e "s/([0-9]12}/$(date -f '%Y%m%d%H%M' 1)/g"


My problem is that I cannot use the result of the match 1 in the date conversion block.



Is there a way to do this or better yet a better way to get the range of dates?







linux awk sed grep regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 7 at 1:23

























asked Aug 6 at 21:55









Proximo

1235




1235







  • 3




    This is a very good example of a XY question...
    – don_crissti
    Aug 6 at 22:23










  • cat 2018070[1-2],301
    – user1133275
    Aug 6 at 22:49






  • 1




    like... sycamore?
    – mikeserv
    Aug 7 at 0:52






  • 1




    Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
    – Proximo
    Aug 7 at 1:47










  • Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
    – Kusalananda
    Aug 7 at 6:14













  • 3




    This is a very good example of a XY question...
    – don_crissti
    Aug 6 at 22:23










  • cat 2018070[1-2],301
    – user1133275
    Aug 6 at 22:49






  • 1




    like... sycamore?
    – mikeserv
    Aug 7 at 0:52






  • 1




    Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
    – Proximo
    Aug 7 at 1:47










  • Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
    – Kusalananda
    Aug 7 at 6:14








3




3




This is a very good example of a XY question...
– don_crissti
Aug 6 at 22:23




This is a very good example of a XY question...
– don_crissti
Aug 6 at 22:23












cat 2018070[1-2],301
– user1133275
Aug 6 at 22:49




cat 2018070[1-2],301
– user1133275
Aug 6 at 22:49




1




1




like... sycamore?
– mikeserv
Aug 7 at 0:52




like... sycamore?
– mikeserv
Aug 7 at 0:52




1




1




Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
– Proximo
Aug 7 at 1:47




Toxic comments, even I mentioned there could be a better way. User1133275, thanks for the suggestion but I need more flexibility when specifying the range. I'll answer my own question for the benefit of the community.
– Proximo
Aug 7 at 1:47












Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
– Kusalananda
Aug 7 at 6:14





Where is your files contents coming from? There might be a better way to get the filenames than from that file, e.g. using find against the actual files in the filesystem (and their timestamps). That is what @don_crissti meant by XY problem. You are may be focusing on an issue which is down a path that you don't need to take while solving another problem.
– Kusalananda
Aug 7 at 6:14











1 Answer
1






active

oldest

votes

















up vote
0
down vote













So I decided not to try to use date function to transform the form and instead use more SED!




cat files





  • ..._201807010112.log




  • ..._201807010132.log




  • ..._201807010152.log




  • ..._201807010202.log



The date is in the format YYYYmmddHHSS and I transformed it to the following format:



YYYY-mm-dd HH:SS using this regex expression with SED.



cat files | sed -e 's/(_[0-9]4)([0-9]2) 
([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'


Next, from there it was a piece of cake to specify the range based on date.



 | sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'


The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.






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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f460912%2fuse-sed-regex-capture-group-in-replace-section-method%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    So I decided not to try to use date function to transform the form and instead use more SED!




    cat files





    • ..._201807010112.log




    • ..._201807010132.log




    • ..._201807010152.log




    • ..._201807010202.log



    The date is in the format YYYYmmddHHSS and I transformed it to the following format:



    YYYY-mm-dd HH:SS using this regex expression with SED.



    cat files | sed -e 's/(_[0-9]4)([0-9]2) 
    ([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'


    Next, from there it was a piece of cake to specify the range based on date.



     | sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'


    The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.






    share|improve this answer
























      up vote
      0
      down vote













      So I decided not to try to use date function to transform the form and instead use more SED!




      cat files





      • ..._201807010112.log




      • ..._201807010132.log




      • ..._201807010152.log




      • ..._201807010202.log



      The date is in the format YYYYmmddHHSS and I transformed it to the following format:



      YYYY-mm-dd HH:SS using this regex expression with SED.



      cat files | sed -e 's/(_[0-9]4)([0-9]2) 
      ([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'


      Next, from there it was a piece of cake to specify the range based on date.



       | sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'


      The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        So I decided not to try to use date function to transform the form and instead use more SED!




        cat files





        • ..._201807010112.log




        • ..._201807010132.log




        • ..._201807010152.log




        • ..._201807010202.log



        The date is in the format YYYYmmddHHSS and I transformed it to the following format:



        YYYY-mm-dd HH:SS using this regex expression with SED.



        cat files | sed -e 's/(_[0-9]4)([0-9]2) 
        ([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'


        Next, from there it was a piece of cake to specify the range based on date.



         | sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'


        The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.






        share|improve this answer












        So I decided not to try to use date function to transform the form and instead use more SED!




        cat files





        • ..._201807010112.log




        • ..._201807010132.log




        • ..._201807010152.log




        • ..._201807010202.log



        The date is in the format YYYYmmddHHSS and I transformed it to the following format:



        YYYY-mm-dd HH:SS using this regex expression with SED.



        cat files | sed -e 's/(_[0-9]4)([0-9]2) 
        ([0-9]2)([0-9]2)([0-9]2) /1-2-3 4:5/g'


        Next, from there it was a piece of cake to specify the range based on date.



         | sed -n '/2018-07-01 01:20/,/2018-07-02 02:01/p'


        The biggest take away for me was knowing the proper date-time format to utilize SED's range pattern option.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 7 at 1:59









        Proximo

        1235




        1235



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460912%2fuse-sed-regex-capture-group-in-replace-section-method%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?