LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 08-20-2009, 04:40 PM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Rep: Reputation: 17
[bash] sort string and discard duplicates


I need a script that accepts a character string as parameter, that sorts the characters in descending order, discards any duplicates and echoes the result to stdout.

The string only contains numbers [0-9].

Any ideas are much appreciated!
 
Old 08-20-2009, 05:31 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
This sounds like homework....

Please tell us more about the context and why you need to do this.

Is there a file with many of these strings, or do you just need to do it occasionally?
 
Old 08-20-2009, 06:03 PM   #3
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
It's not homework. I need a solution to a real-life problem.

The calling program runs once a day and will need to call the script about 30 times. It produces a set of numbers between 0 and 9, which are required in descending order without duplicates. The numbers can be passed as separate parameters or as one string (I prefer the latter). The result can only be returned via stdout.

Last edited by hashbang#!; 08-20-2009 at 06:05 PM.
 
Old 08-20-2009, 06:23 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
How does:
Code:
#!/bin/bash
echo $1 | sed 's/./&\n/g' | head -n -1 | sort -r | uniq
work?

Or if you want them back on one line
Code:
#!/bin/bash
echo $1 | sed 's/./&\n/g' | head -n -1 | sort -r | uniq | awk '{ printf "%s",$0; } END { printf "%s","\n" }'
(Cue someone with a 4 character long perl script that does the same thing...)

Dave

Last edited by ilikejam; 08-20-2009 at 06:34 PM.
 
Old 08-20-2009, 06:46 PM   #5
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Fantastic! I just need the line feeds removed:

Code:
#!/bin/bash
echo $1 | sed 's/./&\n/g' | head -n -1 | sort -r | uniq | tr -d '\n'

Oops - we were editing our message at the same time.
Many thanks, Dave. I would have never come up with the sed replacement.


I'm sure this could be done more efficiently in perl.

Last edited by hashbang#!; 08-20-2009 at 06:49 PM.
 
Old 08-20-2009, 06:50 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
tr -d '\n'

I really need to do something about my awk habit. It's not healthy.
 
Old 08-20-2009, 07:20 PM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Using only Bash:-
Code:
unset result
for n in {9..0};do
  if [ "${1/$n/}" != "$1" ]; then result=$result$n; fi
done
echo $result
 
Old 08-21-2009, 03:33 AM   #8
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by ilikejam
Code:
 ...| awk '{ printf "%s",$0; } END { printf "%s","\n" }'
I really need to do something about my awk habit. It's not healthy.
awk has its place.

You could have shortened your awk command above by setting the line separator to empty:
Code:
awk 'BEGIN {ORS=""}{print $0}'
 
Old 08-21-2009, 03:44 AM   #9
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Thank you for two smart solutions!

By the way, why is this stupid Windoze flag displayed under my profile?
Edit - just realized it's taken from my user agent string - I am identifying as Win98 because I am fed up with sites refusing to operate under Linux.

Last edited by hashbang#!; 08-21-2009 at 03:46 AM.
 
Old 08-21-2009, 05:43 AM   #10
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Off-topic, but do you still find websites that require IE/Windows? I haven't seen any of that for years.

Dave
 
Old 08-21-2009, 06:17 AM   #11
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
OT

Quote:
Originally Posted by ilikejam View Post
Off-topic, but do you still find websites that require IE/Windows? I haven't seen any of that for years.
I have not come across a site that explicitly requires IE.

It happened to me often enough that I changed the default user agent string to Firefox/Windoze.

One example springs to mind: Abbey online banking

I just tested this: it's not Linux that's the problem but Iceweasel (Debian's name for Firefox).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash sort problem ArthurHuang Programming 4 05-02-2009 05:20 PM
bash regex - sort czezz Programming 3 02-05-2009 07:41 PM
BASH out duplicates from multiple text files smudge|lala Linux - General 3 09-24-2008 07:51 PM
Bash script to sort image files dtcs Programming 5 09-26-2006 09:50 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 02:57 PM


All times are GMT -5. The time now is 02:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration