Grepping and sedding IP from ip addr show

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed.



inet <<192.168.0.16>>/24 brd 192.1680.255 scope global enp0s3 $ I want the IP in <<>>
ip addr show | grep 'inet ' | sed -n -e 's/^.*inet (.*)/.*$/1/p'
-n # print nothing by default
s # replacement command
^ # begin line
.* # anything
inet # inet and then a space
(.*) # capture anything
/ # end capture at the / that comes before 24
.* # anything
$ # end
1 # replace all that with the first capture group which should be the IP
p # print the output


But as soon as I add the sed, it gives me nothing. I assume something is wrong with my regex.










share|improve this question
























  • @don_crissti Oh right! Thanks, I forgot about that. That actually fixes my problem. Sorry for the bad information.

    – Rogue
    Nov 28 '15 at 21:52

















2















I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed.



inet <<192.168.0.16>>/24 brd 192.1680.255 scope global enp0s3 $ I want the IP in <<>>
ip addr show | grep 'inet ' | sed -n -e 's/^.*inet (.*)/.*$/1/p'
-n # print nothing by default
s # replacement command
^ # begin line
.* # anything
inet # inet and then a space
(.*) # capture anything
/ # end capture at the / that comes before 24
.* # anything
$ # end
1 # replace all that with the first capture group which should be the IP
p # print the output


But as soon as I add the sed, it gives me nothing. I assume something is wrong with my regex.










share|improve this question
























  • @don_crissti Oh right! Thanks, I forgot about that. That actually fixes my problem. Sorry for the bad information.

    – Rogue
    Nov 28 '15 at 21:52













2












2








2








I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed.



inet <<192.168.0.16>>/24 brd 192.1680.255 scope global enp0s3 $ I want the IP in <<>>
ip addr show | grep 'inet ' | sed -n -e 's/^.*inet (.*)/.*$/1/p'
-n # print nothing by default
s # replacement command
^ # begin line
.* # anything
inet # inet and then a space
(.*) # capture anything
/ # end capture at the / that comes before 24
.* # anything
$ # end
1 # replace all that with the first capture group which should be the IP
p # print the output


But as soon as I add the sed, it gives me nothing. I assume something is wrong with my regex.










share|improve this question
















I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed.



inet <<192.168.0.16>>/24 brd 192.1680.255 scope global enp0s3 $ I want the IP in <<>>
ip addr show | grep 'inet ' | sed -n -e 's/^.*inet (.*)/.*$/1/p'
-n # print nothing by default
s # replacement command
^ # begin line
.* # anything
inet # inet and then a space
(.*) # capture anything
/ # end capture at the / that comes before 24
.* # anything
$ # end
1 # replace all that with the first capture group which should be the IP
p # print the output


But as soon as I add the sed, it gives me nothing. I assume something is wrong with my regex.







sed arch-linux grep ip






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 18 at 3:10









Rui F Ribeiro

42.1k1484142




42.1k1484142










asked Nov 28 '15 at 21:47









RogueRogue

1508




1508












  • @don_crissti Oh right! Thanks, I forgot about that. That actually fixes my problem. Sorry for the bad information.

    – Rogue
    Nov 28 '15 at 21:52

















  • @don_crissti Oh right! Thanks, I forgot about that. That actually fixes my problem. Sorry for the bad information.

    – Rogue
    Nov 28 '15 at 21:52
















@don_crissti Oh right! Thanks, I forgot about that. That actually fixes my problem. Sorry for the bad information.

– Rogue
Nov 28 '15 at 21:52





@don_crissti Oh right! Thanks, I forgot about that. That actually fixes my problem. Sorry for the bad information.

– Rogue
Nov 28 '15 at 21:52










1 Answer
1






active

oldest

votes


















4














This is easier to do with awk than grep and sed:



ip addr show eth0 | awk '/inet / print $2'


If you want to strip the CIDR netmask from the IP:



ip addr show eth0 | awk '/inet / gsub(//.*/,"",$2); print $2'


Note that an interface may have more than one IP address - e.g. ip addr show br0 | awk '/inet / print $2' on my system has 11 IPv4 addresses, some of them being public IP addresses and some of them RFC1918 private addresses.






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',
    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%2f246127%2fgrepping-and-sedding-ip-from-ip-addr-show%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









    4














    This is easier to do with awk than grep and sed:



    ip addr show eth0 | awk '/inet / print $2'


    If you want to strip the CIDR netmask from the IP:



    ip addr show eth0 | awk '/inet / gsub(//.*/,"",$2); print $2'


    Note that an interface may have more than one IP address - e.g. ip addr show br0 | awk '/inet / print $2' on my system has 11 IPv4 addresses, some of them being public IP addresses and some of them RFC1918 private addresses.






    share|improve this answer



























      4














      This is easier to do with awk than grep and sed:



      ip addr show eth0 | awk '/inet / print $2'


      If you want to strip the CIDR netmask from the IP:



      ip addr show eth0 | awk '/inet / gsub(//.*/,"",$2); print $2'


      Note that an interface may have more than one IP address - e.g. ip addr show br0 | awk '/inet / print $2' on my system has 11 IPv4 addresses, some of them being public IP addresses and some of them RFC1918 private addresses.






      share|improve this answer

























        4












        4








        4







        This is easier to do with awk than grep and sed:



        ip addr show eth0 | awk '/inet / print $2'


        If you want to strip the CIDR netmask from the IP:



        ip addr show eth0 | awk '/inet / gsub(//.*/,"",$2); print $2'


        Note that an interface may have more than one IP address - e.g. ip addr show br0 | awk '/inet / print $2' on my system has 11 IPv4 addresses, some of them being public IP addresses and some of them RFC1918 private addresses.






        share|improve this answer













        This is easier to do with awk than grep and sed:



        ip addr show eth0 | awk '/inet / print $2'


        If you want to strip the CIDR netmask from the IP:



        ip addr show eth0 | awk '/inet / gsub(//.*/,"",$2); print $2'


        Note that an interface may have more than one IP address - e.g. ip addr show br0 | awk '/inet / print $2' on my system has 11 IPv4 addresses, some of them being public IP addresses and some of them RFC1918 private addresses.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '15 at 22:05









        cascas

        39.6k456103




        39.6k456103



























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f246127%2fgrepping-and-sedding-ip-from-ip-addr-show%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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay