简体   繁体   English

sed:在n个字符的字符串的开头替换字符

[英]sed: replace character at the beginning of a string of n characters

I have: 我有:

12223335566
19988776655
9918877665566
44410007777222
etc

I am trying to find the sed syntax for replacing 1 with nothing only when 1 is at the beginning of a string composed of 10 digits so that the above input should look like this: 我试图找到用于替换1的sed语法,只有当1位于由10位数组成的字符串的开头时 ,以便上面的输入应如下所示:

2223335566
9988776655
9918877665566
44410007777222

As you can see, the replacement should occur only in the first two strings, leaving the other two untouched because even though the 1 in them is followed by 10 digits, it is not at the beginning. 正如您所看到的,替换应仅在前两个字符串中发生,而另外两个字符串保持不变,因为即使它们中的1后跟10个数字,它也不在开头。

使用sed

sed -r 's/^1([0-9]{10})$/\1/' file

要从11位数字中删除开头1:

sed 's;^1\([0-9]\{10\}\)$;\1;' filename

Here is an awk solution 这是一个awk解决方案

cat file
12223335566
19988776655
1245245543
14545klk342
9918877665566
44410007777222

awk '$1+0>10000000000 && $1+0<199999999999 {sub(/^1/,x)}8' file
2223335566
9988776655
1245245543
14545klk342
9918877665566
44410007777222

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

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