sort by 2nd & 3rd fields

sort by 2nd & 3rd fields

Post by Harr » Sat, 11 Jun 2005 02:01:08



I have a text file, after parsing by a few cmd's, yeild something
like this below.

 TypeA-ProductAAA-10
 TypeA-ProductAAA-12
 TypeA-ProductAAA-14
 TypeA-ProductAAA-15
 TypeA-ProductAAA-17
 ... [snip]
 TypeB-ProductC-176a
 ... [snip]
 TypeB-BBB-29
 TypeB-BBB-35
 TypeB-BBB-38
 TypeB-BBB-45
 TypeB-ProductAAA-1
 TypeB-ProductAAA-2
 TypeB-ProductAAA-7a
 TypeB-ProductAAA-8
 TypeB-ProductAAA-13
 TypeB-ProductAAA-19

How can I sort the above so that the results are sorted by
the 2nd and 3rd fields.

The 3rd field may be 1 to 3 number digits (sometimes
followed bywith a letter).

That is, I want something like this.
 ...
 TypeB-ProductAAA-1
 TypeB-ProductAAA-2
 TypeB-ProductAAA-7a
 TypeB-ProductAAA-8
 TypeA-ProductAAA-10
 TypeA-ProductAAA-12
 TypeB-ProductAAA-13
 TypeA-ProductAAA-14
 TypeA-ProductAAA-15
 TypeA-ProductAAA-17
 TypeB-ProductAAA-19
 ...

TIA

 
 
 

sort by 2nd & 3rd fields

Post by Harr » Sat, 11 Jun 2005 03:11:17


How can I do a sorting of the following by 2nd & 3rd fields.
The format of the 3rd field is 1 to 3 numeric digits, optionally
followed by a letter.

Input:
 TYPE2-Apple-10
 TYPE2-Apple-12
 TYPE2-Apple-14
 TYPE2-Apple-15
 TYPE2-Apple-17
 TYPE1-Orange-29
 TYPE1-Orange-35
 TYPE1-Orange-38
 TYPE1-Orange-45
 TYPE1-Apple-1
 TYPE1-Apple-2
 TYPE1-Apple-7a
 TYPE1-Apple-8
 TYPE1-Apple-13
 TYPE1-Apple-19

Output:
 TYPE1-Apple-1
 TYPE1-Apple-2
 TYPE1-Apple-7a
 TYPE1-Apple-8
 TYPE2-Apple-10
 TYPE2-Apple-12
 TYPE1-Apple-13
 TYPE2-Apple-14
 TYPE2-Apple-15
 TYPE2-Apple-17
 TYPE1-Apple-19
 TYPE1-Orange-29
 TYPE1-Orange-35
 TYPE1-Orange-38b
 TYPE1-Orange-45

TIA

 
 
 

sort by 2nd & 3rd fields

Post by Bill Marcu » Sun, 12 Jun 2005 15:35:35


On Thu, 09 Jun 2005 18:11:17 GMT, Harry

> How can I do a sorting of the following by 2nd & 3rd fields.
> The format of the 3rd field is 1 to 3 numeric digits, optionally
> followed by a letter.

man sort

--
Depend on the rabbit's foot if you will, but remember, it didn't help
the rabbit.
                -- R.E. Shay

 
 
 

sort by 2nd & 3rd fields

Post by * Tong » Thu, 16 Jun 2005 03:50:38



> I have a text file, after parsing by a few cmd's, yeild something
> like this below.

>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  ... [snip]
>  TypeB-ProductC-176a
>  ... [snip]
>  TypeB-BBB-29
>  TypeB-BBB-35
>  TypeB-BBB-38
>  TypeB-BBB-45
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeB-ProductAAA-13
>  TypeB-ProductAAA-19

> How can I sort the above so that the results are sorted by
> the 2nd and 3rd fields.

> The 3rd field may be 1 to 3 number digits (sometimes
> followed bywith a letter).

> That is, I want something like this.
>  ...
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeB-ProductAAA-13
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  TypeB-ProductAAA-19
>  ...

Hmm, should be

sort -t- -k2 -k3n

But somehow it doesn't work -- the 3rd field is not sorted as desired.
The following test method reveals that sort thinks the the 2nd field
extends over the field separator ("-") and lasting to the 3rd "-nnn"
field... strange, I don't why:

sort -t- -k2r -k3n

Maybe somebody else can help.

$ sort --version
sort (coreutils) 5.2.1

--
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection

 
 
 

sort by 2nd & 3rd fields

Post by * Tong » Thu, 16 Jun 2005 03:48:25



> I have a text file, after parsing by a few cmd's, yeild something
> like this below.

>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  ... [snip]
>  TypeB-ProductC-176a
>  ... [snip]
>  TypeB-BBB-29
>  TypeB-BBB-35
>  TypeB-BBB-38
>  TypeB-BBB-45
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeB-ProductAAA-13
>  TypeB-ProductAAA-19

> How can I sort the above so that the results are sorted by
> the 2nd and 3rd fields.

> The 3rd field may be 1 to 3 number digits (sometimes
> followed bywith a letter).

> That is, I want something like this.
>  ...
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeB-ProductAAA-13
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  TypeB-ProductAAA-19
>  ...

Hmm, should be

sort -t- -k2 -k3n

But somehow it doesn't work -- the 3rd field is not sorted as desired.
The following test method reveals that sort thinks the the 2nd field
extends over the field separator ("-") and lasting to the 3rd "-nnn"
field... strange, I don't why:

sort -t- -k2r -k3n

Maybe somebody else can help.

$ sort --version
sort (coreutils) 5.2.1

--
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection

 
 
 

sort by 2nd & 3rd fields

Post by * Tong » Thu, 16 Jun 2005 03:49:31



> I have a text file, after parsing by a few cmd's, yeild something
> like this below.

>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  ... [snip]
>  TypeB-ProductC-176a
>  ... [snip]
>  TypeB-BBB-29
>  TypeB-BBB-35
>  TypeB-BBB-38
>  TypeB-BBB-45
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeB-ProductAAA-13
>  TypeB-ProductAAA-19

> How can I sort the above so that the results are sorted by
> the 2nd and 3rd fields.

> The 3rd field may be 1 to 3 number digits (sometimes
> followed bywith a letter).

> That is, I want something like this.
>  ...
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeB-ProductAAA-13
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  TypeB-ProductAAA-19
>  ...

Hmm, should be

sort -t- -k2 -k3n

But somehow it doesn't work -- the 3rd field is not sorted as desired.
The following test method reveals that sort thinks the the 2nd field
extends over the field separator ("-") and lasting to the 3rd "-nnn"
field... strange, I don't why:

sort -t- -k2r -k3n

Maybe somebody else can help.

$ sort --version
sort (coreutils) 5.2.1

--
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection

 
 
 

sort by 2nd & 3rd fields

Post by * Tong » Thu, 16 Jun 2005 03:47:19



> I have a text file, after parsing by a few cmd's, yeild something
> like this below.

>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  ... [snip]
>  TypeB-ProductC-176a
>  ... [snip]
>  TypeB-BBB-29
>  TypeB-BBB-35
>  TypeB-BBB-38
>  TypeB-BBB-45
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeB-ProductAAA-13
>  TypeB-ProductAAA-19

> How can I sort the above so that the results are sorted by
> the 2nd and 3rd fields.

> The 3rd field may be 1 to 3 number digits (sometimes
> followed bywith a letter).

> That is, I want something like this.
>  ...
>  TypeB-ProductAAA-1
>  TypeB-ProductAAA-2
>  TypeB-ProductAAA-7a
>  TypeB-ProductAAA-8
>  TypeA-ProductAAA-10
>  TypeA-ProductAAA-12
>  TypeB-ProductAAA-13
>  TypeA-ProductAAA-14
>  TypeA-ProductAAA-15
>  TypeA-ProductAAA-17
>  TypeB-ProductAAA-19
>  ...

Hmm, should be

sort -t- -k2 -k3n

But somehow it doesn't work -- the 3rd field is not sorted as desired.
The following test method reveals that sort thinks the the 2nd field
extends over the field separator ("-") and lasting to the 3rd "-nnn"
field... strange, I don't why:

sort -t- -k2r -k3n

Maybe somebody else can help.

$ sort --version
sort (coreutils) 5.2.1

--
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection

 
 
 

sort by 2nd & 3rd fields

Post by John » Thu, 16 Jun 2005 04:29:01




> > I have a text file, after parsing by a few cmd's, yeild something
> > like this below.

> >  TypeA-ProductAAA-10
> >  TypeA-ProductAAA-12
> >  TypeA-ProductAAA-14
> >  TypeA-ProductAAA-15
> >  TypeA-ProductAAA-17
> >  ... [snip]
> >  TypeB-ProductC-176a
> >  ... [snip]
> >  TypeB-BBB-29
> >  TypeB-BBB-35
> >  TypeB-BBB-38
> >  TypeB-BBB-45
> >  TypeB-ProductAAA-1
> >  TypeB-ProductAAA-2
> >  TypeB-ProductAAA-7a
> >  TypeB-ProductAAA-8
> >  TypeB-ProductAAA-13
> >  TypeB-ProductAAA-19

> > How can I sort the above so that the results are sorted by
> > the 2nd and 3rd fields.

> > The 3rd field may be 1 to 3 number digits (sometimes
> > followed bywith a letter).

> > That is, I want something like this.
> >  ...
> >  TypeB-ProductAAA-1
> >  TypeB-ProductAAA-2
> >  TypeB-ProductAAA-7a
> >  TypeB-ProductAAA-8
> >  TypeA-ProductAAA-10
> >  TypeA-ProductAAA-12
> >  TypeB-ProductAAA-13
> >  TypeA-ProductAAA-14
> >  TypeA-ProductAAA-15
> >  TypeA-ProductAAA-17
> >  TypeB-ProductAAA-19
> >  ...

> Hmm, should be

> sort -t- -k2 -k3n

> But somehow it doesn't work -- the 3rd field is not sorted as desired.
> The following test method reveals that sort thinks the the 2nd field
> extends over the field separator ("-") and lasting to the 3rd "-nnn"
> field... strange, I don't why:

> sort -t- -k2r -k3n

Yes, that is how it works. You need to tell it when each
sort key stops as well as starts.
sort -t- -k2,2r -k3,3n

--
John.

 
 
 

1. sort by 2nd and 3rd fields

(this is the 3rd attempt I've tried to post this;
 for some unknown reason, I can't see my previous 2 postings
 showing up.)

I have a text file with some lines like these below.
I want to sort these lines using the 2nd and 3rd field
(i.e. the hyphen is the field separator). Also, the 3rd
field should be sorted so that, say, 37a is in front of 120.

 PBF-PQR50-17
 PBF-PQR50-170a
 PBF-PQR50-171b
 PBF-PQR50-18
 PBF-PQR50-19
 PBF-PQR50-33
 PBF-PQR50-36
 PBF-PQR50-37a
 PBF-PQR50-39
 PBF-PQR50-40
 PBF-PQR50-42
 PBF-JRYPS10-09
 PBF-JRYPS10-10
 PBF-JRYPS10-11
 PBF-JRYPS10-12
 PBF-JRYPS10-14
 PBF-JRYPS10-15
 PBF-JRYPS10-17
 KEH-PQR50-02
 KEH-PQR50-03
 KEH-PQR50-05
 KEH-PQR50-07b
 KEH-PQR50-08
 KEH-PQR50-102
 KEH-PQR50-103
 KEH-PQR50-110
 KEH-PQR50-115
 KEH-PQR50-116
 KEH-PQR50-11b
 KEH-PQR50-12
 KEH-PQR50-120
 KEH-PQR50-121
 KEH-PQR50-122
 KEH-PQR50-123
 KEH-PQR50-124
 KEH-PQR50-128a
 KEH-PQR50-13
 KEH-JRYPS10-01
 KEH-JRYPS10-02
 KEH-JRYPS10-07a
 KEH-JRYPS10-08
 KEH-JRYPS10-13

TIA

2. Getting CDROM/audio drivers working?

3. bash - sort command - can I sort by a certain field?

4. difficulty using mtio.h

5. sort sort: 0653-657 A write error occurred while sorting (4.1.3)

6. Multicast - Boot Problem?

7. gnu sort, field selection bug?

8. ACL's

9. Sorting file by last field

10. Sorting ps on the TIME field

11. Multiple Field Sorts in UNIX(tm)

12. sort on multiple fields

13. Sorting fields in a single line of text?