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

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

Post by Alex Hunsle » Fri, 03 Dec 2004 20:51:05



I'd like to use a bash command to sort a file by a certain field in that
file.
I've looked at sort but I can't see how to do it - interestingly, the
docs say the -t option is for specifying a delimiter, but then I can't
see anything else obvious about saying which field you'd like to sort by!
If sort can't do this directly, I'm sure something is possible using cut
+ sort together, but the exact form eludes me at the moment!

thanks
alex

 
 
 

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

Post by Alex Hunsle » Fri, 03 Dec 2004 21:00:12



> I'd like to use a bash command to sort a file by a certain field in that
> file.
> I've looked at sort but I can't see how to do it - interestingly, the
> docs say the -t option is for specifying a delimiter, but then I can't
> see anything else obvious about saying which field you'd like to sort by!
> If sort can't do this directly, I'm sure something is possible using cut
> + sort together, but the exact form eludes me at the moment!

> thanks
> alex

Well, I thought to try using a cut either side of a sort, to reorder the
fields so that the field I want to sort by was at the front (and then
another cut after the sort to put the order back to normal), but cut
won't allow re-ordering of fields apparently!

  cut  -d , -f 1,2 file.txt

30/11/04,5166857
30/11/04,5166817
30/11/04,5173760

  cut  -d , -f 2,1 file.txt  # note swapped order of fields

30/11/04,5166857
30/11/04,5166817
30/11/04,5173760

.. hmm, same order output each time!

alex

 
 
 

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

Post by Alex Hunsle » Fri, 03 Dec 2004 21:02:21



> I'd like to use a bash command to sort a file by a certain field in that
> file.
> I've looked at sort but I can't see how to do it - interestingly, the
> docs say the -t option is for specifying a delimiter, but then I can't
> see anything else obvious about saying which field you'd like to sort by!
> If sort can't do this directly, I'm sure something is possible using cut
> + sort together, but the exact form eludes me at the moment!

> thanks
> alex

D'OH!
I've just discovered, via random web pages, that I can do:

sort +1 file.txt

... to sort by a certain field. My Cygwin man page says *nothing* about
this ability - why this oversight?

alex

 
 
 

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

Post by John » Fri, 03 Dec 2004 23:38:22



> I'd like to use a bash command to sort a file by a certain field in that
> file.
> I've looked at sort but I can't see how to do it - interestingly, the
> docs say the -t option is for specifying a delimiter, but then I can't
> see anything else obvious about saying which field you'd like to sort by!

You have to know that "key" means the thing to sort by,
and then you can see that you want the -k option to specify
the sort key.

Note that you can specify where the sort key ends as well
as starts, eg sort -k2,2

and that you can specify character positions eg sort -k2.3

and you can have more than one sort key, eg sort -k3,4 -k2,2

and so on and so forth.

--
John.

 
 
 

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

Post by rakesh shar » Sat, 04 Dec 2004 01:43:49



Quote:

> Well, I thought to try using a cut either side of a sort, to reorder the
> fields so that the field I want to sort by was at the front (and then
> another cut after the sort to put the order back to normal), but cut
> won't allow re-ordering of fields apparently!

>   cut  -d , -f 1,2 file.txt

> 30/11/04,5166857
> 30/11/04,5166817
> 30/11/04,5173760

>   cut  -d , -f 2,1 file.txt  # note swapped order of fields

> 30/11/04,5166857
> 30/11/04,5166817
> 30/11/04,5173760

'cut' can't reorder. use 'awk' for that.

sort -k

is for sorting on fields.

 
 
 

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

Post by Michael Tos » Fri, 03 Dec 2004 21:36:02




> > I'd like to use a bash command to sort a file by a certain field in that
> > file.
> > I've looked at sort but I can't see how to do it - interestingly, the
> > docs say the -t option is for specifying a delimiter, but then I can't
> > see anything else obvious about saying which field you'd like to sort by!
> > If sort can't do this directly, I'm sure something is possible using cut
> > + sort together, but the exact form eludes me at the moment!

> > thanks
> > alex

> D'OH!
> I've just discovered, via random web pages, that I can do:

> sort +1 file.txt

> .... to sort by a certain field. My Cygwin man page says *nothing* about
> this ability - why this oversight?

> alex

sort -t , +1 file.txt
is old style.
sort -t , -k 2 file.txt
is new style. At least this should be in your manual pages.

--
Michael Tosch
IT Specialist
Managed Services Germany
Hewlett-Packard GmbH
Phone: +49 2407 575 313
Mail: michael.tosch:hp.com

 
 
 

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

Post by Michael Tos » Fri, 03 Dec 2004 21:40:05




> > I'd like to use a bash command to sort a file by a certain field in that
> > file.
> > I've looked at sort but I can't see how to do it - interestingly, the
> > docs say the -t option is for specifying a delimiter, but then I can't
> > see anything else obvious about saying which field you'd like to sort by!
> > If sort can't do this directly, I'm sure something is possible using cut
> > + sort together, but the exact form eludes me at the moment!

> > thanks
> > alex

> Well, I thought to try using a cut either side of a sort, to reorder the
> fields so that the field I want to sort by was at the front (and then
> another cut after the sort to put the order back to normal), but cut
> won't allow re-ordering of fields apparently!

>   cut  -d , -f 1,2 file.txt

> 30/11/04,5166857
> 30/11/04,5166817
> 30/11/04,5173760

>   cut  -d , -f 2,1 file.txt  # note swapped order of fields

> 30/11/04,5166857
> 30/11/04,5166817
> 30/11/04,5173760

> ... hmm, same order output each time!

cut can only cut.
You would need sed or awk:

awk -F, '{print $2","$1}' file.txt

--
Michael Tosch
IT Specialist
Managed Services Germany
Hewlett-Packard GmbH
Phone: +49 2407 575 313
Mail: michael.tosch:hp.com

 
 
 

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

Post by Greg Beek » Sat, 04 Dec 2004 02:45:51




> > I'd like to use a bash command to sort a file by a certain field in that
> > file.
> > I've looked at sort but I can't see how to do it - interestingly, the
> > docs say the -t option is for specifying a delimiter, but then I can't
> > see anything else obvious about saying which field you'd like to sort by!
> > If sort can't do this directly, I'm sure something is possible using cut
> > + sort together, but the exact form eludes me at the moment!

> > thanks
> > alex

> Well, I thought to try using a cut either side of a sort, to reorder the
> fields so that the field I want to sort by was at the front (and then
> another cut after the sort to put the order back to normal), but cut
> won't allow re-ordering of fields apparently!

>   cut  -d , -f 1,2 file.txt

> 30/11/04,5166857
> 30/11/04,5166817
> 30/11/04,5173760

>   cut  -d , -f 2,1 file.txt  # note swapped order of fields

> 30/11/04,5166857
> 30/11/04,5166817
> 30/11/04,5173760

> .. hmm, same order output each time!

> alex

To sort on the second field (first field is 0) Use this:
sort -n -t, +1 file.txt
-n sorts in number order, rather than alpha

Quote:>sort -n -t, +1 file.txt

30/11/04,5166817
30/11/04,5166857
30/11/04,5173760
 
 
 

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

On a 4.1.3 system, we intermittently are seeing:

sort: 0653-657 A write error occurred while sorting

There is nothing in errpt, and the destination file system of
the sort is certainaly not full.

Does anyone have any suggestions as to what this could mean?

Thanks.

p.s. the actual sort operation is:

sort -d -f -u +0 -1 +1 -2 funds.load | awk '
  { printf "%s %s %s\n", $1, $2, $3 }' >funds.sorted

 *or it might be* :

sort -d -f -u +0 -1 acc_codes.update >acc_codes.sorted

(this is not my code, and I'm not familiar with its design).

Thanks,

--

2. RH8: Internet packets refused, why?

3. SORT command > sorting decimal numbers in columns on a file not working !

4. shutdown script

5. Sorting by second field. Quicker command?

6. Installing SuSE Linux Issues

7. sort: write error while sorting: No space left on device??

8. strange delay while dns resolve

9. "sort" sorting uppercase/lowercase differently on different boxes

10. Sorting without "sort" ?

11. default secondary sort line in 'sort'

12. (sort | uniq) vs (sort -u)

13. How to have sort start sorting froma given column?