2014-05-03 28 views
3

你好在下面的sed命令中,我需要在第二組圓括號中不接受以下一組單詞:Inc The Ltd LLC如何使用sed搜索和替換,而不包含一組字符?

它會打破list.txt中的以下數據公司名稱在一行中,公司名稱在逗號後面,但有時候「Inc」,「Ltd」,「LLC」和「The」跟隨公司。

這是相當先進的正則表達式,我似乎得不到。

sed -re 's/([a-zA-Z.]), (Need code here)/\1\n\2/g' list.txt 

LIST.TXT具有以下數據:

Electronic Arts, Inc., Electronic Arts Ltd. 
Activision Publishing, Inc., ak tronic Software & Services GmbH 
Coplin Software 
Electronic Arts, Inc. 
Electronic Arts, Inc. 
In-Fusio 
Activision Publishing, Inc. 
Domark Ltd. 
Electronic Arts, Inc. 
Electronic Arts, Inc. 
Aspyr Media, Inc., Electronic Arts, Inc. 
Activision Deutschland GmbH, Activision Publishing, Inc., ak tronic Software & Services GmbH, Noviy Disk, Square Enix Co., Ltd. 
Electronic Arts, Inc. 
Electronic Arts, Inc., Electronic Arts Ltd. 
Electronic Arts, Inc. 
Electronic Arts, Inc. 
Electronic Arts, Inc., Electronic Arts Square, K.K., MGM Interactive 
Electronic Arts Ltd. 

預期輸出(注意逗號):

GarageGames, Inc. 
The Avalon Hill Game Company 
Microforum International, The 
Telenet Japan Co., Ltd. 
Glu Mobile, Inc. 
Warner Bros. Digital Distribution 
Atari, Inc. 
+0

你想不匹配一組字符,或特定的詞? – Barmar

+0

您使用'[^ ​​abcxyz]'匹配不在'xyzabc'中的字符。 – Barmar

+0

你可以發佈和'list.txt'的簡短例子嗎? –

回答

3
perl -pe 's/([^,]), (?!Inc|LLC|The|Ltd)/\1\n/g' list.txt 
+0

sed不支持'(?!Inc | LLC | The | Ltd)所以@hwnd使用Perl,現在我認爲它比SED更好,並且更有用。但你的答案仍然需要一些調整。我最終使用'perl -pe's /([a-zA-Z \。]),(?!Inc | LLC | The | Ltd)/ \ 1 \ n \ 2/g'list.txt'它仍然沒有表現出我的意圖,但它的結局。如果公司名稱是'ABC公司,IncaCola公司',它將不會將其分成兩部分,因爲'IncaCola公司'以'Inc'開頭。它接近那些,謝謝@hwnd – user1742835

+0

那時你可以調整字邊界'\ b' – hwnd

3

根據你的榜樣list.txt,你可以試試這個:

sed -re 's/(,)?(Inc.|The|Ltd.?|LLC)//g' list.txt| tr ',' '\n' | sed -re 's/(.*)/\1/g' | sed -re '/^\s*$/d' | sed -re 's/(^ | $)//g' 

輸出:

Electronic Arts 
Electronic Arts 
Activision Publishing 
ak tronic Software & Services GmbH 
Coplin Software 
Electronic Arts 
Electronic Arts 
In-Fusio 
Activision Publishing 
Domark 
Electronic Arts 
Electronic Arts 
Aspyr Media 
Electronic Arts 
Activision Deutschland GmbH 
Activision Publishing 
ak tronic Software & Services GmbH 
Noviy Disk 
Square Enix Co. 
Electronic Arts 
Electronic Arts 
Electronic Arts 
Electronic Arts 
Electronic Arts 
Electronic Arts 
Electronic Arts Square 
K.K. 
MGM Interactive 

注:

可以通過管道上面的列表awk並只顯示獨特的結果,例如:

sed -re 's/(,)?(Inc.|The|Ltd.?|LLC)//g' list.txt| tr ',' '\n' | sed -re 's/(.*)/\1/g' | sed -re '/^\s*$/d' | sed -re 's/(^ | $)//g'| awk '!seen[$0]++' 

輸出:

Electronic Arts 
Activision Publishing 
ak tronic Software & Services GmbH 
Coplin Software 
In-Fusio 
Domark 
Aspyr Media 
Activision Deutschland GmbH 
Noviy Disk 
Square Enix Co. 
Electronic Arts Square 
K.K. 
MGM Interactive 
+0

實際上它需要在每個公司名稱之後添加換行符。理想情況下,它必須在每個逗號後面,但在我的情況下,有時候最後是「Inc.」 「」有限責任公司「」有限責任公司「,需要與公司名稱。我只是在我的問題中包含了一些list.txt的內容。 – user1742835

+0

您的預期產出是多少? – hwnd

+0

hwnd檢查問題,我張貼輸出。 – user1742835

0

一個perl版本:

$ perl -anlF'(?!,[\x20](?:Inc|Ltd|LLC|The).?),[\x20]' -e '$n{$_}++ for @F; END { print join "\n", sort keys %n; }' test.txt 
Activision Deutschland GmbH 
Activision Publishing, Inc. 
Aspyr Media, Inc. 
Coplin Software 
Domark Ltd. 
Electronic Arts Ltd. 
Electronic Arts Square 
Electronic Arts, Inc. 
In-Fusio 
K.K. 
MGM Interactive 
Noviy Disk 
Square Enix Co., Ltd. 
ak tronic Software & Services GmbH 
1
sed -nr '/^ *([^,]+(, *(Inc\.?|The|Ltd\.?|LLC))?)(,(.*))?/ { 
        s//\1\n\5/ 
        P 
        D 
}'