Modify a substring without altering the string in Bash












-1















I have this string:



14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


I want to parse it to delete minutes and seconds from the hour. The result would be the next:



14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:



cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries


Thanks!










share|improve this question



























    -1















    I have this string:



    14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


    I want to parse it to delete minutes and seconds from the hour. The result would be the next:



    14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


    I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:



    cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries


    Thanks!










    share|improve this question

























      -1












      -1








      -1








      I have this string:



      14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


      I want to parse it to delete minutes and seconds from the hour. The result would be the next:



      14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


      I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:



      cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries


      Thanks!










      share|improve this question














      I have this string:



      14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


      I want to parse it to delete minutes and seconds from the hour. The result would be the next:



      14-Nov-2018 10 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)


      I get this string from a file, so It would be perfect if It is achieved with a pipe. My current line is as follows:



      cat $dns_logs | grep "$date" | HERE WOULD BE YOUR SOLUTION > $temp_queries


      Thanks!







      linux bash shell awk grep






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 9:19









      Miguel.GMiguel.G

      9810




      9810
























          3 Answers
          3






          active

          oldest

          votes


















          0














          sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'





          share|improve this answer


























          • Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

            – Miguel.G
            Nov 14 '18 at 9:49



















          0














          If you are ok with awk then could you please try following.



          echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" | 
          awk '
          match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
          print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
          next
          }
          1'





          share|improve this answer



















          • 1





            Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

            – Miguel.G
            Nov 14 '18 at 9:50











          • @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

            – RavinderSingh13
            Nov 15 '18 at 1:40





















          0














          The whole line can be done as



          awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"





          share|improve this answer

























            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%2f53296679%2fmodify-a-substring-without-altering-the-string-in-bash%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'





            share|improve this answer


























            • Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

              – Miguel.G
              Nov 14 '18 at 9:49
















            0














            sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'





            share|improve this answer


























            • Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

              – Miguel.G
              Nov 14 '18 at 9:49














            0












            0








            0







            sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'





            share|improve this answer















            sed -r 's/(:[0-9]{2}){2}.[0-9]{3} //'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 9:57

























            answered Nov 14 '18 at 9:26









            Martin HeraleckýMartin Heralecký

            3,06721034




            3,06721034













            • Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

              – Miguel.G
              Nov 14 '18 at 9:49



















            • Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

              – Miguel.G
              Nov 14 '18 at 9:49

















            Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

            – Miguel.G
            Nov 14 '18 at 9:49





            Thanks! I modify your solution and It works: sed -r 's/:[0-9]{2}:[0-9]{2}.[0-9]{3} / /g'.

            – Miguel.G
            Nov 14 '18 at 9:49













            0














            If you are ok with awk then could you please try following.



            echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" | 
            awk '
            match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
            print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
            next
            }
            1'





            share|improve this answer



















            • 1





              Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

              – Miguel.G
              Nov 14 '18 at 9:50











            • @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

              – RavinderSingh13
              Nov 15 '18 at 1:40


















            0














            If you are ok with awk then could you please try following.



            echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" | 
            awk '
            match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
            print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
            next
            }
            1'





            share|improve this answer



















            • 1





              Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

              – Miguel.G
              Nov 14 '18 at 9:50











            • @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

              – RavinderSingh13
              Nov 15 '18 at 1:40
















            0












            0








            0







            If you are ok with awk then could you please try following.



            echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" | 
            awk '
            match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
            print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
            next
            }
            1'





            share|improve this answer













            If you are ok with awk then could you please try following.



            echo "14-Nov-2018 10:14:44.775 client IP1#59098: view internal: query: DOMAIN IN A + (IP2)" | 
            awk '
            match($0,/[0-9]+:[0-9]+:[0-9]+.[0-9]+/){
            print substr($0,1,RSTART-1),substr(substr($0,RSTART,RLENGTH),1,2),substr($0,RSTART+RLENGTH+1)
            next
            }
            1'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 9:31









            RavinderSingh13RavinderSingh13

            27.3k41538




            27.3k41538








            • 1





              Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

              – Miguel.G
              Nov 14 '18 at 9:50











            • @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

              – RavinderSingh13
              Nov 15 '18 at 1:40
















            • 1





              Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

              – Miguel.G
              Nov 14 '18 at 9:50











            • @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

              – RavinderSingh13
              Nov 15 '18 at 1:40










            1




            1





            Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

            – Miguel.G
            Nov 14 '18 at 9:50





            Wow thanks! That is interesting since I didn't know how to match inside AWK. Thanks again!

            – Miguel.G
            Nov 14 '18 at 9:50













            @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

            – RavinderSingh13
            Nov 15 '18 at 1:40







            @Miguel.G, your welcome, you could try to encourage people by up-voting their helpful answers too on SO.

            – RavinderSingh13
            Nov 15 '18 at 1:40













            0














            The whole line can be done as



            awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"





            share|improve this answer






























              0














              The whole line can be done as



              awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"





              share|improve this answer




























                0












                0








                0







                The whole line can be done as



                awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"





                share|improve this answer















                The whole line can be done as



                awk -v d="$date" '($0 ~ d){$2=substr($2,1,2); print}' "$dns_logs"






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 6:20

























                answered Nov 14 '18 at 18:08









                kvantourkvantour

                8,63731330




                8,63731330






























                    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%2f53296679%2fmodify-a-substring-without-altering-the-string-in-bash%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

                    Bressuire

                    Vorschmack

                    Quarantine