简体   繁体   中英

How to increment and find/replace number using sed or perl

I have a git tag named "a_tag-30" and I want find the last tag and increment it by 1 using a sed or perl expression, so the result will be "a_tag-31". Can someone please help me with this?

Following git command gets all the tags matching the pattern "a_tag-*" sorted in descending order via "-taggerdate" key.

git tag --list 'a_tag-*' --sort=-taggerdate | head -1 | awk -F- '{print "a_tag-" $NF+1}'

Then the first line is fed to the awk command where "-" used as a field separator. Hence $NF positional parameter (ie NF means Number of Fields and it is a predefined variable in awk) holds the value of last field.

Finally the new tag value is printed by incrementing $NF by 1.

This kind of thing is easiest using a combination of utilities in bash. For example:

echo "a_tag-$(($(git tag --list 'a_tag-*' 
    | sed -e 's/^.*-\([0-9]*\)/\1/'  
    | sort -nr
    | head -1) + 1))" > output

This will write the new tag to output .

As well as having a prefix "a_tag", It assumes all your tags end in a dash followed by a number.

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