1
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.

Jobs
  • 3,317
  • 6
  • 26
  • 52
  • 2
    There are several *nix utilities to process that line one you have it... `cut`, `sed` etc. – Stephen P Apr 16 '19 at 23:18
  • *"... I have to be using ifconfig ..."* - Good luck on Fedora and others, where `ifconfig` is gone and you have to use the `ip` utility. – jww Apr 17 '19 at 01:09

8 Answers8

5

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
Tom Lubenow
  • 1,109
  • 7
  • 15
4

One way to do it ..

ifconfig | grep -m1 "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
Adam vonNieda
  • 1,635
  • 2
  • 14
  • 22
4

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
souser
  • 5,868
  • 5
  • 35
  • 50
2

Use cut with a delimiter

| cut -d':' -f 2 | cut -d' ' -f 1
Matthew Fisher
  • 2,258
  • 2
  • 14
  • 23
2

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2

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"
  • The { and } brackets define a block of commands to be run, with the commands separated by a ;
  • The s command is fairly straightforward - it just captures the IP and replaces the whole line with just the IP
  • The p 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.
  • The 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.

Mike Holt
  • 4,452
  • 1
  • 17
  • 24
1

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.

1

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.

James Brown
  • 36,089
  • 7
  • 43
  • 59
  • 1
    I like the idea of doing as much as possible in pure `bash` instead of relying on `awk`, `sed`, `cut`, et. al., but why not get rid of the `grep` as well? `[[ $(ifconfig) =~ inet\ addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) ]] && echo ${BASH_REMATCH[1]}` – Mike Holt Apr 17 '19 at 17:53