sed用数字1替换数字 [英] sed replace number with number-1

查看:72
本文介绍了sed用数字1替换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sed是否可以处理替换并同时评估表达式以某种方式替换?

Can sed handle a substitution and simultaneously evaluate an expression to substitute somehow?

例如,我在文本文件中有以下条目:

For example, I have the following entry in a text file:

##################################### topd Tree1 - Tree14 #######################################

出于我不愿讨论的原因,相对于到目前为止我一直在使用的另一种编号方案,每棵树的编号为N + 1.

For reasons I won't go in to, each Tree's number is N+1 relative to another numbering scheme I've been using so far.

可以sed(或真的任何util,sed只是我进行查找和替换操作的地方),找到 TreeN 的每个实例,然后将它们替换为 TreeN-1 代码>,使上面的行看起来像这样:

Could sed (or any util really, sed is just my go to for find-and-replace operations), find every instance of TreeN and replace them with TreeN-1 such that the above line would then look like:

##################################### topd Tree0 - Tree13 #######################################

(使用Ubuntu 12.02,几乎可以进行所有操作.)

(Using Ubuntu 12.02, so pretty much anything goes.)

推荐答案

使用perl

perl -pe 's/Tree\K\d+/$&-1/ge' file

输出

##################################### topd Tree0 - Tree13 #######################################

这篇关于sed用数字1替换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆