sed replace number with number-1

有些话、适合烂在心里 提交于 2019-12-25 09:12:04

问题


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 #######################################

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.

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 #######################################

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


回答1:


Using perl

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

Output

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



回答2:


I'd use perl in this case, with the e modifier to evaluate an expression in the replacement part:

perl -pe 's/(Tree)(\d+)/$1 . ($2 - 1)/ge'

You can also take advantage of more advanced regular expression support, e.g. by using a lookbehind assertion:

perl -pe 's/(?<=Tree)(\d+)/$1 - 1/ge'



回答3:


Using gnu awk below

awk '{
    for(i=1;i<=NF;i++){
     $i ~ /^Tree[[:digit:]]+$/
     {
     n=(gensub(/Tree/,"",1,$i)-1);
     $i=gensub(/Tree([[:digit:]]+)/,"Tree"n,1,$i);
     }
     }
     print}' file

would do the trick




回答4:


You can try this;

sed 's/[^0-9,-]*//g' filename | sed -e "s/-/ /g" | awk '{print "##################################### topd Tree"$1-1 " - Tree" $2-1 " #######################################"}'

awk '{gsub(/Tree/,"Tree ",$0);print $1" "$2" "$3$4-1" "$5" "$6$7-1" "$8}' filename


来源:https://stackoverflow.com/questions/38348547/sed-replace-number-with-number-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!