how to change last value (ip address) with sed

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











up vote
0
down vote

favorite












I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24



Thank you for your support and best greetings!



 varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )









share|improve this question





















  • Maybe this is easier to parse: hostname -i | sed 's/[^.]*$/0/24/'
    – Valentin B
    Oct 9 '17 at 19:55










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34














up vote
0
down vote

favorite












I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24



Thank you for your support and best greetings!



 varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )









share|improve this question





















  • Maybe this is easier to parse: hostname -i | sed 's/[^.]*$/0/24/'
    – Valentin B
    Oct 9 '17 at 19:55










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24



Thank you for your support and best greetings!



 varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )









share|improve this question













I read the current ip address with following command line. after then i need to replace the last value with 0/24
For example. The current ip ist 192.168.178.1 and i need to replace the var value in 192.168.178.0/24



Thank you for your support and best greetings!



 varip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' )






shell-script awk sed ip replace






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 9 '17 at 19:32









mikeeyy

1




1











  • Maybe this is easier to parse: hostname -i | sed 's/[^.]*$/0/24/'
    – Valentin B
    Oct 9 '17 at 19:55










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34
















  • Maybe this is easier to parse: hostname -i | sed 's/[^.]*$/0/24/'
    – Valentin B
    Oct 9 '17 at 19:55










  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
    – Kusalananda
    Oct 12 '17 at 19:34















Maybe this is easier to parse: hostname -i | sed 's/[^.]*$/0/24/'
– Valentin B
Oct 9 '17 at 19:55




Maybe this is easier to parse: hostname -i | sed 's/[^.]*$/0/24/'
– Valentin B
Oct 9 '17 at 19:55












If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
– Kusalananda
Oct 12 '17 at 19:34




If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
– Kusalananda
Oct 12 '17 at 19:34










5 Answers
5






active

oldest

votes

















up vote
1
down vote













Use the following sed code at the end:



sed 's:[^.]*$:0/24:'


It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s, so that I can use '/' un-escaped.




Or go with sed all the way :)



ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'





share|improve this answer





























    up vote
    1
    down vote













    Try this:



    ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'


    But you do not need cut, replace it with sed:



    ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'


    This command return the same output.



    And you do not need tail, replace it with sed too:



    ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'


    But the shortest way is to use hostname instead:



    hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'


    Output will be the same.






    share|improve this answer





























      up vote
      0
      down vote













      To get Your-IP-Adress/24 use the following command:



      ip addr | grep inet | egrep /24 | awk 'print $2'





      share|improve this answer



























        up vote
        0
        down vote













        Here is a perl one liner:



        perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'


        It is predicated on the HOSTNAME environmental variable being set to the proper machine name.






        share|improve this answer





























          up vote
          0
          down vote













          Just using Awk:



          ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'





          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%2f397107%2fhow-to-change-last-value-ip-address-with-sed%23new-answer', 'question_page');

            );

            Post as a guest






























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Use the following sed code at the end:



            sed 's:[^.]*$:0/24:'


            It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s, so that I can use '/' un-escaped.




            Or go with sed all the way :)



            ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'





            share|improve this answer


























              up vote
              1
              down vote













              Use the following sed code at the end:



              sed 's:[^.]*$:0/24:'


              It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s, so that I can use '/' un-escaped.




              Or go with sed all the way :)



              ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'





              share|improve this answer
























                up vote
                1
                down vote










                up vote
                1
                down vote









                Use the following sed code at the end:



                sed 's:[^.]*$:0/24:'


                It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s, so that I can use '/' un-escaped.




                Or go with sed all the way :)



                ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'





                share|improve this answer














                Use the following sed code at the end:



                sed 's:[^.]*$:0/24:'


                It works by replacing the last occurring sub-string that doesn't contain '.', with '0/24'. Note how I use ':' as separator for the sed command s, so that I can use '/' un-escaped.




                Or go with sed all the way :)



                ip addr | sed -rn '/state UP/n;n;s:^ *[^ ]* *([^ ]*).*:1:;s:[^.]*$:0/24:p'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 9 '17 at 20:14

























                answered Oct 9 '17 at 19:49









                seshoumara

                25127




                25127






















                    up vote
                    1
                    down vote













                    Try this:



                    ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'


                    But you do not need cut, replace it with sed:



                    ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'


                    This command return the same output.



                    And you do not need tail, replace it with sed too:



                    ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'


                    But the shortest way is to use hostname instead:



                    hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'


                    Output will be the same.






                    share|improve this answer


























                      up vote
                      1
                      down vote













                      Try this:



                      ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'


                      But you do not need cut, replace it with sed:



                      ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'


                      This command return the same output.



                      And you do not need tail, replace it with sed too:



                      ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'


                      But the shortest way is to use hostname instead:



                      hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'


                      Output will be the same.






                      share|improve this answer
























                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        Try this:



                        ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'


                        But you do not need cut, replace it with sed:



                        ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'


                        This command return the same output.



                        And you do not need tail, replace it with sed too:



                        ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'


                        But the shortest way is to use hostname instead:



                        hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'


                        Output will be the same.






                        share|improve this answer














                        Try this:



                        ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | cut -f1 -d'/' | sed 's/.[0-9]*$/.0/24/'


                        But you do not need cut, replace it with sed:



                        ip addr | grep 'state UP' -A2 | tail -n1 | awk 'print $2' | sed 's/.[0-9]*//.0//'


                        This command return the same output.



                        And you do not need tail, replace it with sed too:



                        ip addr | grep 'state UP' -A2 | awk 'print $2' | sed '$!d;s/.[0-9]*//.0//'


                        But the shortest way is to use hostname instead:



                        hostname -I | tr -d " " | sed 's/[0-9]*$/0/24/'


                        Output will be the same.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 9 '17 at 20:18

























                        answered Oct 9 '17 at 19:46









                        Egor Vasilyev

                        1,792129




                        1,792129




















                            up vote
                            0
                            down vote













                            To get Your-IP-Adress/24 use the following command:



                            ip addr | grep inet | egrep /24 | awk 'print $2'





                            share|improve this answer
























                              up vote
                              0
                              down vote













                              To get Your-IP-Adress/24 use the following command:



                              ip addr | grep inet | egrep /24 | awk 'print $2'





                              share|improve this answer






















                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                To get Your-IP-Adress/24 use the following command:



                                ip addr | grep inet | egrep /24 | awk 'print $2'





                                share|improve this answer












                                To get Your-IP-Adress/24 use the following command:



                                ip addr | grep inet | egrep /24 | awk 'print $2'






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 9 '17 at 20:07









                                GAD3R

                                22.7k154895




                                22.7k154895




















                                    up vote
                                    0
                                    down vote













                                    Here is a perl one liner:



                                    perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'


                                    It is predicated on the HOSTNAME environmental variable being set to the proper machine name.






                                    share|improve this answer


























                                      up vote
                                      0
                                      down vote













                                      Here is a perl one liner:



                                      perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'


                                      It is predicated on the HOSTNAME environmental variable being set to the proper machine name.






                                      share|improve this answer
























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        Here is a perl one liner:



                                        perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'


                                        It is predicated on the HOSTNAME environmental variable being set to the proper machine name.






                                        share|improve this answer














                                        Here is a perl one liner:



                                        perl -MSys::Hostname -MEnv -e ' print join ".",( unpack("C4",(gethostbyname($HOSTNAME))[4]))[0..2],"0/24n";'


                                        It is predicated on the HOSTNAME environmental variable being set to the proper machine name.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Oct 9 '17 at 20:13









                                        GAD3R

                                        22.7k154895




                                        22.7k154895










                                        answered Oct 9 '17 at 19:49









                                        DannyK

                                        20413




                                        20413




















                                            up vote
                                            0
                                            down vote













                                            Just using Awk:



                                            ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'





                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote













                                              Just using Awk:



                                              ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'





                                              share|improve this answer






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Just using Awk:



                                                ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'





                                                share|improve this answer












                                                Just using Awk:



                                                ip a | awk '/state UP/nr[NR+2]; NR in nr gsub(/.([0-9]+)/([0-9]+)/,".0/24"); print $2'






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Oct 9 '17 at 23:36









                                                jasonwryan

                                                47.1k14127178




                                                47.1k14127178



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f397107%2fhow-to-change-last-value-ip-address-with-sed%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?