使用awk按行块的第一行按字段排序文件

时间:2014-07-14 05:17:12

标签: sorting awk

最好用awk但其他脚本可以。 ("{{{1"在线末端用于vim折叠,应该与所需的输出无关。)示例中的数据来自在线聊天室,我将其粘贴到文本文件中。

Begin file contents:
-----------------------------------------------
/mysql    unauth'd user on show processlist, plus db conn error in a site {{{1

1:05 
Gary
can you ck belljar20 instance?

1:06
Justin looks like reverse dns issue

-----------------------------------------------
/mysql    pingtimes to db server solved by adding domain to /etc/hosts on db server {{{1
per internal wiki
...

-----------------------------------------------
/php54    back to php52 with manual fix for https {{{1
Gary 
can u force mkp44aaa.net to bind to an ip address?
...

-----------------------------------------------
:End file contents

记录,又名块(不同行数)以一个单词“/ category”作为第一行的第一个单词开头,在开始正斜杠“/”之后,以一条约40行结束破折号。上面,在3个块示例中,两个类别为“/ mysql”,另一个类别为“php54”。

在上面的示例中,我希望输出已经排序,因此两个“/ mysql”类别块在排序输出中彼此相邻。

因此,基本上,只需按类别名称对块进行排序。

我已经看到了很多解决方案的组件,但似乎找不到一个足以让我适应它的点。

1 个答案:

答案 0 :(得分:1)

如果您可以使用perl

#! /bin/bash

input=/tmp/file

perl -n0le '
    while (s/(\/\w+(.|\n)*?-+)//m){
        $str=$1; $str=~/(\/\w+)/;
        $h{$1}=[] unless exists $h{$1};
        push @{h{$1}},$str;
    }
    END{
        foreach $key (sort keys %h){
            foreach ( @{$h{$key}} ){
                print $_."\n";
            }
        }
    }' $input

<强>解释

有很多事情要发生,首先我们想要一个多行匹配,这就是为什么我们使用-0将输入文件的全部内容放入$_

然后我们想要提取我们的模式"(\/\w+(.|\n)*?-+)"创建一个数组的哈希值,键为“/ category”。最后,我们根据该键和打印进行排序。

<强>输出:

bash test.sh 
/aaa
this is a test

-----------------------------------------------
/mysql    unauth'd user on show processlist, plus db conn error in a site {{{1

1:05 
Gary
can you ck belljar20 instance?

1:06
Justin looks like reverse dns issue

-----------------------------------------------
/mysql    pingtimes to db server solved by adding domain to /etc/hosts on db server {{{1
per internal wiki
...

-----------------------------------------------
/php54    back to php52 with manual fix for https {{{1
Gary 
can u force mkp44aaa.net to bind to an ip address?
...

-----------------------------------------------
相关问题