LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] sort string and discard duplicates (http://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-sort-string-and-discard-duplicates-749134/)

hashbang#! 08-20-2009 04:40 PM

[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!

pixellany 08-20-2009 05:31 PM

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?

hashbang#! 08-20-2009 06:03 PM

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.

ilikejam 08-20-2009 06:23 PM

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

hashbang#! 08-20-2009 06:46 PM

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.

ilikejam 08-20-2009 06:50 PM

tr -d '\n'

I really need to do something about my awk habit. It's not healthy.

Kenhelm 08-20-2009 07:20 PM

Using only Bash:-
Code:

unset result
for n in {9..0};do
  if [ "${1/$n/}" != "$1" ]; then result=$result$n; fi
done
echo $result


hashbang#! 08-21-2009 03:33 AM

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}'

hashbang#! 08-21-2009 03:44 AM

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.

ilikejam 08-21-2009 05:43 AM

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

Dave

hashbang#! 08-21-2009 06:17 AM

OT
 
Quote:

Originally Posted by ilikejam (Post 3652176)
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).


All times are GMT -5. The time now is 06:48 AM.