Reverse sort on delimited chunks within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reverse sort on delimited chunks within a file
# 8  
Old 09-19-2012
Not the most efficient, but seems to work:
Code:
$ awk -v RS="" -v ORS="\n" -v OFS="\n" -F"\n" '{ print "\n"$1 ; sub(/^[^\n]*\n/, ""); print | "sort" ; close("sort") }' data

#awsekar
! aaosekar=.....
! aausekar=.....
! aousekar=.....
! auosekar=.....
! ausekar=......
! aushekar=.....
! avasekar=......
! avsekar=......
! awasekar=......
! awsekar=......

#ayaaj
! aayaj=....
! aayaz=....
! aiyaz=.....
! ayaaj=
! ayaaz=.......
! ayaj=.....
! ayaja=.....
! ayaz=.......
! ayaza=.....
! ayyaj=.......
! ayyaz=......

#ayeza
! aaeesa=....
! aaeesha=....
! aaesha=.....
! aaisa=....
! aaisha=....
! aayasa=....
! aayasha=....
! aayeesha=.....
! aayesha=....
! aayeshaa=....
! aayisha=.....
! aaysa=....
! aaysha=....
! aeesa=....
! aeesha=....
! aesha=.....
! aeysha=....
! aiesha=.....
! aisa=....
! aisha=....
! aiyasha=....
! aiyesha=....
! aiysha=....
! ayaesha=.....
! ayasa=....
! ayasha=....
! ayeesha=.....
! ayesha=....
! ayeshah=.....
! ayeshaha=....
! ayeza=....
! ayisha=.....
! aysa=....
! aysha=....

Make it | "sort -r" to reverse the order..
These 2 Users Gave Thanks to Corona688 For This Post:
# 9  
Old 09-19-2012
PROCINFO[] is only supported in GNU awk; awk --version should report something like "GNU Awk 4.0.1".



Otherwise Corona688's solution is quite acceptable for small to mid-sized data files.
This User Gave Thanks to Chubler_XL For This Post:
# 10  
Old 09-19-2012
Here's a version which uses one sort. Should be much more efficient than my first version.

Code:
$ awk -v RS="" -v ORS="\n" -v OFS="\n" -F"\n" '{
        $1=sprintf("%08d\t%s", ++Z, $1);
        Z++; for(N=2; N<=NF; N++)
        $N=sprintf("%08d\t%s", Z, $N) } 1' data |
        sort | sed 's/^[^\t]*\t//;s/#/\n#/'


#awsekar
! aaosekar=.....
! aausekar=.....
! aousekar=.....
! auosekar=.....
! ausekar=......
! aushekar=.....
! avasekar=......
! avsekar=......
! awasekar=......
! awsekar=......

#ayaaj
! aayaj=....
! aayaz=....
! aiyaz=.....
! ayaaj=
! ayaaz=.......
! ayaj=.....
! ayaja=.....
! ayaz=.......
! ayaza=.....
! ayyaj=.......
! ayyaz=......

#ayeza
! aaeesa=....
! aaeesha=....
! aaesha=.....
! aaisa=....
! aaisha=....
! aayasa=....
! aayasha=....
! aayeesha=.....
! aayesha=....
! aayeshaa=....
! aayisha=.....
! aaysa=....
! aaysha=....
! aeesa=....
! aeesha=....
! aesha=.....
! aeysha=....
! aiesha=.....
! aisa=....
! aisha=....
! aiyasha=....
! aiyesha=....
! aiysha=....
! ayaesha=.....
! ayasa=....
! ayasha=....
! ayeesha=.....
! ayesha=....
! ayeshah=.....
! ayeshaha=....
! ayeza=....
! ayisha=.....
! aysa=....
! aysha=....

$

It works by prepending the same number to each line in a group of names, forcing sort to group them. Once they're sorted, sed strips the numbers back off and puts the newlines back in.
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 09-19-2012
Many thanks.
The first solution handles middle sized files. As soon as a large file is given it seems to scatter data
I can't use the second solution, Since I am working under windows, the sed command doesn't work for me.
Many thanks all the same for attacking the problem.
# 12  
Old 09-19-2012
Gimley,

If you can get gawk 4.0.1 for windows, I just downloaded it and tested it here and the sort works fine (3.1.6 dosn't sort).
# 13  
Old 09-21-2012
Windows is known to react badly to rapidly launching thousands of small processes -- some sort of table fills up faster than it can empty and processes start failing to launch -- so yeah, I can see my first version not working properly in Windows. In UNIX, it would work, but at suboptimal speed.

Here's the single-sort version rewritten without sed.

Code:
awk -v RS="" -v ORS="\n" -v OFS="\n" -F"\n" '{
        $1=sprintf("%08d\t%s", ++Z, $1);
        Z++; for(N=2; N<=NF; N++)
        $N=sprintf("%08d\t%s", Z, $N) } 1' data |
        sort | awk '{ sub(/^[^!#]*/, ""); sub(/^#/, "\n#"); } 1'


#awsekar
! aaosekar=.....
! aausekar=.....
! aousekar=.....
! auosekar=.....
! ausekar=......
! aushekar=.....
! avasekar=......
! avsekar=......
! awasekar=......
! awsekar=......

#ayaaj
! aayaj=....
! aayaz=....
! aiyaz=.....
! ayaaj=
! ayaaz=.......
! ayaj=.....
! ayaja=.....
! ayaz=.......
! ayaza=.....
! ayyaj=.......
! ayyaz=......

#ayeza
! aaeesa=....
! aaeesha=....
! aaesha=.....
! aaisa=....
! aaisha=....
! aayasa=....
! aayasha=....
! aayeesha=.....
! aayesha=....
! aayeshaa=....
! aayisha=.....
! aaysa=....
! aaysha=....
! aeesa=....
! aeesha=....
! aesha=.....
! aeysha=....
! aiesha=.....
! aisa=....
! aisha=....
! aiyasha=....
! aiyesha=....
! aiysha=....
! ayaesha=.....
! ayasa=....
! ayasha=....
! ayeesha=.....
! ayesha=....
! ayeshah=.....
! ayeshaha=....
! ayeza=....
! ayisha=.....
! aysa=....
! aysha=....

$

You could also use Busybox for Windows to fill out your missing system utilities. It's a single executable which bundles all of these:
Code:
[, [[, ar, ash, awk, base64, basename, bash, bbconfig, bunzip2, bzcat,
bzip2, cal, cat, catv, cksum, cmp, comm, cp, cpio, cut, date, dc, dd,
diff, dirname, dos2unix, echo, ed, egrep, env, expand, expr, false,
fgrep, find, fold, getopt, grep, gunzip, gzip, hd, head, hexdump, kill,
killall, length, ls, lzcat, lzma, lzop, lzopcat, md5sum, mkdir, mv, od,
pgrep, pidof, printenv, printf, ps, pwd, rm, rmdir, rpm2cpio, sed, seq,
sh, sha1sum, sha256sum, sha512sum, sleep, sort, split, strings, sum,
tac, tail, tar, tee, test, touch, tr, true, uncompress, unexpand, uniq,
unix2dos, unlzma, unlzop, unxz, unzip, usleep, uudecode, uuencode, vi,
wc, wget, which, whoami, xargs, xz, xzcat, yes, zcat

Particularly useful is the built-in shell, since it can run pipe chains properly, and will know how to use all the above commands without prepending 'busybox.exe ' to them.

Its built-in BASH is not actually BASH shell, just the same Bourne you get with sh, but a quite reasonably functional shell nonetheless.

Last edited by Corona688; 09-21-2012 at 12:37 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort tab delimited file according to which rows have missing values

Hello! I have a tab delimited file with values in three columns. Some values occur in all three columns, other values are present in only one or two columns. I would like to sort the file so that rows with no missing values come first, rows with one missing values come next, and rows with two... (9 Replies)
Discussion started by: MBarrett1213
9 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Reverse the order of a list of file names (but not sort them alphabetically or numerically)

Hello all, I have a list of file names in a text document where each file name consists of 4 letters and 3 numbers (for example MACR119). There are 48 file names in the document (they are not in alphabetical or numerical order). I would like to reorder the list of names so that the 48th name is... (3 Replies)
Discussion started by: MDeBiasse
3 Replies

3. Shell Programming and Scripting

Reverse sort

Hello, I have a large list of names and would like to do a reverse sort on them i.e. the sort should be by the ending and not by the beginning of the word. I had written in awk a small script but it does wrong things { for(i=length($0);i>=1;i--) printf("%s/n",substr($0,i,1)); } Could anyone... (3 Replies)
Discussion started by: gimley
3 Replies

4. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

5. Shell Programming and Scripting

reverse sort file

Hi all I am trying to numerically reverse sort a file but I seem to be having trouble. Example of file contents: text1,1 text2,-1 text3,0 I can sort using sort -k 2n -t, filename without any problems. However I want my results in descending order but using -r in my command... (2 Replies)
Discussion started by: pxy2d1
2 Replies

6. UNIX for Dummies Questions & Answers

Sort the fields in a comma delimited file

Hi, I have a comma delimited file. I want to sort the fields alphabetically and again store them in a comma delimited file. For example, My file looks like this. abc,aaa,xyz,xxx,def pqr,ggg,eee,iii,qqq zyx,lmo,pqr,abc,fff and I want my output to look like this, all fields sorted... (3 Replies)
Discussion started by: swethapatil
3 Replies

7. UNIX for Dummies Questions & Answers

sort -reverse order

I need to sort the particular column only in reverse order how i can give it.. if i give the -r option the whole file is getting sorted in reverse order. 1st 2nd col 3rd C col 4th col 5th col ------------------------------------------- C... (7 Replies)
Discussion started by: sivakumar.rj
7 Replies

8. Shell Programming and Scripting

reverse sort

Hello, How do i sort a csv file. i should be sorting column1(varchar),column2*(varchar) in ascending and column4 in descending order(numeric datatype). I tried few combinations of sort, but doesn't seem to be getting the right result. sort -t "," -k 1 -k 2 -k 4nr file any help is... (3 Replies)
Discussion started by: markjason
3 Replies

9. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies

10. Shell Programming and Scripting

sort a file in reverse order

I a file with log entries... I want to sort it so that the last line in the file is first and the first line is last.. eg. Sample file 1 h a f 8 6 After sort should look like 6 8 f a h 1 (11 Replies)
Discussion started by: frustrated1
11 Replies
Login or Register to Ask a Question