Sort Issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort Issue
# 1  
Old 02-03-2010
Sort Issue

these are two records(adr.txt)trying to sort with the the code expl below.
5423|336110|2730 Pierce St|Ste 300|Sioux City|IA|Woodbury|51104|3765||42518651|96405013|A|2|3|U|12/08/2009
5423|14462335|624 JONES ST|STE 5400|Sioux City|IA|Woodbury|51101|||42496648|96400644|A|8|2|U |12/24/2009
Code:
nawk -F'|' '{n=split($NF, a, "[ /]"); gsub(":", "", a[n]);printf("%d%02d%02d%s%s\n", \
a[3], a[1], a[2], a[n], OFS $0)}' OFS='|' adr.txt | sort -t '|' -k2n,2 -k17,17 -k16n,16 \
-k15n,15r -k1n,1r -k3n,3r | cut -d '|' -f2-

The result is reverse as i expect the bottome one to come top...
...The issue here is ("THE LAST BUT ONE FIELD IS NOT CONSISTENT AS IT HAS A SPACE AFTER "U" IN ONE ROW AND NOT IN OTHER ROW")... Either with or without spaces in two rows the above sort works fine
I just want to ignore the space in that filed while sorting...Any ideas please?
Thanks

Last edited by vbe; 02-03-2010 at 11:19 AM.. Reason: writing code isnt enough... you need code tags!
# 2  
Old 02-03-2010
Sort Issue

Hi

Code:
nawk -F'|' '{n=split($NF, a, "[ /]"); gsub(":", "", a[n]);printf("%d%02d%02d%s%s\n", a[3], a[1], a[2], a[n], OFS $0)}' OFS='|' adr.txt \
| sort -t '|' -k2n,2 -k17.1,17.1 -k16n,16 -k15n,15r -k1n,1r -k3n,3r \
| cut -d '|' -f2-

Assuming the column containing your "U"-values is column number 17.

Alternatively, sed to strip unnecessary whitespace.

Also noticed that your AWK builds a first column like this:
Code:
200908142009|...

It won't do any damage, but you perhaps meant to build
Code:
20090814|...

instead?

BRgds
JG

Last edited by jgrogan; 02-03-2010 at 01:01 PM.. Reason: Bad edit last time
# 3  
Old 02-03-2010
JG,

It works absolutely fine...

Just one more issue if you have few minutes...

Like you said , the 17th filed is being sorted with the way you suggested 17.1 which works fine...

I just want to take no chance with other fileds too with the space issue like in 17th.

for example, in 16th filed the value can be anywhere between 1 to 99. I want to consider only values , I mean just one value in case of single digit and 2 values in case of double digit. I dont find any DOC on this...

please suggest me if you have any ideas...hope I am clear

Thanks
# 4  
Old 02-04-2010
Sort Issue

Hi

I would strip all unnecessary whitespace

Code:
sed -e "s/[ ]*|[ ]*/|/g" -e "s/^[ ]*//" -e "s/[ ]*$//"

and change the sort condition for column 17 back to as you had it the first place, giving you something like (untested):

Code:
sed -e "s/[ ]*|[ ]*/|/g" -e "s/^[ ]*//" -e "s/[ ]*$//" adr.txt \
| nawk -F'|' '{n=split($NF, a, "[ /]"); gsub(":", "", a[n]);printf("%d%02d%02d%s%s\n", a[3], a[1], a[2], a[n], OFS $0)}' OFS='|' \
| sort -t '|' -k2n,2 -k17,17 -k16n,16 -k15n,15r -k1n,1r -k3n,3r \
| cut -d '|' -f2-

Hope this helps
JG
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Difference of Sort -n -k2 -k3 & Sort -n -k2,3

Hi, Could anyone kindly show me a link or explain the difference between sort -n -k2 -k3 & sort -n -k2,3 Also, if I like to remove the row with repetition at both $2 and $3, Can I safely use sort -u -k2 -k3 Example; 100 20 30 100 20 30 So, both $2 and $3 are same and I... (2 Replies)
Discussion started by: Indra2011
2 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

Alternate to sort --random-sort

sort --random-sort The full command is path=`find /testdir -maxdepth 1 -mindepth 1 -type d | ***Some sort of sort function*** | head -1` I have a list I want to randomly sort. It works fine in ubuntu but on a 'osx lion' sort dosen't have the --random-sort option. I don't want to... (5 Replies)
Discussion started by: digitalviking
5 Replies

5. Shell Programming and Scripting

Issue with Sort in Unix

Hi All, I am trying to sort the below data using sort command. temp.dat H|S1-511091486889|27-Jul-2011 00:00:00 H|S1-511091486823|27-Jul-2011 00:00:00 H|S1-511091486757|27-Jul-2011 00:00:00 L|S1-511091486889|1 L|S1-511091486823|1 L|S1-511091486757|1 sort -t "|" -k2 -k1 temp.dat My... (5 Replies)
Discussion started by: deepaknbk
5 Replies

6. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

8. Shell Programming and Scripting

sort date issue

Hi Everyone, # cat b Sat 12 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 11:32:49 AM MYT; Sat 13 Sep 2009 10:31:49 PM MYT;a;a;a;Mon 14 Sep 2009 10:31:49 PM MYT; Sat 14 Sep 2009 10:31:49 PM MYT;a;a;a;Sun 13 Sep 2009 10:31:49 PM MYT; # sort -t';' -k5 b Sat 13 Sep 2009 10:31:49 PM... (8 Replies)
Discussion started by: jimmy_y
8 Replies

9. UNIX for Dummies Questions & Answers

sort issue

Hi Guys , Please help me I am having a requirement as below: $ ls -i log* | head 66486532 log1 66486662 log10 66486663 log11 66486664 log12 66486665 log13 66486533 log2 66486534 log3 66486535 log4 66486584 log5 66486590 log6 This id listing the first 10 files starting with log. ... (4 Replies)
Discussion started by: mraghunandanan
4 Replies

10. Shell Programming and Scripting

performance issue using gzcat, awk and sort

hi all, I was able to do a script to gather a few files and sort them. here it is: #!/usr/bin/ksh ls *mainFile* |cut -c20-21 | sort > temp set -A line_array i=0 file_name='temp' while read file_line do line_array=${file_line} let i=${i}+1 (5 Replies)
Discussion started by: naoseionome
5 Replies
Login or Register to Ask a Question