Awk and sort (of text files)

Bill Oliver vendor at billoblog.com
Mon Jun 29 17:48:10 UTC 2015


On Mon, 29 Jun 2015, jd1008 wrote:

>
>
> On 06/29/2015 03:39 AM, Dario Lesca wrote:
>>  Il giorno dom, 28/06/2015 alle 18.38 -0600, jd1008 ha scritto:
>> >  Hi,
>> >  I have text files made of paragraphs of text, separated by
>> >  blank lines.
>> > 
>> >  Each "paragraph" is information about a different item.
>> > 
>> >  In need to sort these paragraphs based on the first line
>> >  of each paragraph.
>> > 
>> >  Need some hints how to accomplish this.
>> > 
>> >  Thanx.
>>  An example of your text file can help us to help you.
>> 
> I described them perfectly.
> text paragraphs made of a few or several lines.
>
> The paragraphs are separated by an empty line.
>
>

Try something like this.  It's buggy, but what can you expect for 5
minutes of work.

This takes a text with lines separated by hard breaks, and an empty line
between paragraphs, and sorts it.

Here are the obvious problems I haven't bothered to debug:

1) I counts the empty lines as paragraphs, so you get blank space at the
top.

2) I'm doing something wrong with asort (see comment).

3) It looks like I'm sorting twice -- once with asort, and then to
reindex.  There should be a smart way to do this.

Here's the awk code:


BEGIN{newparagraph=0; numlines=0; paranum=0;}

         {
         #if the line is blank, it's time to start a new paragraph
         if ($0==""){
                 paranum++;
                 numlines=0;
                 }
         #if it's not blank, buffer it
         else {
                 numl[paranum]=numlines;
                 paragraph[paranum][numlines++] = $0;
                 }

         }


END{

         for (i=0;i<=paranum;i++){
                 firstline[i] = paragraph[i][0]

                 }

         #for a reason I don't understand, "sorted" has one index more than firstline!?
         #I'm probably making some mistake with starting with 0 vs 1, but I'm not going to fix it.
         # so, I'll just increment paranum, because I'm lazy
         asort(firstline,sorted);
         paranum++



         #Renumber the indices
         for (i=0;i<=paranum;i++){

                 found=0;
                 newindex[i] = 999;
                 for(j=0;((j<=paranum) && (found==0));j++){
                         if(sorted[i] == firstline[j]){
                                 newindex[i]=j;
                                 found=1;
                                 }
                 }

         }

         #print it out
         for(i=0;i<=paranum;i++){

                 current_paragraph = newindex[i];
                 new_numlines = numl[current_paragraph];

                 for (j=0;j<=new_numlines;j++){
                         print (paragraph[newindex[i]][j]);
                         }
                 print("");
                 }

         }



Here's the Gettysburg Address:



Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, 
and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. 
We are met on a great battle-field of that war. 
We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.
It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. 
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. 
The world will little note, nor long remember what we say here, but it can never forget what they did here. 
It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. 
It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased 
devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these 
dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and 
that government of the people, by the people, for the people, shall not perish from the earth.

Abraham Lincoln
November 19, 1863


Here it is sorted:


%awk -f para.awk < test.txt






Abraham Lincoln
November 19, 1863

But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. 
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. 
The world will little note, nor long remember what we say here, but it can never forget what they did here. 
It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. 
It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased 
devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these 
dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and 
that government of the people, by the people, for the people, shall not perish from the earth.

Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, 
and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. 
We are met on a great battle-field of that war. 
We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.
It is altogether fitting and proper that we should do this.



More information about the users mailing list