Grepping and sedding IP from ip addr show
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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
add a comment |
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
@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
add a comment |
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
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
sed arch-linux grep ip
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
add a comment |
@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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 28 '15 at 22:05
cascas
39.6k456103
39.6k456103
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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