移除字串的標點符號 - Linux

By Rosalind
at 2011-04-09T10:24
at 2011-04-09T10:24
Table of Contents
※ 引述《jeremy007 (Linux藍海帝國)》之銘言:
: $ cat file.txt
: 2000,"1,234,567","5,678",3000,10000,"1,300"
: 如上述的文字格式, 每個欄位都用逗號 (,) 隔開
: 請問, 如何移除 "...." 符號內的逗號呢 ?
: ex: 預期的結果是
: 2000,1234567,5678,3000,10000,1300
#coding=utf8
import re, subprocess
def myrepl(m):
return m.group(1).replace(',', '')
# 處理前
s = subprocess.Popen(["cat", "file.txt"], stdout=subprocess.PIPE).communicate()[0]
print s
# 處理後
s = re.sub(re.compile(r'(".+?")'), myrepl, s)
print s
執行結果:
2000,"1,234,567","5,678",3000,10000,"1,300"
2000,"1234567","5678",3000,10000,"1300"
用Python寫的 可以弄成一行Linux指令 不過會很醜 = =
--
: $ cat file.txt
: 2000,"1,234,567","5,678",3000,10000,"1,300"
: 如上述的文字格式, 每個欄位都用逗號 (,) 隔開
: 請問, 如何移除 "...." 符號內的逗號呢 ?
: ex: 預期的結果是
: 2000,1234567,5678,3000,10000,1300
#coding=utf8
import re, subprocess
def myrepl(m):
return m.group(1).replace(',', '')
# 處理前
s = subprocess.Popen(["cat", "file.txt"], stdout=subprocess.PIPE).communicate()[0]
print s
# 處理後
s = re.sub(re.compile(r'(".+?")'), myrepl, s)
print s
執行結果:
2000,"1,234,567","5,678",3000,10000,"1,300"
2000,"1234567","5678",3000,10000,"1300"
用Python寫的 可以弄成一行Linux指令 不過會很醜 = =
--
Tags:
Linux
All Comments

By Bethany
at 2011-04-11T07:48
at 2011-04-11T07:48
Related Posts
移除字串的標點符號

By Carol
at 2011-04-09T01:48
at 2011-04-09T01:48
pcmanx連bbs的畫面延遲?

By Gary
at 2011-04-09T00:05
at 2011-04-09T00:05
出現"磁碟機隨時會壞掉"的訊息 該怎麼辦 ?

By Heather
at 2011-04-08T21:31
at 2011-04-08T21:31
Ubuntu 無法登入問題

By Carol
at 2011-04-08T18:36
at 2011-04-08T18:36
fedora 5 yum install卡住

By Skylar DavisLinda
at 2011-04-08T18:31
at 2011-04-08T18:31