移除字串的標點符號 - Linux

Quintina avatar
By Quintina
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如果不支援註解的,要把#開頭那行拿掉才能執行

--
Tags: Linux

All Comments

Necoo avatar
By Necoo
at 2011-04-12T12:35
感謝您的協助 :)
Bennie avatar
By Bennie
at 2011-04-17T05:56
讚!! 您這個方法邏輯更簡單有效..
Megan avatar
By Megan
at 2011-04-20T10:21
謝謝

移除字串的標點符號

Steve avatar
By Steve
at 2011-04-10T09:55
※ 引述《antontw (三十個扮上進青年之一)》之銘言: : 設計內容: : $ cat file.txt : 2000,and#34;1,234,567and#34;,and#34;5,678and#34;,3000,10000,and#34;1,300and#34; : 1000,and#34;1,1 ...

FreeRadius一定得搭配LDAP嗎

Dorothy avatar
By Dorothy
at 2011-04-09T21:02
小弟安裝了FreeRadius 遵照 http://tw.myblog.yahoo.com/jw!xk77IlqaEQfH7drhkxfU3rmaCNnffP_1/article?mid=37 的步驟 但是在下這行指令後: radtest test test localhost 0 testing12 ...

sed用法 插入

Frederic avatar
By Frederic
at 2011-04-09T14:44
剛開始學習,所以有些應用還是不太熟 我現在要在檔案的最前頭插入兩行 假設插入 example 1 example 2 在某個檔案內文的前頭 需要怎麼寫入 本來是想用網路所說的 # 在匹配式樣「regex」的行之前插入一空行 sed and#39;/regex/{x;p;x;}and#39; ...

想學習虛擬化技術

Regina avatar
By Regina
at 2011-04-09T13:44
我們公司內部電腦數量大概有四十台左右 因為BSA的關係 軟體授權成本一直都很高= =a 加上為了防止員工私自灌盜版軟體以及有效集中管理資料 一直都花了蠻大的心力與成本在這上面 因此 我一直在研究導入linux的可行性 基本上 某程度而言是可行的 (但是網路印表機目前克服不了XD) 之前長期配合的廠 ...

VMWare Player做一個NAT

Franklin avatar
By Franklin
at 2011-04-09T13:00
感謝鄉民的協助,網路卡指定的問題解決了,現在又有另一個問題。 目前用VMware Player建立一個安裝Ubuntu 10.10的虛擬機器, Host os是XP,配有兩張實體網路卡。 VM的兩張虛擬網路卡設定為bridge,Virtual Network Editor 設定vnet0和vnet2分別對 ...