2017-06-04 62 views
0

我有兩列的文件列表。我需要刪除第一列並保留一個文件名列表。如果我碰巧有一個以空格結尾的文件,例如「某個文件」,則該空間將被awk刪除。製作AWK保持末尾的那個空白

與文件 「輸入」(注意在 「一些文件」 的端部的空間)

abc some file 
def some other file 

運行

cat input | awk '{$1=""; print substr($0, 2)}' > output 

會產生文件輸出

some file 
some other file 

實施例其中「某些文件」現在是「某個文件」,導致處理文件列表時不存在文件。

任何便攜式溶液理解:)

[編輯]嘗試簡化的上方,以便更清楚的例子,但實際上有更多的列,以便一些解決方案可能不適用。

實際文件是一個rsync的--list-僅輸出:

drwxr-xr-x  4096 2017/06/04 11:24:21 . 
drwxr-xr-x  234234 2017/06/04 11:24:19 some file 
drwxr-xr-x  1341212 2017/06/04 11:24:19 some other file 

示出的filesizes可能膨脹,從而除去後的字符將導致錯誤的固定量的列。

確實的文件名可以包含路徑和多個空格。

實施例測試文件(記住,文件大小可以變化,所以第二列可能在尺寸上增加):

drwxr-xr-x  4096 2017/06/04 11:24:21 . 
drwxr-xr-x  4096 2017/06/04 11:24:19 another 
drwxr-xr-x  4096 2017/06/04 11:24:19 another/one 
drwxr-xr-x  4096 2017/06/04 11:24:19 another/one/bites 
drwxr-xr-x  4096 2017/06/04 11:24:19 another/one/bites/ de_dust 
-rw-r--r--   0 2017/06/04 11:24:19 another/one/bites/ de_dust/ 2017/06/04 11:24:19 Iron Rhapsody 
drwxr-xr-x  4096 2017/06/04 11:24:19 phantom of 
drwxr-xr-x  4096 2017/06/04 11:24:19 phantom of /the opera 
-rw-r--r--   0 2017/06/04 11:24:19 phantom of /the opera/Bohemian Maiden 

[/編輯]

回答

2
$ awk '{sub(/[^/]+\/.{15}/,"")}1' file 
. 
another 
another/one 
another/one/bites 
another/one/bites/ de_dust 
another/one/bites/ de_dust/ 2017/06/04 11:24:19 Iron Rhapsody 
phantom of 
phantom of /the opera 
phantom of /the opera/Bohemian Maiden 

或GNU或OSX sed的用於-E(具有嚴格POSIX SEDS你會逃避+,{和}):

$ sed -E 's:[^/]+/.{15}::' file 
. 
another 
another/one 
another/one/bites 
another/one/bites/ de_dust 
another/one/bites/ de_dust/ 2017/06/04 11:24:19 Iron Rhapsody 
phantom of 
phantom of /the opera 
phantom of /the opera/Bohemian Maiden 
+0

由於不同的文件大小,文件名之前並不總是44個字符。文件名可能包含路徑,所以「/」可以出現在文件名中。試圖閱讀你的正則表達式,請謹慎解釋最後一個?仍然需要適應,以適應我的請求數:) – deajan

+1

謝謝,做編輯:) 在正則表達式不好,有沒有辦法說'''從行的開始刪除後先HTE 17個字符/上線'''也許?使用通配符總是會抓住最後一個,我不知道如何獲得第一個。謝謝。 – deajan

+0

改變你的答案太快了,我只是有時間來提高你的最後一個這個'''sed的-E「s/^。{10} + [0-9] + [0-9 /] {10} [0 -9:] {8} //''''這應該是一些多少防彈我猜。 – deajan

1

我與GNU建議的sed:

sed -r 's/^.* [0-9/]{10} [0-9:]{8} //' input 

輸出:

 
. 
some file 
some other file 
+0

看起來不錯,你能解釋中的正則表達式{10}和{8}嗎? – deajan

+0

精確匹配10(連續的)的0,1,2,3,4,5,6,7,8,9和/字符。 – Cyrus

+0

謝謝,剛剛在Linux和BSD上測試過,似乎工作得很好:) – deajan

0

trcut A液:

tr -s ' ' <inputfile | cut -d' ' -f5- 
+0

只有沒有超過一個連續空格的文件名纔可以工作 – deajan

+0

@deajan它也會剝離一個前導空間,沒有在你的要求中說明。 – fd0