awk - sort characters within each line
Some months ago I asked for an awk which would sort all words within each line.
firstfire provided this excellent solution using asort. Code:
awk -v IGNORECASE=1 '{gsub(/[[:punct:]]/, ""); split($0, w); s=""; for(i=1; i<=asort(w); i++) s=s w[i] " "; print s }' infile.txt Have: Code:
0100 Code:
0001 |
the sort command will do that for you by itself.
|
Wait, after looking at your output there is no real sorting method there, how are you trying to sort these?
|
I suggest something similar but simpler:
Code:
awk '{ split($0,array,""); for ( i = 1; i <= asort(array); i++ ) printf array[i]; printf "\n" }' file |
Quote:
Daniel B. Martin |
And a bit of ruby:
Code:
ruby -ane 'BEGIN{$; = ""};puts $F[0..-2].sort.join' file |
Hi all,
I have a file with a lot of records similar to this: 023456AB2543999210709 022656AB2543999210409 024556AB2543999200709 024556AB2543999200909 I want to sort this file by using two parts of each record, for example: positions 3 to 4 like one field ascending and positions 16 to 19 like other field descending. The result: 022656AB2543999210409 023456AB2543999210709 024556AB2543999200909 024556AB2543999200709 It is posible? Thanks. |
Quote:
Code:
echo "Sort on columns 3-4 (ascending) and columns 16-19 (descending)." |
All times are GMT -5. The time now is 08:44 PM. |