sort question

Tim Chase blinux.list at
Tue Jun 24 12:02:07 UTC 2008

> I'd like to sort, maybe cut, and redirect text lines based on the 3 or 4
> last chars; is this possible? how?

One key element you omit is whether the lines are all the same 
length, or if they have varying lengths.

If they're all the same length, you can just determine the 
offsets and use those for sorting.

However, if they have a ragged right-edge (aren't the same 
length), you'll need to do a bit more work.

If I understand what you're trying to do, you want to isolate the 
last couple characters in the line, throwing away the rest of the 
line, sort+unique the remainder, and then dump the results to a 
file.  That could be done with sed and sort:

   sed 's/.*\(...\)$/\1/' src.txt | sort -u > nodupes.txt

That should do the last three characters ("...").  If you need 4, 
just add an extra period.  I used the "-u" functionality of sort 
which is the same as a basic sort+uniq call (if you need some of 
uniq's other functionality like counting, you'd have to use it 
too).  The only caveat is the semi-undefined behavior if a line 
has less than 3 or 4 characters in it, which this handles, and 
just treats it as its own thing to be sorted/uniq'ed/output.

Hope this helps,


More information about the Blinux-list mailing list