Regex 我如何搜索&;是否使用sed替换而不包含一组字符?

Regex 我如何搜索&;是否使用sed替换而不包含一组字符?,regex,linux,bash,sed,Regex,Linux,Bash,Sed,Hello在下面的sed命令中,我需要在第二组括号中包含不接受以下一组单词的代码:Inc the Ltd LLC 它将打破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,

Hello在下面的sed命令中,我需要在第二组括号中包含不接受以下一组单词的代码:Inc the Ltd LLC

它将打破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.
预期输出(注意逗号):


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

注意:

您可以通过管道将上述列表连接到
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

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

你想不匹配一组字符或特定的单词吗?你可以使用
[^abcxyz]
来匹配一个不在
xyzabc
中的字符。你可以发布
list.txt
的简短示例吗?我已经更正了这个问题。list.txt
Activision Deutschland GmbH的简短示例,Activision Publishing,Inc。,ak tronic Software&Services
它应该在逗号处打断公司名称,但您可以看到,有些公司名称在有逗号时不需要打断,因为公司名称的一部分仍在继续。实际上,它需要在每个公司名称后添加一个换行符。理想情况下,它必须在每个逗号之后,但在我的例子中,以下内容有时在“Inc.”“Ltd”“LLC”的末尾,需要使用公司名称。我刚才在我的问题中包含了list.txt的一些内容。您期望的输出是什么?请检查问题,我发布了输出。@Tuga您可以使用
-r
标志而不是转义
sed-re的/(,\s*)?(Inc\.| The | Ltd | LLC)//g'文件
@Tuga见此。
-r
标志用于扩展正则表达式=)sed不支持
(?!Inc | LLC | The | Ltd)
部分,因此@hwnd使用了perl,现在我认为它比sed更好、更有用。但你的答案还需要稍作调整。我最终使用了
perl-pe的/([a-zA-Z\),(?!Inc | LLC | The | Ltd)/\1\n\2/g'list.txt
它仍然没有按照我的意图运行,但已经接近了。如果公司名称是ABC公司,IncaCola,Inc.,则不会将其拆分为两个,因为
IncaCola,Inc.
Inc
开头。关闭这些,谢谢@hwnd,此时您可以调整单词边界
\b
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
$ 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
sed -nr '/^ *([^,]+(, *(Inc\.?|The|Ltd\.?|LLC))?)(,(.*))?/ {
                   s//\1\n\5/
                   P
                   D
}'             
perl -pe 's/([^,]), (?!Inc|LLC|The|Ltd)/\1\n/g' list.txt