2012-07-11 81 views
2

我的工作有數百行,看起來像下面的濃縮版這個文本文件:SED插入字符匹配

# <block_uid> # <freq> <memop> <fpop> <insn> <line> <fname> <perc> 
3941319690354688 # 776547959 2.232946 0 
3941427129090048 # 465293088 40 37 74 4.215079 0 
3941427110739968 # 465558912 36 35 68 

我試圖用sed插入dfpattern在每行上的3941319690354688# 776547959之間,因爲該文件大到需要手動完成。一個16位數字圖案之後並前# 即它 所以看起來就像這樣:

# <block_uid> # <freq> <memop> <fpop> <insn> <line> <fname> <perc> 
3941319690354688 dfpattern # 776547959 2.232946 0 
3941427129090048 dfpattern # 465293088 40 37 74 4.215079 0 
3941427110739968 dfpattern # 465558912 36 35 68 

我在SED一個新手,我不能拿出命令擺弄周圍,所以我問求助。 你可以請你解釋一下你簡要發佈的命令,以便我可以從中學習。 謝謝!

回答

1

請嘗試以下命令。它從行的開頭匹配(^),任意數字後面跟着一個空格,其內容相同(\1),文字dfpattern被附加。

sed -e 's/^\([0-9]*[ ]\)/\1dfpattern /' infile 

輸出:

# <block_uid> # <freq> <memop> <fpop> <insn> <line> <fname> <perc> 
3941319690354688 dfpattern # 776547959 2.232946 0 
3941427129090048 dfpattern # 465293088 40 37 74 4.215079 0 
3941427110739968 dfpattern # 465558912 36 35 68 
+0

豈不是足夠用,以取代第一空間'dfpattern' ? 's// dfpattern /' – tripleee 2012-07-11 20:50:26

+0

@tripleee:是的。它也可以工作,但考慮到跳過標題,比如'sed -e'2,$ s// dfpattern /'infile' – Birei 2012-07-11 20:53:29

+0

由於某些原因,它不適用於我:它給了我同樣的輸出。但是sed -e's/\([0-9] * [] \)/ \ 1dfpattern/g'testing.txt - 改變了輸出,但是在錯誤的地方,因爲我沒有指定^前面的行字符。 – 2012-07-11 21:16:38