移除字串的標點符號 - Linux

By Quintina
at 2011-04-10T14:18
at 2011-04-10T14:18
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如果不支援註解的,要把#開頭那行拿掉才能執行
--
: ※ 引述《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如果不支援註解的,要把#開頭那行拿掉才能執行
--
Tags:
Linux
All Comments

By Necoo
at 2011-04-12T12:35
at 2011-04-12T12:35

By Bennie
at 2011-04-17T05:56
at 2011-04-17T05:56

By Megan
at 2011-04-20T10:21
at 2011-04-20T10:21
Related Posts
移除字串的標點符號

By Steve
at 2011-04-10T09:55
at 2011-04-10T09:55
FreeRadius一定得搭配LDAP嗎

By Dorothy
at 2011-04-09T21:02
at 2011-04-09T21:02
sed用法 插入

By Frederic
at 2011-04-09T14:44
at 2011-04-09T14:44
想學習虛擬化技術

By Regina
at 2011-04-09T13:44
at 2011-04-09T13:44
VMWare Player做一個NAT

By Franklin
at 2011-04-09T13:00
at 2011-04-09T13:00