0

I'm not that advanced at bash script so I suspect it may be something very simple. I made a script to run a URL with curl then parse the output using grep for some ids before appending it to a text file. By appending it to a text file, the ids are space delimited so I tried to replace the spaces with newlines like this:

#!/bin/bash

IDS=$(curl -v --silent "http://example.com" 2>&1 | grep -Eo 'data-user_id="[0-9]+"' | grep -Eo "[0-9]+")

echo ${IDS} >> "ids"
sed 's/ /\n/g' "ids"

But when I run this, I see the output of my file being written to the terminal, if I remove the last line (sed), the output disappears. Before I had used tr and the same thing happened. How do I stop this from happening?

  • @don_crissti Sorry, what do you mean? – Nubcake Feb 25 '17 at 22:28
  • Ok sorry I misunderstood there. I changed it to echo "$IDS" >> "ids" but it still happens. – Nubcake Feb 25 '17 at 22:30
  • I see the output of the file being printed to the terminal. – Nubcake Feb 25 '17 at 22:31
  • Yes, I see them appearing one per line. I didn't realise that was happening, even when I used tr ' ' '\n' < "ids" – Nubcake Feb 25 '17 at 22:36
0

sed is printing the file with the substitution you told it to make.

I think a better idea might be the following:

#!/bin/bash

IDS=$(curl -v --silent "http://example.com" 2>&1 | grep -Eo 'data-user_id="[0-9]+"' | grep -Eo "[0-9]+")

echo "$IDS" | tr ' ' '\n' >> "ids"

This will echo $IDS and apply the space to new line translation before appending to the file.

0

Try it with perl

IDS=$(curl -sv http://example.com |& perl -lne 'print $1 if /data-user_id="(\d+)"/')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.