IMO 可能会更容易一些awk
:
awk '!/regex/ {$1=++x; print}' inputFile
在/.../
您可以将regex
发生在需要删除的行上。
测试:
$ cat inputFile
1 This is the first line
2 this is the second line
3 this is the third line
4 This is the fourth line
$ awk '!/second/ {$1=++x; print}' inputFile
1 This is the first line
2 this is the third line
3 This is the fourth line
$ awk '!/third/ {$1=++x; print}' inputFile
1 This is the first line
2 this is the second line
3 This is the fourth line
$ awk '!/first/ {$1=++x; print}' inputFile
1 this is the second line
2 this is the third line
3 This is the fourth line
注意:由于我们正在重新构建该$1
字段,任何空白序列都将被删除。