0

The following command:

jq ".[] | .file.url_private_download" *json

generates many lines in the form of:

"https://files.slack.com/files-pri/T27SFGS2W-F78LH1DN2/download/img_1964.jpg?t=xoxe-243624297126-248125875671-248125975751-cee1f8d9a1"

What is the simplest way to prepend a wget command in front of each of these lines ?

Thank you very much

0

3 Answers 3

4

Here's your command with only a few modifications:

jq -r '.[] | @sh "wget \(.file.url_private_download)"' *json

The changes:

  • Use -r to get decoded strings as output instead of JSON-encoded strings.

  • Create strings that start with wget followed by the value that you need from the structure, using \(...) to interpolate the value from the document into the string.

  • Use @sh to properly quote the generated string for the shell. This ensures that the interpolated value in each string is safely quoted for use in the shell.

With the above command, the entry that contains the URL that you show is expected to generate the following line as output:

wget 'https://files.slack.com/files-pri/T27SFGS2W-F78LH1DN2/download/img_1964.jpg?t=xoxe-243624297126-248125875671-248125975751-cee1f8d9a1'
0
1

You could use xargs to prepend the command you want to each line:

jq ".[] | .file.url_private_download" *json | xargs -n1 /bin/echo "wget"  
1
  • thank you, btw wget has an option to read URLs from a file (but curl on my Mac does not AFAIK) Sep 28, 2017 at 8:13
1

You could use sed to rewrite the start of each line:

jq ".[] | .file.url_private_download" *json  | sed 's/^/wget /'

which "replaces" the start of the line with whatever the replacement pattern is, here wget

Or to wget all the files:

jq ".[] | .file.url_private_download" *json  | wget -i -
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .