繁体   English   中英

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

[英]Removing the following characters after a pattern in a text file in Unix

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

Customer Details Report - A03_2014-01-04_09-00-09.txt
DemandResetFailureReport_2014-01-04_11-00-08.txt
ExcessiveMissingReadsReport_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 ),该命令将文本文件的内容编辑为:

Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

我遇到了一些命令,例如sed '/^pattern/ d'以删除模式之后的所有行。 但是命令中指定的文本文件在哪里?

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

$ awk -F"[-_]" '{print $1}' file
Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport
grep -o '^[^-_]*' 

输出:

Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

我总是使用perl -pi,如下所示:

$ perl -pi -e 's/[-_].*//' file
$ cat file
Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

如果需要备份原始文件,请为备份文件指定后缀,例如:

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

另请参见以下有关在适当位置编辑文件的主题: sed在适当位置编辑文件

我建议使用sed -i 's/[-_].*//' file.txt 您的文本文件( file.txt )必须作为参数(我选择了这种方式)或在标准输入( sed 's/[-_].*//' < file.txt > file2.txt )上sed 's/[-_].*//' < file.txt > file2.txt您无法就地编辑( -i )。 确保不要使用sed … <file.txt >file.txt因为那样会删除file.txt内容

这可能对您有用(GNU sed):

sed -ri 's/( -|_).*//' file

另一个awk

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM