Reputation: 3377
ifconfig | grep -m1 "inet addr"
Gives me
inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0
However, I only want the ip, which is 172.30.1.6
. How can I do this?
Note that I have to be using ifconfig, as this is an embedded system with limited functionalities.
Upvotes: 1
Views: 1549
Reputation: 37404
Using Bash's regex operator =~
:
$ [[ $(ifconfig | grep -m1 "inet addr") =~ [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]] && echo ${BASH_REMATCH[0]}
172.30.1.6
Update: Something even better in the comments.
Upvotes: 1
Reputation: 4612
Here's a way to do it with a single sed
command, eliminating the call to grep
:
ifconfig | sed -n '/inet addr/{s/^.*inet addr:\([^ ]*\).*$/\1/p;q}'
There are a few things going on here:
sed -n
tells sed
not to print every line like it normally does/inet addr/
is a sed
address - it tells sed
to only operate on lines containing "inet addr"
{
and }
brackets define a block of commands to be run, with the commands separated by a ;
s
command is fairly straightforward - it just captures the IP and replaces the whole line with just the IPp
flag at the end of the s
command tells sed
to print the result of the substitution. This is necessary because we called sed
with the -n
option.q
command tells sed
to quit, so that it only processes the first line containing "inet addr"
.Using the -n
option, the /inet addr/
address, the p
flag on the s
command, and the q
command, essentially has the same effect as grep -m1 "inet addr"
, which makes calling grep
unnecessary. In fact, it's worth noting that the following commands produce identical output:
> ifconfig | grep -m1 "inet addr"
inet addr:192.168.1.1 Bcast:192.168.2.255 Mask:255.255.255.0
> ifconfig | sed -n '/inet addr/{p;q}'
inet addr:192.168.1.1 Bcast:192.168.2.255 Mask:255.255.255.0
Here, I've omitted the s/pattern/replacement/p
part of the sed
command, and replaced it with a p
command (which just prints the whole line), just to show the effect of the other parts in isolation.
Upvotes: 2
Reputation: 203597
Is this all you're trying to do?
awk -F'[: ]' '/inet addr/{print $3; exit}'
For example using cat file
in place of ifconfig
:
$ cat file
inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0
$ cat file | awk -F'[: ]' '/inet addr/{print $3; exit}'
172.30.1.6
Upvotes: 2
Reputation: 71
Just use the command cut.
ip a | grep -m1 "inet addr" | cut -d':' -f 2 | cut -d' ' -f 1
I also advise you to learn the use of other commands such as : wc,sed,tr,sort,uniq. They will help manipulate the output as you please. Here is a small lesson where we present you all these command : https://www.javatpoint.com/linux-filters
I hope to help you.
Upvotes: 1
Reputation: 6120
If all you want to do is obtain the ip address, there might be easier ways of achieving that using say hostname -i ( reference Which terminal command to get just IP address and nothing else? )
Since others have mentioned cut and awk, I will provide a solution using sed :
echo "inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0" | sed -e "s/.*\(addr:[^ ]*\) .*/\1/" addr:172.30.1.6 echo "inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0" | sed -e "s/.*addr:\([^ ]*\) .*/\1/" 172.30.1.6
Upvotes: 4
Reputation: 1161
Get out your scissors, it's cuttin' time.
echo inet addr:172.30.1.6 Bcast:172.30.140.255 Mask:255.255.252.0 | cut -d : -f 2 | cut -d " " -f 1
Upvotes: 5
Reputation: 1745
One way to do it ..
ifconfig | grep -m1 "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
Upvotes: 4