[SOLVED] (BASH) How to assign 2 variables to same line?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello to all.
I'm trying to write a small bash funtion that retrieves Xbox Live Deal of the Week info.
Here's what i have so far:
Code:
xbox()
{
# get xbox live deals of the week game titles
xbox_name=$(curl -s http://marketplace.xbox.com/en-US/Promotion/dealoftheweek|grep -i "a class"|grep -i "title="|cut -d"\"" -f6)
# get deal prices
xbox_price=$(curl -s http://marketplace.xbox.com/en-US/Promotion/dealoftheweek|grep -i "mspoints goldprice"|cut -d">" -f3|cut -d"<" -f1)
# display info
echo -e "$xbox_name""\n$xbox_price"
}
I'm not very proficient so my method of obtaining the title and price may be a bit crude but grep and cut are tools i'm familiar with. I'd love to hear any other ways to do it.
My issue is getting the title and price on the same line. As of now, the output looks like this:
Code:
Defense Grid
Orcs Must Die!
Dungeon Defenders
South Park
400
600
800
400
I'd like it to look like this:
Code:
Defense Grid 400
Orcs Must Die! 600
Dungeon Defenders 800
South Park 400
Should i be trying to put both the title and price inside it's own variable (if so how would one achieve this?) or is there a way to echo it?
Any help is much obliged.
Last edited by Dick Dastardly; 01-14-2012 at 05:39 PM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
"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
I'd like to point out that, for efficiency purposes, it's generally better to download the entire data set only once, and parse the saved text as needed.
Code:
xdata=$( curl -s http://marketplace.xbox.com/en-US/Promotion/dealoftheweek | grep -e "ProductBox" -e "GoldPrice" )
mapfile -t titles < <( sed -rn 's/.*title="([^"]*)".*/\1/p' <<<"$xdata" )
mapfile -t prices < <( sed -rn 's/.*GoldPrice ProductPrice">([^<]+)<.*/\1/p' <<<"$xdata" )
for i in "${!titles[@]}"; do
tabs='\t'
(( ${#titles[i]} <= 16 )) && tabs='\t\t'
printf "%s$tabs%s\n" "${titles[i]}" "${prices[i]}"
done
I used bash 4's new mapfile command to set the arrays instead of the IFS technique Roken used above, and I've simplified his loop a bit. Otherwise it's the same basic procedure. You might even want to add more tabs and more tests for them in case you get some really long tiles.
Edit: I just want to say, grail's use of the column command to format the output is an excellent suggestion. You can modify the loop above to use it like this:
Code:
for i in "${!titles[@]}"; do
printf "%s|%s\n" "${titles[i]}" "${prices[i]}"
done | column -t -s '|'
Last edited by David the H.; 01-15-2012 at 01:53 AM.
Reason: as stated
Yeah, ever since I realized that mapfile could also be used internally for parsing lines from commands, I've found myself using it more and more. It can often be quite a bit less hassle than running it through a while+read loop.
Speaking of which, I'd also written up a traditional loop version that replaced the sed commands with bash regexes, but eventually decided it wasn't worth cluttering up my post with it. I guess it won't hurt to post it now, though.
Distribution: Arch, Debian, LFS (debian and LFS relegated to backups)
Posts: 182
Rep:
Quote:
Originally Posted by David the H.
I'd like to point out that, for efficiency purposes, it's generally better to download the entire data set only once, and parse the saved text as needed.
I agree. As I mentioned in my original reply, I didn't really look at the data gathering code since it worked, and I assumed that this is an occasional script anyway, so not big deal re the extra download.
Having said that, mapfile and column I wasn't even aware of. Couple of new things for me to become familiar with
Thanks for all the great info. I love the way there's always more than one way to do it.
I'll have to do a lot of reading to understand most of what you guys are telling me.
I'll have a play around and see what i come up with.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.