[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Indexing Unix command



Christian Seiler <christian@iwakd.de> writes:

> On 08/01/2016 02:52 PM, Rodolfo Medina wrote:
>> with the following simple script:
>> 
>> #!/bin/sh
>> 
>> cat index.idx | sort > index.ind
>> 
>> I sort the contents of a file and write it in another file.  Now, I want
>> that a small vertical space, i.e. an empty line or two, were inserted before
>> all the words that start with a new letter.
>
> If the first letters of your words are only ASCII characters
> (or you are using an 8 bit character encoding), then the
> following should do the trick:
>
> sort < index.idx | awk 'BEGIN {
>           c = -1;
>         }
>         {
>           if (substr($0, 1, 1) != c && c != -1)
>             printf("\n\n");
>           c = substr($0, 1, 1);
>           print;
>         }' > index.ind
>
> See also:
> https://en.wikibooks.org/wiki/AWK


Thanks indeed, it works fine.

Rodolfo


Reply to: