移除字串的標點符號 - Linux

Table of Contents

※ 引述《dyoll (lloyd huang)》之銘言:
: ※ 引述《antontw (三十個扮上進青年之一)》之銘言:
: : 設計內容:
: : $ cat file.txt
: : 2000,"1,234,567","5,678",3000,10000,"1,300"
: : 1000,"1,121,234,567","4,321",3000,10000,"2,600"
: : 執行結果:
: : $ echo $(sed -e 's/$/#/' -e 's/"\([^"]*\)"/\n"\1"\n/g' file.txt | \
: : sed -e '/^"/s/[",]//g') | sed -e 's/ //g' -e 's/#/\n/g'
: : 2000,1234567,5678,3000,10000,1300
: : 1000,1121234567,4321,3000,10000,2600
: : 唔 ... 犧牲掉一個 # 了 ...
: Hi antontw
: 謝謝你,看了你的解法,讓我有了另外的想法,參考您的作法後改變如下
: $ cat file.txt | sed -e 's/$/END/' -e 's/"\([^"]*\)"/\n"\1"\n/g' | \
: sed -e '/^"/s/,//g' | sed -e :x -e 'N;s/\n//;/END/!tx;{s/END//}'
: 2000,"1234567","5678",3000,10000,"1300"
: 1000,"1121234567","4321",3000,10000,"2600"

我是用loop的方式去除""內的,
sed ':head
s/"\([0-9][0-9]*\),\([0-9][0-9,]*\)"/"\1\2"/g
# substite again if there are comma inside quotes
thead
# else, remove the quotes
s/"//g'
這樣應該會比較簡單一點
有的sed如果不支援註解的,要把#開頭那行拿掉才能執行

--

All Comments

Necoo avatarNecoo2011-04-12
感謝您的協助 :)
Bennie avatarBennie2011-04-17
讚!! 您這個方法邏輯更簡單有效..
Megan avatarMegan2011-04-20
謝謝