2016-06-01 12 views
0

所以,我試圖刪除表格中的數字後,我從命令行格式化的選項卡。以下是原始表中的數據,有關從文件coipied和直接粘貼:奇怪的行爲,使用`sed`

File Path     Line Description 
/home/nick/.bashrc    9   # TODO  Chop this into code import files 
/home/nick/.bashrc   204   # TODO  Add $HOME/os-setup to OS installation disc 
/home/nick/.bashrc   207   # TODO  Custom power actions don't work; system tray notifications 

當添加一個最終sed命令,但是管,出現一些奇怪的行爲。作爲一個例子,考慮下面的sed命令:

cat somefile.txt | column -tx -s : | sed -e 's/\([0-9]\{1,\}\)/\1/g' 
File Path     Line Description 
/home/nick/.bashrc    9   # TODO  Chop this into code import files 
/home/nick/.bashrc   204   # TODO  Add $HOME/os-setup to OS installation disc 
/home/nick/.bashrc   207   # TODO  Custom power actions don't work; system tray notifications 

此查找表中的每一行中的數字,然後替換匹配正則表達式的第一部分。由於整個比賽用大括號包裹,這意味着沒有任何變化,因爲它被替換爲自身。

然而,當我再試試同一sed命令,但我添加了\t字符,文字標籤,以匹配正則表達式的sed輸出似乎也截斷號碼匹配!見下:

cat somefile.txt | column -tx -s : | sed -e 's/\([0-9]\{1,\}\)\t/\1/g' 
File Path     Line Description 
/home/nick/.bashrc    # TODO  Chop this into code import files 
/home/nick/.bashrc   20 # TODO  Add $HOME/os-setup to OS installation disc 
/home/nick/.bashrc   20 # TODO  Custom power actions don't work; system tray notifications 

爲什麼sed截斷每個數字的最後一位數字?如何停止sed這樣做?

+0

可以請你用\ t顯示sed代碼嗎? –

+0

有趣,不能重現 –

+0

@NickBull:這個文件從哪裏來的? – sjsam

回答

1

而不是在數字後刪除標籤我刪除# TODO之前的空格。

AWK(GNU)溶液

awk '{print gensub(/[ ]+( # TODO)/,"\\1","g",$0)} ' file 

SED溶液

sed -E 's/[ ]+# TODO/ # TODO/' file 

輸出

File Path     Line Description 
/home/nick/.bashrc    9 # TODO  Chop this into code import files 
/home/nick/.bashrc   204 # TODO  Add $HOME/os-setup to OS installation disc 
/home/nick/.bashrc   207 # TODO  Custom power actions don't work; system tray notification 

假設

的描述總是以# TODO

注意

開始你可以把在選擇# TODO之前的空間所需的號碼。我把兩個。