0

I'm writing a bash script that gets some data into a variable and then I need to format the variable. It holds some text in a following manner:

"sth 23 asd 2435 jasbd null asdj 123 jasd j123 asd 24 null asd 436 123jwba null"

I would like to add a newline after every null. How can I do it? My code looks like this:

output=$(curl -s 'http://127.0.0.1:3000/api/com.pokemon.go/nearby?latitude='"$latitude1"'&longitude='"$longitude1"'&altitude=0step=10&offset=0' \
  -H 'Authorization: Bearer '"$acces_token")

#bunch of formatting that works
output="${output//[}"
output="${output//]}"
output="${output//pokemons}"
output="${output//pokestops}"
output="${output//gyms}"
output="${output//gym_points}"
output="${output//{}"
output="${output//\}}"
output="${output//\"}"
output="${output//unsigned:false}"
output="${output//unsigned:true}"
output="${output//unsigned:null}"
output=${output//:/ }
output=${output//,/ }

#formatting that doesn't work
output="${output//null/null$'\n'}"

echo $output

but it doesn't work

user30935
  • 51
  • 1
  • 8
  • 1
    it does work. Did you test with quotes? `echo "$output"` and `echo "${output//null/null$'\n'}"` – fedorqui Aug 02 '16 at 10:49
  • If i do echo "$output" and then echo "${output//null/null$'\n'}" I see the newlines but when I do output="${output//null/null$'\n'}" and then echo "$output" it doesn't work and unfortunately I need it assigned to a variable – user30935 Aug 02 '16 at 10:53
  • Please [edit] showing exactly what you are doing and what is the output. I just took your sample data into `$output` and then did `r="${output/null/nullXXXX$'\n'XX}"; echo "$r"` and it worked fine on GNU Bash. – fedorqui Aug 02 '16 at 10:56
  • I've edited my question – user30935 Aug 02 '16 at 11:03
  • 1
    This is because you `echo $output`. Do `echo "$output"` instead, since double quotes preserve the format. – fedorqui Aug 02 '16 at 11:04
  • It worked! Thanks! Can I ask what's the difference between echo $output and echo "$output" – user30935 Aug 02 '16 at 11:06
  • Nice : ) See the question I marked this as duplicate to. In there you have plenty of juicy information on the topic. – fedorqui Aug 02 '16 at 11:08
  • Thanks! It was very helpful :) – user30935 Aug 02 '16 at 11:19

0 Answers0