Regex 在Unix中删除文本文件中模式后的以下字符

Regex 在Unix中删除文本文件中模式后的以下字符,regex,shell,unix,sed,awk,Regex,Shell,Unix,Sed,Awk,我有一个文本文件,其中包含以下行: 客户详细信息报告-A03_2014-01-04_09-00-09.txt DemandResetFailureReport_2014-01-04_11-00-08.txt 超标读数报告_2014-01-04_09-00-11.txt LipaBillingSumCheckReport_2014-01-04_11-00-08.txt LipaUsageBillingReport_2014-01-04_12-55-06.txt 我想在UNIX中运行一个命令(例如

我有一个文本文件,其中包含以下行:

客户详细信息报告-A03_2014-01-04_09-00-09.txt
DemandResetFailureReport_2014-01-04_11-00-08.txt
超标读数报告_2014-01-04_09-00-11.txt
LipaBillingSumCheckReport_2014-01-04_11-00-08.txt
LipaUsageBillingReport_2014-01-04_12-55-06.txt
我想在UNIX中运行一个命令(例如,
sed
),该命令将文本文件的内容编辑为:

<代码>客户详细信息报告 DemandResetFailureReport 过度丢失读数报告 口红报告 LipaUsageBillingReport
我遇到了一些命令,比如
sed'/^pattern/d'
来删除pattern之后的所有行。但是命令中指定的文本文件在哪里?

使用
awk
可以将
-
-
设置为字段分隔符(
-F[-\u]
)并打印第一个块(
{print$1}
):


使用
awk
可以将
-
-
设置为字段分隔符(
-F[-\u]
)并打印第一个块(
{print$1}
):

产出:

Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport
产出:

Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

我建议使用
sed-I的/[-\u]./'file.txt
。您的文本文件(
file.txt
)必须作为参数传递(我选择了这种方式)或在标准输入(
sed的/[-\u].*/'file2.txt
)上传递,但这样您就无法在位编辑它(
-I
)。请确保不要使用
sed…file.txt
as.

我建议使用
sed-I的/[-\u].//'file.txt
。您的文本文件(
file.txt
)必须作为参数传递(我选择了这种方式)或在标准输入(
sed的/[-\u].*/'file2.txt
)上传递,但这样您就无法在位编辑它(
-I
)。请确保不要将sed…file.txt作为使用。

我始终使用perl-pi,如下所示:

$ perl -pi -e 's/[-_].*//' file
$ cat file
Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport
如果需要备份原始文件,请为备份文件指定后缀,例如:

$ perl -pi.bak -e 's/[-_].*//' file

另请参见以下关于就地编辑文件的主题:

我始终使用perl-pi,如下所示:

$ perl -pi -e 's/[-_].*//' file
$ cat file
Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport
如果需要备份原始文件,请为备份文件指定后缀,例如:

$ perl -pi.bak -e 's/[-_].*//' file
另请参见以下关于就地编辑文件的主题:

这可能适合您(GNU-sed):

这可能适用于您(GNU-sed):


另一个
awk

awk '{sub(/[-_].*/,x)}1' file
Customer Details Report
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

这将删除您不需要的内容并打印其余内容。

另一个
awk

awk '{sub(/[-_].*/,x)}1' file
Customer Details Report
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

这将删除您不需要的内容并打印其余内容。

OP不希望输出中包含
\U 2014
。解决后,只保留
awk
版本。谢谢,Kent 2014:)最好确保引用-F模式。如果您有一个名为“u”的文件,或者启用了nullglob选项,那么您就有麻烦了。OP不想在输出中包含
\u 2014
。解决后,只需保留
awk
版本。谢谢,Kent 2014:)最好确保引用-F模式。如果您有一个名为“_”的文件,或者启用了nullglob选项,那么您就有麻烦了。顺便说一句,sed'/^pattern/d'将删除所有开头有单词
pattern
的行。如果使用正则表达式而不是
^pattern
,则会删除与该模式匹配的所有行。顺便说一句
sed'/^pattern/d'
会删除所有开头有单词
pattern
的行。如果使用正则表达式而不是
^pattern
,则会删除与该模式匹配的所有行。