2013-11-25 24 views
0
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 
+1

你是如何獲得第一名單? – anubhava

回答

0

我假設你的輸入數據在一個文件中。所以我用你的輸入數據創建了一個文件。所以它在我的服務器中就像這樣。

貓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 

我在這裏幫助您獲取目錄只有名字。我不確定如何將它們打印在每組線的頂部。

+0

非常感謝你們!感謝所有的投入:) – user3030566

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變量不需要啓動,它只是簡單地設置爲空字符串。

+0

好的解決方案@pfnuesel .. – Mari

+1

縮短一些:'awk -F /'f!= $(NF-1){f = $(NF-1); print f} 1'file' – Jotne

相關問題