LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2013, 10:55 AM   #1
disno
LQ Newbie
 
Registered: Nov 2013
Posts: 12

Rep: Reputation: Disabled
Bash Problem


i use this command:
curl http://xxx.com | lynx -dump -listonly -nonumbers -stdin

the output i got is urls like this:
http://qq.com
http://aa.com
http://zz.com

now i got two problems some of the URLS are duplicates i want to remove the duplicates and choose a random URL and store it in paramater "QA"
can someone help me to handle this problem?

Thanks For helpers
 
Old 11-18-2013, 11:29 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
http://qq.com
http://aa.com
http://zz.com
http://qq.com
http://aa.com
http://zz.com
http://qq.com
http://aa.com
http://zz.com
... this code ...
Code:
QA=$(sort $InFile |uniq |shuf |head -1)
echo "QA=" $QA
... produces a result of this form ...
Code:
QA= http://zz.com
Daniel B. Martin
 
1 members found this post helpful.
Old 11-18-2013, 01:10 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
That's a workable solution, but it could be somewhat simplified: QA=$(sort -u ${InFile} | shuf -n 1) works as well.

Note that, if you wanted the likelihood of a URL being selected to be proportional the frequency of it's occurrence in the page, then the unique sort could be skipped and a QA=$(shuf -n 1 ${InFile}) used.

Also, if you wanted to do it all in one line, this
QA=$(curl http://xxx.com | lynx -dump -listonly -nonumbers -stdin | sort -u | shuf -n 1)
should do the trick.
 
1 members found this post helpful.
Old 11-18-2013, 02:24 PM   #4
disno
LQ Newbie
 
Registered: Nov 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
thanks guyz it works
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash problem MartenH Linux - Newbie 2 06-26-2013 02:58 PM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
[SOLVED] bash problem struggled_newbie Linux - Newbie 5 09-07-2010 03:59 AM
[SOLVED] bash : getopts problem in bash script. angel115 Programming 2 03-02-2009 10:53 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration