SED -替换字符串中间出现的第一个数字

时间:2021-08-06 16:52:39

I just started with regex and sed, and I am having an issue with the following:

我刚开始使用regex和sed,我有以下问题:

echo "abc2def3ghi" | sed 's/^\(.*\)[0-9]\(.*\)$/"&" \1\2/i'  

I am looking to remove the first digit (2 in this case, but can by any single digit), but I get the following as a result:

我想删除第一个数字(在这种情况下是2,但是可以删除任何一个数字),但是我得到以下结果:

"abc2def3ghi" abc2defghi

How would I go about accomplishing this?

我该怎么做呢?

Thanks.

谢谢。

2 个解决方案

#1


2  

Try this :

试试这个:

sed 's/[0-9]//1' file.txt

The number 1 in s///1 stands for Nth occurence

s///1中的数字1表示第n次发生

#2


1  

sed has no reluctant/non-greedy quantifier, so you have to match zero or more non-digits specifically:

sed没有不情愿的/非贪婪的量词,因此您必须特别匹配零或更多非数字:

^\([^0-9]*\)[0-9]\(.*\)$

#1


2  

Try this :

试试这个:

sed 's/[0-9]//1' file.txt

The number 1 in s///1 stands for Nth occurence

s///1中的数字1表示第n次发生

#2


1  

sed has no reluctant/non-greedy quantifier, so you have to match zero or more non-digits specifically:

sed没有不情愿的/非贪婪的量词,因此您必须特别匹配零或更多非数字:

^\([^0-9]*\)[0-9]\(.*\)$