移除字串的標點符號 - Linux

Steve avatar
By Steve
at 2011-04-10T09:55

Table of Contents

※ 引述《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"

--
Tags: Linux

All Comments

Christine avatar
By Christine
at 2011-04-12T20:21
謝謝您的協助, 這個一行文可以動, 但我需要好好研究 ^_^
Valerie avatar
By Valerie
at 2011-04-15T08:00
謝謝 dyoll 的 feedback,我要消化一下。

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分別對 ...

移除字串的標點符號

Rosalind avatar
By Rosalind
at 2011-04-09T10:24
※ 引述《jeremy007 (Linux藍海帝國)》之銘言: : $ cat file.txt : 2000,and#34;1,234,567and#34;,and#34;5,678and#34;,3000,10000,and#34;1,300and#34; : 如上述的文字格式, 每個欄位都用逗號 (,) ...

移除字串的標點符號

Carol avatar
By Carol
at 2011-04-09T01:48
※ 引述《jeremy007 (Linux藍海帝國)》之銘言: : $ cat file.txt : 2000,and#34;1,234,567and#34;,and#34;5,678and#34;,3000,10000,and#34;1,300and#34; : 如上述的文字格式, 每個欄位都用逗號 (,) ...