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
答案 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可以解决这个问题:
$ 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 "/"
调用awk相同,但为了清楚起见,我将所有内容都放入脚本,而不仅仅是awk 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
这里只是帮助您获取目录名称。我不确定如何在每个组线的顶部打印它们。