Home > Back-end >  Remove Special Charactors from Az command Output using Az CLI
Remove Special Charactors from Az command Output using Az CLI

Time:11-11

I'm Trying to get only IP address from below command using Azure cli. But it always coming with extra "".

az network nic ip-config list --resource-group "RG_TEST" --nic-name "TEST_NIC6768" --query "[0].privateIpAddress"

Output: "10.244.4.4"

Required Output: 10.244.4.4

CodePudding user response:

Pipe the output to |ForEach-Object Trim '"' to have PowerShell automatically strip leading and trailing "'s from the output:

az network nic ip-config list --resource-group "RG_TEST" --nic-name "TEST_NIC6768" --query "[0].privateIpAddress" |ForEach-Object Trim '"'

CodePudding user response:

The az CLI outputs JSON data by default, which in the case of a string value means that it is output with enclosing "...".

While you can remove those " characters afterwards, it is better to ask az not to include them in the first place, which you can do with --output tsv (-o tsv):

# Note the addition of `--output tsv`
az network nic ip-config list --output tsv --resource-group "RG_TEST" --nic-name "TEST_NIC6768" --query "[0].privateIpAddress"

Note:

  • --output tsv requests the output in TSV (tab-separated values) form, which in the case of a single string means that it is printed as-is (verbatim); aside from not having to remove enclosing "...", this also means that you never need to worry about unescaping JSON-escaped characters such as \ and embedded ".

  • az supports additional output formats.


The quick-and-dirty alternative is to simply remove all " chars. with a -replace operation:

(
  az network nic ip-config list --resource-group "RG_TEST" --nic-name "TEST_NIC6768" --query "[0].privateIpAddress"
) -replace '"'
  • Related