我試圖編寫一個使用perl來查找和替換字符的bash函數。我已經寫了下面的功能:用引號寫一個bash函數
find_replace() {
perl -p -i -e "s/$1/$2/g" "$3"
}
這是不對的,現在的工作,我想是因爲$ 1和$ 2正在包圍他們(引號逃脫其據我所知,有一部分需要在那裏的perl語法)。
有關如何使此功能工作(或更好的方式來寫它,以避免這個問題)的任何提示?
編輯:
繼Barmar的建議,這裏是輸出,當我試圖運行功能:
dholtz$ find_replace \001 , revenue_by_offer_tid
+ find_replace 001 , revenue_by_offer_tid
+ perl -p -i -e ''\''s/001/,/g'\''' revenue_by_offer_tid
++ update_terminal_cwd
++ local 'SEARCH= '
++ local REPLACE=%20
++ local PWD_URL=file://Dave-Mac-2.local/Users/dholtz
++ printf '\e]7;%s\a' file://Dave-Mac-2.local/Users/dholtz
dholtz$ head revenue_by_offer_tid
+ head revenue_by_offer_tid
Friday00228686050.0
Friday00228690410.0
Friday017438366585.040000000000004
Friday017438366591.3200000000000003
Friday017438366600.12
Friday0174383666114.759999999999962
Friday017438371407.440000000000006
Friday0174383815118.599999999999977
Friday017438382221.5600000000000005
Friday017438383663.480000000000002
預期成果是:
Friday,0,0,22,86860,50.0
Friday,0,0,22,86904,10.0
Friday,0,1,7438,36658,5.040000000000004
Friday,0,1,7438,36659,1.3200000000000003
Friday,0,1,7438,36660,0.12
Friday,0,1,7438,36661,14.759999999999962
Friday,0,1,7438,37140,7.440000000000006
Friday,0,1,7438,38151,18.599999999999977
Friday,0,1,7438,38222,1.5600000000000005
Friday,0,1,7438,38366,3.480000000000002
您不需要在函數中轉義引號。 – Barmar 2013-03-01 02:20:35
我沒有看到你期望如何通過你所做的替代來獲得預期的產出。如何用''替換'001',''Friday002'換成'Friday,0,0,2'? – Barmar 2013-03-01 03:05:50
Barmar - 我猜這個文件裏有SOH字符,當它顯示在終端上時就不會顯示出來。 – 2013-03-01 04:03:18