Reputation: 3097
How can I convert the shell command output to an array so I can print variables as uniqueMetrics[0]
, uniqueMetrics[1]
etc?
const uniqueMetrics = await execShellCommand('aws cloudwatch list-metrics --namespace AWS/ApiGateway | jq -r .Metrics[].Dimensions[].Name | sort -u')
console.log(uniqueMetrics);
output:
ApiId
ApiName
Stage
It seems the data is returned as follows with \r
and \n
.
[ 'ApiId\r\nApiName\r\nStage\r\n' ]
So I'm guessing I'd need to search and replace these strings first. Am I thinking right?
Upvotes: 0
Views: 758
Reputation: 344
You should be able to use String.split() function:
let uniqueMetricsArray = uniqueMetrics.split('\r\n');
For better compatibility, it might make sense to make it work for both \r\n and just \n using RegExp:
let uniqueMetricsArray = uniqueMetrics.split(/\r?\n/);
Upvotes: 1