Alphabetical Sort

Alphabetical Sort

Post by Derek Abbo » Tue, 19 Sep 1995 04:00:00



I have a straight ascii file. Each entry is separated by a blank line.
Each entry consists of more than one line.

I need some code that will perform an alphabetical sort of the entries.
Any type of code will do, but some kind of awk or sed script would be
convenient.

Any ftp site suggestions?

[The application is sorting a bibliography for input into latex, without having
to use bibtex -- has anyone else experienced this problem?]

 
 
 

Alphabetical Sort

Post by Soren Dayt » Tue, 19 Sep 1995 04:00:00



Quote:> I have a straight ascii file. Each entry is separated by a blank line.
> Each entry consists of more than one line.

> I need some code that will perform an alphabetical sort of the entries.
> Any type of code will do, but some kind of awk or sed script would be
> convenient.

        What about something like:

        sed '/^$/d' | sort | awk '{ print $0 ; print "" }'

Soren

 
 
 

Alphabetical Sort

Post by hal.. » Tue, 19 Sep 1995 04:00:00



Quote:> I have a straight ascii file. Each entry is separated by a blank line.
> Each entry consists of more than one line.

> I need some code that will perform an alphabetical sort of the entries.
> Any type of code will do, but some kind of awk or sed script would be
> convenient.

An idea:

File M.sed:
        /^$/{
                N
                s/\n/ /
        }

File B.sed:
        s/ /\
        \
        /

sed -f M.sed | tr '\012\014' '\013\012' | sort |
tr '\013\012' '\012\014' | sed -f B.sed

If your file has no HT in it, uze that instead of VT (\013).

I suspect that there will be useless emptie lines after all this.

 
 
 

Alphabetical Sort

Post by Eddie Cor » Fri, 22 Sep 1995 04:00:00



>I have a straight ascii file. Each entry is separated by a blank line.
>Each entry consists of more than one line.
>I need some code that will perform an alphabetical sort of the entries.
>Any type of code will do, but some kind of awk or sed script would be
>convenient.
>Any ftp site suggestions?
>[The application is sorting a bibliography for input into latex, without having
>to use bibtex -- has anyone else experienced this problem?]

If I understand what you're saying correctly - I suggest the easiest method is
to combine all the multiline entries into one line, seperating them with say
" ~ " or some other unique character surrounded by a space.  At the same time
removing the blank lines.  Sort as usual then convert " ~ " back to newline.
The first part should be a trivial awk script.

Eddie

 
 
 

Alphabetical Sort

Post by J. Kan » Sat, 23 Sep 1995 04:00:00



|> >I have a straight ascii file. Each entry is separated by a blank line.
|> >Each entry consists of more than one line.

|> >I need some code that will perform an alphabetical sort of the entries.
|> >Any type of code will do, but some kind of awk or sed script would be
|> >convenient.

|> >Any ftp site suggestions?

|> >[The application is sorting a bibliography for input into latex, without having
|> >to use bibtex -- has anyone else experienced this problem?]

|> If I understand what you're saying correctly - I suggest the easiest method is
|> to combine all the multiline entries into one line, seperating them with say
|> " ~ " or some other unique character surrounded by a space.  At the same time
|> removing the blank lines.  Sort as usual then convert " ~ " back to newline.
|> The first part should be a trivial awk script.

It is.  I'm surprised not to see others suggesting this, as it is a
classical example of a small AWK feature (see page 84 in "The AWK
Programming Language"):

        #! /bin/sh

        awk '
                BEGIN { RS = "" ; FS = "\n" }
                      {
                                  printf( "%s" , $1 )
                              for ( i = 2 ; i <= NF ; i ++ )
                                      printf( "$%s" , $i )
                                  printf "\n"
                          }
                ' |
        sort  |
        awk '
                BEGIN { FS = "$" }
                          {
                                  for ( i = 1 ; i <= NF ; i ++ )
                                          print $i
                                  print
                          } '

This sorts on the first field, but it is easy to add any particular
field you like to the start of the line in the first pass through AWK,
then simply not output it in the second.  (This is what actually happens
in the example in the book.)
--

GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
                             --Beratung in industrieller Datenverarbeitung

 
 
 

1. Alphabetical Sort


==>I have a straight ascii file. Each entry is separated by a blank line.
==>Each entry consists of more than one line.
==>
==>I need some code that will perform an alphabetical sort of the entries.
==>Any type of code will do, but some kind of awk or sed script would be
==>convenient.
==>
==>Any ftp site suggestions?
==>
==>[The application is sorting a bibliography for input into latex, without having
==>to use bibtex -- has anyone else experienced this problem?]

Here's a solution in Perl:
-----------------------------------------------------------------
#! /usr/bin/perl

open(FILE,"$ARGV[0]")||die "Couldn't open $ARGV[0]: $!\n";
$FILE=$ARGV[0];

$/="\n\n";    # Set the input record separator to be two
                # consecutive carriage returns. This treats
                # entire paragraphs as single records.


-----------------------------------------------------------------

Warning! This may be a real memory hog, depending on how big the
input file is.

---
Mark Bergman    Biker, IATSE #1 Stagehand, Unix mechanic

I want a newsgroup with a infinite S/N ratio! Now taking CFV on:

rec.motorcycles.stagehands.pet-bird-owners.pinballers.ex-unix-supporters
        5 So Far

2. Any one used vinum?

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

4. mounting CD and error message

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

6. Problem Getting My Sound Card to Work....

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

8. Downloading of Linux thru an FTP Client

9. Sorting without "sort" ?

10. default secondary sort line in 'sort'

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

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

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