2011-02-18 48 views
24

這應該是非常容易,但我不能讓它工作。我只是想用sed從一個字符串替換到一行的結尾。例如,如果我有以下數據文件:從比賽替換到隊尾

one two three five 
    four two five five six 
    six one two seven four 

,我想通過與「嗒嗒」字與輸出結束了該行的結尾從字「二」取代:

one BLAH 
    four BLAH 
    six one BLAH 

那不是僅僅是:

sed -e 's/two,$/BLAH/g' 

我不是在正則表達式的最好的,也許這就是問題所在

回答

44

這應該做你想要什麼:

sed 's/two.*/BLAH/'

$ echo " one two three five 
> four two five five six 
> six one two seven four" | sed 's/two.*/BLAH/' 
    one BLAH 
    four BLAH 
    six one BLAH 

$是不必要的,因爲.*將在該行的結束反正完成,並末尾的g是不必要的,因爲您的第一場比賽將是第一個two到線的末尾。

11

使用此兩<anything any number of times><end of line>

's/two.*$/BLAH/g' 
3

AWK

awk '{gsub(/two.*/,"")}1' file 

紅寶石

ruby -ne 'print $_.gsub(/two.*/,"")' file