LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk - sort characters within each line (https://www.linuxquestions.org/questions/programming-9/awk-sort-characters-within-each-line-945586/)

danielbmartin 05-17-2012 03:04 PM

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
Now I want to do something similar but have been unable to modify it to do the job. I have a file with one character string per line. The characters in this example file are numerics but the question applies to alphanumerics.

Have:
Code:

0100
0101
0102
0103
0104
0110
0111
0112
0113
0114
0120

Want:
Code:

0001
0011
0012
0013
0014
0011
0111
0112
0113
0114
0012

Daniel B. Martin

Kustom42 05-17-2012 03:15 PM

the sort command will do that for you by itself.

Kustom42 05-17-2012 03:17 PM

Wait, after looking at your output there is no real sorting method there, how are you trying to sort these?

colucix 05-17-2012 03:17 PM

I suggest something similar but simpler:
Code:

awk '{ split($0,array,""); for ( i = 1; i <= asort(array); i++ ) printf array[i]; printf "\n" }' file
The sorting process depends on your locale definitions and specifically from LC_COLLATE. You can use IGNORECASE as well.

danielbmartin 05-17-2012 03:40 PM

Quote:

Originally Posted by colucix (Post 4681197)
Code:

awk '{ split($0,array,""); for ( i = 1; i <= asort(array); i++ ) printf array[i]; printf "\n" }' file

Ooh, that is sweet! Thank you!

Daniel B. Martin

grail 05-18-2012 09:39 AM

And a bit of ruby:
Code:

ruby -ane 'BEGIN{$; = ""};puts $F[0..-2].sort.join' file

or

ruby -pe '$_ = $_.chomp.split("").sort.join + "\n"' file


jachaja 11-07-2012 07:20 PM

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.

danielbmartin 11-07-2012 08:52 PM

Quote:

Originally Posted by jachaja (Post 4824610)
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.

Try this ...
Code:

echo "Sort on columns 3-4 (ascending) and columns 16-19 (descending)."
sort -k1.3,1.4 -k1.16,1.19r $InFile > $OutFile

Daniel B. Martin


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