0

I used a command to get a list of IP from EC2.

The result so far i got is a list like this:

[
    "172.31.11.7",
    "172.31.48.141",
    "172.31.64.201",
    "172.31.64.149",
    "172.31.64.148",
    "172.31.64.111"
]

I want to get result as:

172.31.11.7
172.31.48.141
172.31.64.201
172.31.64.149
172.31.64.148
172.31.64.111

How can i do that?

Kusalananda
  • 333,661
The One
  • 4,862

5 Answers5

3

Using jq:

jq -r '.[]' file

Using egrep:

egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' file

or

egrep -o '([0-9]+\.){3}[0-9]+' file

Using grep:

grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' file
pLumo
  • 22,565
1

The data is a JSON array. Using jq to extract the elements:

$ jq -r '.[]' file
172.31.11.7
172.31.48.141
172.31.64.201
172.31.64.149
172.31.64.148
172.31.64.111
Kusalananda
  • 333,661
1

using AWK,

  awk -F '"' '{if (NF==3) print $2}' file
  • using as a delimiter
  • printing second filed $2
  • NF==3 if the maximum number of field is equal to 3
Siva
  • 9,077
Stack EG
  • 1,636
1

For that sample,

cut -d'"' -sf2

would be enough. It returns the part of the line between the first and second " occurrence and (with -s) discards the lines that don't contain any ".

0
awk -F\" 'NF>1{print $2}' file
Kamaraj
  • 4,365