Skip to content

Instantly share code, notes, and snippets.

@sullust
Created August 24, 2012 17:20
Show Gist options
  • Save sullust/3453120 to your computer and use it in GitHub Desktop.
Save sullust/3453120 to your computer and use it in GitHub Desktop.
BASH combine two data sources into an array
This will do it:
Code:
#!/bin/bash
IFS='
'
xbox_name=($(curl -s http://marketplace.xbox.com/en-US/Promotion/dealoftheweek|grep -i "a class"|grep -i "title="|cut -d""" -f6))
xbox_price=($(curl -s http://marketplace.xbox.com/en-US/Promotion/dealoftheweek|grep -i "mspoints goldprice"|cut -d">" -f3|cut -d"<" -f1))
loop=${#xbox_name[*]}
for ((counter=0; counter<=$loop; counter++))
do
echo "${xbox_name[$counter]}" "${xbox_price[$counter]}"
done
"IFS=" forces bash to delimit at line breaks rather than spaces
The next two lines read name and price into arrays
loop= gets the number of array items
And the rest iterates through the array, echoing the name and corresponding price on each line.
I didn't bother looking at how you are reading the input since it seems to do the job.
Note, if you want to tabulate it like your example you'll have to play with your formatting for the echo - but I'm not doing everything for you
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment