简体   繁体   English

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

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

I have a text file which has the following lines: 我有一个文本文件,其中包含以下几行:

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

I want to run a command in UNIX (say, sed ) which will edit the contents of the text file as: 我想在UNIX中运行命令(例如sed ),该命令将文本文件的内容编辑为:

Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

I came across some commands such as sed '/^pattern/ d' to remove all lines after pattern. 我遇到了一些命令,例如sed '/^pattern/ d'以删除模式之后的所有行。 But where is the text file specified in the command? 但是命令中指定的文本文件在哪里?

With awk you can set - and _ as field separator ( -F[-_] ) and print the first block ( {print $1} ): 使用awk可以将-_设置为字段分隔符( -F[-_] )并打印第一个块( {print $1} ):

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

outputs: 输出:

Customer Details Report 
DemandResetFailureReport
ExcessiveMissingReadsReport
LipaBillingSumCheckReport
LipaUsageBillingReport

I always use perl -pi, as follows: 我总是使用perl -pi,如下所示:

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

If a backup of the original is needed, specify a suffix for the backup file, for example: 如果需要备份原始文件,请为备份文件指定后缀,例如:

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

See also the following topic on editing files in place: sed edit file in place 另请参见以下有关在适当位置编辑文件的主题: sed在适当位置编辑文件

I would suggest using sed -i 's/[-_].*//' file.txt . 我建议使用sed -i 's/[-_].*//' file.txt Your text file ( file.txt ) must be passed as argument (I chose that way) or on standard input ( sed 's/[-_].*//' < file.txt > file2.txt ), but that way you could not edit it in-place ( -i ). 您的文本文件( file.txt )必须作为参数(我选择了这种方式)或在标准输入( sed 's/[-_].*//' < file.txt > file2.txt )上sed 's/[-_].*//' < file.txt > file2.txt您无法就地编辑( -i )。 Be sure not to use sed … <file.txt >file.txt as that will delete your file.txt contents . 确保不要使用sed … <file.txt >file.txt因为那样会删除file.txt内容

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

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

Another awk 另一个awk

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

This removes what you does not want and print the rest. 这将删除您不需要的内容并打印其余部分。

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

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