Help me to resolve uncertian behaviour of a sort command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me to resolve uncertian behaviour of a sort command
# 1  
Old 12-21-2005
Data Help me to resolve uncertian behaviour of a sort command

I have got a file BeforeSort.txt having 40 fields seperated by "|"
First field= RecordType (Value will be P or FP)
Second field= CamCode
Third field = UpdatingDate
Fourth field = ProductType
Fifth field = ActionCode (Value may be 01, 02 or 03)
Sixth field = ProductCode
and so on

My requirement is that to sort the above file to get unique records based on Second, Fourth and Sixth fields AND also
(1) If there are multiple records for above 3 key fields and with first field RecordType P and FP then take only P record
(2) If there are multiple records for above 3 key fields and with first field RecordType P only then take only P record
(3) If there are multiple records for above 3 key fields and with first field RecordType FP only then take only FP record


I used following sort command to get required records as above :
sort -ur -t "|" -k 2,2 -k 4,4 -k 6,6 BeforeSort.txt > AfterSort.txt

(I am attaching file BeforeSort.txt herewith)

This command is working fine for this small file.
==============

Now the actual problem :
But when I executed the above command for a real big file PRinLG90_PFP and redirecting the output in file PRinLG90_PFPSort. Then this output file contains some unexpected record for some of ProductCodes.
(I have attached file PRinLG90_PFP.txt in zip file PRinLG90_PFP.zip)

sort -ur -t "|" -k 2,2 -k 4,4 -k 6,6 PRinLG90_PFP.txt > PRinLG90PFPSort.txt

For Example :

(1) For sixth field ProductCode R5V2*L503BB6 there are two records with first field RecordType = P & FP and fourth field CamCode = 0049 in file PRinLG90_PFP. And in file PRinLG90_PFPSort there is record of first field RecordType = P
(Which is the required and right output record).

(2)But for sixth field ProductCode R8V2*L503BB6 there are two records with first field RecordType = P & FP and fourth field CamCode = 0049 in file PRinLG90_PFP. But in file PRinLG90_PFPSort there is record of first field RecordType = FP
(While our requirement is that ,it should be P record instead of FP record)


I could not understand the above different behaviour of this used sort command.

could I take your expert help on this to know the reason/action of this uncertain behaviour of this above sort command.
Please help me, because this command is to be used to send a very important and urget delivery


Many Thanks & Regards,
Pankaj Rai

Last edited by pankajrai; 12-21-2005 at 08:04 AM..
# 2  
Old 12-21-2005
I'm not going to download that big file. But I think I see the problem. Your field 1 is "P" or "FP". After you sort on the other three fields, if you have a collection of lines for the 3 sort keys, you really need the first line in the collection to be a P record. This means that you must sort on field 1. Try this:

sort -r -t \| -k2,2 -k4,4 -k6,6 -k1,1 < data | sort -rum -t \| -k2,2 -k 4,4 -k6,6
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Red Hat

Ps command different behaviour

Hi Experts, ps command behavior in Redhat is such that it outputs all the output(of long lengths). In Unix the ps command output was limited to only 80 chars. In that if you pipe its output to another command hen the 80 chars restriction wouldn't be there. This 80 char limitation will only be... (14 Replies)
Discussion started by: Albert_Pinto7
14 Replies

2. Programming

Sort behaviour

I see strange results when sorting with -n options and I wander if somebody can explain it. Input file and two results: $ cat aa 14 -1 11 -1 0 -1 0 $ sort -u aa -1 0 (1 Reply)
Discussion started by: migurus
1 Replies

3. Shell Programming and Scripting

Behaviour of pwd command in sh and ksh

I have a script as below. bash-3.00$ cat test.sh #!/usr/bin/ksh path=`pwd` echo $path var=$path/temp11 echo $var If run it is giving output bash-3.00$ ksh test.sh //var/tmp/SB2/miscellaneous //var/tmp/SB2/miscellaneous/temp11 (5 Replies)
Discussion started by: millan
5 Replies

4. Shell Programming and Scripting

Exec command behaviour

Hello All, I came across this line in a shell script that I was looking at yesterday. Couldn't get a clue of what it does. :confused: .... .... blah blah .... ..... exec >&- 2>&- ...... blah blah ..... Can someone explain me this?? Usually I have seen exec commands used to... (3 Replies)
Discussion started by: PikK45
3 Replies

5. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

6. Shell Programming and Scripting

Is this "Out of Memory!" error due to sort() of perl how to resolve it?

I am running a program written in perl script and it is stopped with "Out of memory!" error. This is very strange because at the time then the program is aborted, it only used 4GB RAM and there are still 30GB free physical memory left in the system. I check the perl script and found the program... (3 Replies)
Discussion started by: lilili07
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. UNIX for Dummies Questions & Answers

shell: reconcile language and sort behaviour

Hi Don't know if this is a dummy question, but let's give it a try. I yesterday had a problem with undefined behaviour in the sort shell command (I'm using bash), leading to different sort orders without apparent reasons. I resolved this by typing export LC_ALL="C" export LC_COLLATE="C"... (5 Replies)
Discussion started by: jossojjos
5 Replies

9. UNIX for Advanced & Expert Users

Sort command - strange behaviour

Hi guys, I have the following example data: A;00:00:19 B;00:01:02 C;00:00:13 D;00:00:16 E;00:02:27 F;00:00:12 G;00:00:21 H;00:00:19 I;00:00:13 J;00:13:22 I run the following sort against it, yet the output is as follows: sort -t";" +1 -nr example_data.dat A;00:00:19 (16 Replies)
Discussion started by: miwinter
16 Replies
Login or Register to Ask a Question