简体   繁体   中英

sed replace number with number-1

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.)

Using perl

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

Output

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

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, eg by using a lookbehind assertion:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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