Reputation:
I've been trying to remove the new line and carriage return indicators from my JSON output using this answer.
I've also tried running this based on another answer I've seen on Stack Overflow, but it still does not appear to be working:
sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' text.json > text_clean.json
My current .sh
script looks like this:
getPage() {
curl --fail -X GET 'https://api.test.com/v1/marx?page=1&pageSize=1000&sort=desc' \
-H 'Authorization: Bearer xxxxxxx' \
-H 'cache-control: no-cache'
}
getPage \
| jq -c '.data[]' \
> text.json
What am I missing here?
If it helps, an example of the string containing it takes on many different forms in the output, but here's a good one:
Ok! Thank you so much.\r\nBest,\r\nClaire\r\n\r\n
Upvotes: 5
Views: 9824
Reputation: 204
I would use tr
for this.
In you example, try the following:
echo "Ok! Thank you so much.\r\nBest,\r\nClaire\r\n\r\n"| tr -d '\012\015'
Upvotes: 2
Reputation: 1955
You can remove the characters by using perl
if you have it installed:
echo "Ok! Thank you so much.\r\nBest,\r\nClaire\r\n\r\n" | perl -pe 's/\r\n//g'
Upvotes: 0
Reputation: 295315
If you want to modify JSON, do it in jq
.
In this case, that might mean changing:
getPage \
| jq -c '.data[]' \
> text.json
...to...
getPage \
| jq -c '.data[] | sub("\r\n"; " ") | sub("\n"; " ")' \
> text.json
Upvotes: 3