I'm trying to come up with a quick command in order to get the IP address of a computer via grep
and the ifconfig
command.
So far, I have this ifconfig eth0 | grep -Eo 'inet addr:[0-9\\.]+'
ifconfig eth0 | grep -Eo 'inet addr:[0-9\\.]+'
which will return:
inet addr:192.168.1.26
I'm trying to adjust the regex though so that I only get the IP address itself. I have very limited knowledge of regex, and I put a non-capturing group around 'inet addr:' which made the command look like this:
ifconfig eth0 | grep -Eo '(?:inet addr:)[0-9\\.]+'
but that still didn't solve my issue.
You can use:
ifconfig | awk '$1=="inet" && $2!="127.0.0.1"{print $2}'
Or on Linux:
ifconfig eth0 | awk -F '[ :]+' '$2=="inet" {print $4}'
You can use cut
, to cut output into substrings by delimiter :
.
ifconfig eth0 | grep -Eo 'inet addr:[0-9\.]+' | cut -d':' -f 2
Flag -f 2
is used to select second substring.
Use a lookbehind expression; with grep
, you need to add the -P
switch to enable this construct instead of -E
(at least under bash v4.1.1 for cygwin). And so just change a little your last try:
ifconfig eth0 | grep -Po '(?<=inet addr:)[0-9\.]+'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.