Extract an IP address from a custom text file usin

2019-08-28 09:06发布

问题:

I have a text file with following contents:

NAME                       REGION        ADDRESS         STATUS
instance-name              europe-west1  1.2.3.4         IN_USE
instance-name-2            europe-west1  1.3.2.4         IN_USE
instance-name-3            europe-west1  1.5.3.2         IN_USE

I want to extract the IP address only from "instance-name-3". How would it be possible in that situation?

For example, this allows me to find all the IP addresses, but I only want the "instance-name-3" one:

grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0
-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" iplist.txt

回答1:

pBit more compact alternative to @waymobetta's solution

awk '/^instance-name-3  /{print $3}' your_file.txt


回答2:

If column 1 is instance-name-3 then print column 3:

awk '$1=="instance-name-3" {print $3}' file

Output:

1.5.3.2


回答3:

You could try something like the following:

cat your_file.txt | grep 'instance-name-3' | awk '{print $3}'

This will pull out only the IP address (column 3) of the row with name instance-name-3

result:



标签: bash grep