[英]How to delete a string with the same word and consecutively increasing number in a text file using a shell script?
For example I have: 例如,我有:
Hello1 :
Hello2 :
Hello3 :
How Could I delete all of these with a shell script. 如何使用Shell脚本删除所有这些。 The number reaches up all the way to 1000.
这个数字一直到1000。
sed -i '/^Hello[[:digit:]]\+\>/d' file.txt
或者,如果要输出到其他文件:
sed '/^Hello[[:digit:]]\+\>/d' file.txt > newfile.txt
If you wish to delete all the lines that contain only Hello(number) :
use below : 如果要删除仅包含
Hello(number) :
所有行,请使用以下命令:
Sample Input in file
file
样本输入
Hell
Hello1 :
No hello stuff here
Unjulating stuff
Hello2 :
Some sentence
Hello99 :
Script 脚本
sed -Ei '/^Hello[[:digit:]]+ :$/d' file
Sample Output in the modified file
修改后的
file
中的示例输出
Hell
No hello stuff here
Unjulating stuff
Some sentence
What happens above 上面发生了什么
^
in the pattern we check for the beginning of the line. ^
,我们检查行的开头。 Hello(number) :
using Hello[[:digit:]]+ :$
. Hello[[:digit:]]+ :$
检查模式Hello(number) :
Note that I used -E to enable sed extended regular expressions so I need not escape the + ie ( \\+
). \\+
)。 Here [[:digit:]]
is a class which contains all the decimal digits and +
is used to check if the pattern before it matches at least one time. [[:digit:]]
是一个类,它包含所有十进制数字,而+
用于检查之前的模式是否至少匹配一次。 $
$
检查行尾 d
option d
选项将其删除 inplace edit
option -i
so that the changes are directly saved to the file. inplace edit
选项-i
以便将更改直接保存到文件中。 If you wish to change the a line the begins with Hello(number) :
then use the below script 如果您想更改一行,则以
Hello(number) :
开头Hello(number) :
然后使用以下脚本
sed -Ei '/^Hello[[:digit:]]+ :/d' file
You might have notices that I just removed the $
, so our pattern matches any line that starts with Hello(number) :
您可能已经注意到,我刚刚删除了
$
,所以我们的模式匹配以Hello(number) :
开头的任何行Hello(number) :
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.