How do i search & replace using sed and not include a group of characters?

时间:2022-06-01 20:12:16

Hello in the following sed command i need to have in the second group of parenthesis code that will NOT accept the following group of words: Inc The Ltd LLC

你好,在下面的sed命令中,我需要在第二组括号中使用不接受以下组词的组:Inc The Ltd LLC

It will break the following data in list.txt to have each company name on a line, the company names are after commas but some times "Inc", "Ltd", "LLC", and "The" follow a company.

它会破坏list.txt中的以下数据,使每个公司名称都在一行上,公司名称在逗号之后,但有时“Inc”,“Ltd”,“LLC”和“The”跟随公司。

This is pretty advance regular expression that i cant seem to get.

这是一个非常先进的正则表达式,我似乎无法得到。

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

list.txt has the following data:

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.

expected output(notice the commas):

预期输出(注意逗号):

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

4 个解决方案

#1


3  

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

#2


3  

Based on your example list.txt, you can try this:

根据您的示例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'

OUTPUTS:

输出:

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

NOTE:

注意:

You can pipe the above list to awk and display only unique results, ex:

您可以将上面的列表传递给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]++'

Outputs:

输出:

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

#3


1  

sed -nr '/^ *([^,]+(, *(Inc\.?|The|Ltd\.?|LLC))?)(,(.*))?/ {
                   s//\1\n\5/
                   P
                   D
}'             

#4


0  

A perl version:

一个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


3  

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

#2


3  

Based on your example list.txt, you can try this:

根据您的示例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'

OUTPUTS:

输出:

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

NOTE:

注意:

You can pipe the above list to awk and display only unique results, ex:

您可以将上面的列表传递给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]++'

Outputs:

输出:

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

#3


1  

sed -nr '/^ *([^,]+(, *(Inc\.?|The|Ltd\.?|LLC))?)(,(.*))?/ {
                   s//\1\n\5/
                   P
                   D
}'             

#4


0  

A perl version:

一个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