florit
florit

Reputation: 345

jq slurp and add key / value pairs

$DATA is a long string containing some Email addresses.

echo "$DATA" | grep -Eo "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" | sort | uniq | jq --slurp --raw-input 'split("\n")[:-1]'

Output:

[
  "[email protected]",
  "[email protected]",
  "[email protected]",
  "[email protected]"
]

Desired Output:

[
  {
    "email": "[email protected]",
    "free": "0",
    "used": "0"
  },
  {
    "email": "[email protected]",
    "free": "0",
    "used": "0"
  },
  {
    "email": "[email protected]",
    "free": "0",
    "used": "0"
  },
  {
    "email": "[email protected]",
    "free": "0",
    "used": "0"
  }
]

I guess it should be something like += {"free": "0"}

Upvotes: 1

Views: 1097

Answers (1)

Aaron
Aaron

Reputation: 24812

You can replace your current jq command by the following :

jq --slurp --raw-input 'split("\n")[:-1] | map({email: ., free: 0, used: 0})'

You can try it here.

Upvotes: 0

Related Questions