Re: [問題] 移除字串的標點符號

看板Linux作者 (Ken)時間13年前 (2011/04/10 14:18), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串5/5 (看更多)
※ 引述《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如果不支援註解的,要把#開頭那行拿掉才能執行 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.205.130.47

04/10 17:29, , 1F
感謝您的協助 :)
04/10 17:29, 1F

04/11 01:22, , 2F
讚!! 您這個方法邏輯更簡單有效..
04/11 01:22, 2F

04/11 10:43, , 3F
謝謝
04/11 10:43, 3F
文章代碼(AID): #1DeKkj2j (Linux)
討論串 (同標題文章)
文章代碼(AID): #1DeKkj2j (Linux)