Sorting Difficulties


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting Difficulties
# 1  
Old 09-21-2008
Sorting Difficulties

Hey guys,

I am sort of new to unix and I am having difficulty sorting. What I am trying to do is sort a particular field in reversed order and another field in alphabetic order to create a new file that looks like this:

MILLER,EDWARD AL 14 101293 9341
MOORE,HENRY 15 101297 9077
NGUYEN,LE HUU 13 101314 9576

*NOTE THAT THE SPACINGS ARE NOT DISPLAYED CORRECTLY; IT SHOULD BE ALL LINED UP

here is what I tried:

sort -r +0.20 -0.24 +0.00 -0.16 "input file" > "output file"

Basically, "+0.20 -0.24" represents the first two digit numbers that needs to be reversed and "+0.00 -0.16" represents the names that needs to be in alphabetic order.

can someone help me?

Last edited by nthamma; 09-21-2008 at 03:40 PM..
# 2  
Old 09-21-2008
why don't you use sort -k to specify the field and -t as field seperator
this is just an example..
Quote:
sort -t: +1 -2 -n +0 -1 -r filename
This command sequence performs a numeric sort on the second field (+1 -2 -n). Within that ordering, it sorts the first field in reverse alphabetic order (+0 -1 -r)

just go through man page of sort..
# 3  
Old 09-21-2008
How do I specify the field?
# 4  
Old 09-21-2008
if you have any field seperator like ',' you can go for -t option
or else try this...

Quote:
sort +0.00 -0.16 -d +0.20 -0.24 -r filename
# 5  
Old 09-21-2008
it sorted the two digit numbers in reversed order which is what I want but instead of sorting the names in alphabetic order, it reversed it as well.

Last edited by nthamma; 09-21-2008 at 04:08 PM..
# 6  
Old 09-21-2008
then you should specify some field seperator..
paste some part of input file and color the part which you wanna sort...

Last edited by vidyadhar85; 09-21-2008 at 04:24 PM..
# 7  
Old 09-21-2008
I got it sorted!

What I did was give out this command:

sort-r +0.20 -0.24 -k 1,1d "input file" > "output file"

I could not specify the field for the two digit numbers because the names are not homogeneous. What I mean by that is that some of the names have 1 field and some of them have 2 fields:

Code:
MILLER,EDWARD AL   14 101293 9341
MOORE,HENRY        15 101297 9077
NGUYEN,LE HUU      13 101314 9576

You can see from above that the first and third name have 2 fields because of the middle name initials separated from the full names by a space. So, for those names, to get to the two digit numbers, you have to specify the 3rd field but for the name in the middle, you only have to specify up to the 2nd field.

So, what I did was count the spaces and use columns to specify the two digit numbers and reversed them. Next, I used the -k option to specify the 1st field for the names in alphabetic order using -d (sort in "phone directory" order). And it made my day! Took me so long to figure that one out and your replies really helped me understood how the whole thing works. Thanks for your help!

Last edited by Perderabo; 09-21-2008 at 05:32 PM.. Reason: Add code tags for readability
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difficulties in matching left bracket as literal in awk

I need to work with records having #AX in the EXP1 , please see my data sample and my attempt below: $ cat xx 08:30:33 KEY1 (1255) EXP1 VAL:20AX0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AX0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AW0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AW0030006 $ gawk '{... (1 Reply)
Discussion started by: migurus
1 Replies

2. UNIX for Dummies Questions & Answers

Facing difficulties to untar a tra.gz file

Hi , I trying to cp a files to one location and trying to untar there,the tar.gz files get copied to the specified location but wont untar. Here is what i did if cp /path/of/the/file.tar.gz /path/to/the/file/file.tar.gz then tar -zxvf /path/to/the/file/file.tar.gz echo "FILE... (2 Replies)
Discussion started by: vikatakavi
2 Replies

3. Red Hat

Difficulties with pam_tally2! :-(

Dear All I have having some trouble with pam_tally2 on RHEL 6.0 which I am getting quite frustrated with and I am sure it is a simple problem. I have the following lines in system-auth and password-auth: - auth required pam_tally2.so onerr=fail file=/var/log/faillog... (1 Reply)
Discussion started by: gz3xzf
1 Replies

4. Homework & Coursework Questions

Sorting help

i have list of files: Wang De Wong CVPR 09.pdf Yaacob AFGR 99 Second edition.pdf Shimon CVPR 01.pdf Den CCC 97 long one.pdf Ronald De Bour CSPP 04.pdf ..... how can i sort this directory so the output will be in the next format: <year>\t<conference/journal>\t<author list> - t is tab (its... (1 Reply)
Discussion started by: nirnir26
1 Replies

5. UNIX for Dummies Questions & Answers

Sorting help

how can i sort the next list just by look at the numbers (ignore letters) example: abc123 dff4f aaa2aa bbbb55555bb output: aaa2aa dff4f abc123 bbbb55555bb (1 Reply)
Discussion started by: nirnir26
1 Replies

6. Filesystems, Disks and Memory

iSCSI client setup difficulties

I am trying to set up iscsi linux clients and am having some problems. iscsid is running, I can do discovery fine, but adding the iscsi lun I get the following error: iscsiadm -m node -T iqn.xxxxxxxxxxxxxx -p y.y.y.y:3260 -l Logging in to iscsid: session already running. iscsiadm: Could not... (0 Replies)
Discussion started by: humbletech99
0 Replies

7. Shell Programming and Scripting

trying to cope with awk difficulties

The data we are searching is populated in this way: ----IP---------DAY----MONTH----DATE--------TIME---------YEAR 12.3234.34-----Fri------Nov-------15-------18:05:14 GMT---2008 I want the user to be able to search for the data according to month and year. However, I cannot quite figure out... (8 Replies)
Discussion started by: amatuer_lee_3
8 Replies

8. Shell Programming and Scripting

Sorting :

Hi .... iam having a file with 20,000 rows and 56 colums. Iam trying to sort a file ....for that iam using 'sort' command. If iam having 3 and 5 key colums etc ... as numerical this 'sort' command will work? what ihave to do ? sample example : E100|0|19940102|1|11111|E003... (3 Replies)
Discussion started by: satyam_sat
3 Replies

9. IP Networking

Proxy ARP Difficulties

edited ... (7 Replies)
Discussion started by: TheMaskedMan
7 Replies

10. Programming

Simple Network Program Difficulties

I'm trying to write 2 programs, client & server, that communicate with integers, however, all resources I have found on the net assume that you want to send and recieve information as a character array. I don't want to send my integers as characters, I want to send them as ints (casting them to... (2 Replies)
Discussion started by: Mistwolf
2 Replies
Login or Register to Ask a Question