提取目录名称并将其放在列表顶部

时间:2013-11-25 08:39:30

标签: unix sed awk ksh

Aug 1 2013 /home/s/tone/TONE/gong1
Aug 1 2013 /home/s/tone/TONE/gong1.x
Aug 1 2013 /home/s/tone/TONE/gong2
Aug 1 2013 /home/s/tone/TONE/gong1.kbd
Aug 1 2013 /home/s/tone/TONE/gong2.x
Aug 1 2013 /home/s/tone/TONE/gong2.kbd
Aug 1 2013 /home/s/tone/TONE/gong3.kbd
Oct 10 2013 /home/s/man/whatisSPEC
Oct 10 2013 /home/s/man/man3/ctx.3
Oct 10 2013 /home/s/man/man3/sos.3
Oct 10 2013 /home/s/man/man3/dt.3
Oct 10 2013 /home/s/man/man3/timexpr.3
Oct 10 2013 /home/s/man/man3/mpusw.3
Oct 10 2013 /home/s/man/man3/mpu.err.3
Oct 10 2013 /home/s/man/man3/dbr.3
Oct 10 2013 /home/s/man/man3/psi.err.3
Oct 10 2013 /home/s/man/man3/stapo.3

大家好,

我想知道是否有任何方法可以将directory name插入到顶部并重新打印列表,使其看起来像这样。非常感谢。

TONE
Aug 1 2013 /home/s/tone/TONE/gong1
Aug 1 2013 /home/s/tone/TONE/gong1.x
Aug 1 2013 /home/s/tone/TONE/gong2
Aug 1 2013 /home/s/tone/TONE/gong1.kbd
Aug 1 2013 /home/s/tone/TONE/gong2.x
Aug 1 2013 /home/s/tone/TONE/gong2.kbd
Aug 1 2013 /home/s/tone/TONE/gong3.kbd
man
Oct 10 2013 /home/s/man/whatisSPEC
man3
Oct 10 2013 /home/s/man/man3/ctx.3
Oct 10 2013 /home/s/man/man3/sos.3
Oct 10 2013 /home/s/man/man3/dt.3
Oct 10 2013 /home/s/man/man3/timexpr.3
Oct 10 2013 /home/s/man/man3/mpusw.3
Oct 10 2013 /home/s/man/man3/mpu.err.3
Oct 10 2013 /home/s/man/man3/dbr.3
Oct 10 2013 /home/s/man/man3/psi.err.3
Oct 10 2013 /home/s/man/man3/stapo.3

2 个答案:

答案 0 :(得分:1)

目前尚不清楚您从何处获取列表,因此我做出与Mari

相同的假设
$ cat sample.txt
> Aug 1 2013 /home/s/tone/TONE/gong1
> Aug 1 2013 /home/s/tone/TONE/gong1.x
> Aug 1 2013 /home/s/tone/TONE/gong2
> Aug 1 2013 /home/s/tone/TONE/gong1.kbd
> Aug 1 2013 /home/s/tone/TONE/gong2.x
> Aug 1 2013 /home/s/tone/TONE/gong2.kbd
> Aug 1 2013 /home/s/tone/TONE/gong3.kbd
> Oct 10 2013 /home/s/man/whatisSPEC
> Oct 10 2013 /home/s/man/man3/ctx.3
> Oct 10 2013 /home/s/man/man3/sos.3
> Oct 10 2013 /home/s/man/man3/dt.3
> Oct 10 2013 /home/s/man/man3/timexpr.3
> Oct 10 2013 /home/s/man/man3/mpusw.3
> Oct 10 2013 /home/s/man/man3/mpu.err.3
> Oct 10 2013 /home/s/man/man3/dbr.3
> Oct 10 2013 /home/s/man/man3/psi.err.3
> Oct 10 2013 /home/s/man/man3/stapo.3

可以解决这个问题:

$ awk -f script.awk sample.txt
> TONE
> Aug 1 2013 /home/s/tone/TONE/gong1
> Aug 1 2013 /home/s/tone/TONE/gong1.x
> Aug 1 2013 /home/s/tone/TONE/gong2
> Aug 1 2013 /home/s/tone/TONE/gong1.kbd
> Aug 1 2013 /home/s/tone/TONE/gong2.x
> Aug 1 2013 /home/s/tone/TONE/gong2.kbd
> Aug 1 2013 /home/s/tone/TONE/gong3.kbd
> man
> Oct 10 2013 /home/s/man/whatisSPEC
> man3
> Oct 10 2013 /home/s/man/man3/ctx.3
> Oct 10 2013 /home/s/man/man3/sos.3
> Oct 10 2013 /home/s/man/man3/dt.3
> Oct 10 2013 /home/s/man/man3/timexpr.3
> Oct 10 2013 /home/s/man/man3/mpusw.3
> Oct 10 2013 /home/s/man/man3/mpu.err.3
> Oct 10 2013 /home/s/man/man3/dbr.3
> Oct 10 2013 /home/s/man/man3/psi.err.3
> Oct 10 2013 /home/s/man/man3/stapo.3

,此示例中使用的script.awk如下所示:

BEGIN {
    FS="/"
}
lastDir!=$(NF-1){
    lastDir=$(NF-1)
    print lastDir
}
{
    print $0
}

一开始我们将字段分隔符FS设置为/,这与使用awk -F "/"调用相同,但为了清楚起见,我将所有内容都放入脚本,而不仅仅是 oneliner。

NF变量为您提供每行的字段数,$(NF-1)因此是每行中的最后一个字段(由/分隔),这正是每个行的名称目录。现在我们比较一下,如果lastDir变量$(NF-1)(当前目录)相同,那么我们会覆盖lastDir变量并打印它。无论如何,我们用$0打印整行。请注意,不需要启动lastDir变量,只需将其设置为空字符串。

答案 1 :(得分:0)

我假设你的输入数据在一个文件中。所以我用你的输入数据创建了一个文件。所以它在我的服务器中就是这样。

cat sample.txt

Aug 1 2013 /home/s/tone/TONE/gong1
Aug 1 2013 /home/s/tone/TONE/gong1.x
Aug 1 2013 /home/s/tone/TONE/gong2
Aug 1 2013 /home/s/tone/TONE/gong1.kbd
Aug 1 2013 /home/s/tone/TONE/gong2.x
Aug 1 2013 /home/s/tone/TONE/gong2.kbd
Aug 1 2013 /home/s/tone/TONE/gong3.kbd
Oct 10 2013 /home/s/man/whatisSPEC
Oct 10 2013 /home/s/man/man3/ctx.3
Oct 10 2013 /home/s/man/man3/sos.3
Oct 10 2013 /home/s/man/man3/dt.3
Oct 10 2013 /home/s/man/man3/timexpr.3
Oct 10 2013 /home/s/man/man3/mpusw.3
Oct 10 2013 /home/s/man/man3/mpu.err.3
Oct 10 2013 /home/s/man/man3/dbr.3
Oct 10 2013 /home/s/man/man3/psi.err.3
Oct 10 2013 /home/s/man/man3/stapo.3

因此,您可以从此命令获取目录名称

awk -F "/" '{print $(NF-1)}' sample.txt | uniq

<强>输出

TONE
man
man3

这里只是帮助您获取目录名称。我不确定如何在每个组线的顶部打印它们。