1

I need to batch rename bunch of files with id in name, adding a constant number to id

 <id>-xxx.txt => <id+shift>-yyy.txt

Any ideas how to make it? Some awk maybe?

haziz
  • 2,929
ts01
  • 399

1 Answers1

4
constant=42
for f in *.txt; do    # choose your pattern as appropriate.
    IFS='-.' read id suffix ext <<< "$f"
    newname="$(( 10#$id + constant ))-yyy.$ext"
    echo mv "$f" "$newname"
done

I added "10#" in the arithmetic expression to ensure the number is treated as base-10 even if it begins with a zero.

If this doesn't meet your needs, please provide more requirements in the question.

glenn jackman
  • 17,900
  • it's quite good, only problem is that 'xxx' part is variable, so i need to extract id and that part separately, modify id and merge again – ts01 Feb 05 '13 at 05:04
  • Does 'xxx' contain dashes or dots? If not, then this code will work: the suffix variable will contain whatever 'xxx' is. If there can be dashes and dots, I can provide alternatives, but this is exactly the kind of information you need to provide in the question. – glenn jackman Feb 05 '13 at 22:59
  • ok, I figured this out (IFS was quite new for me) and I made it working. Thanks a lot – ts01 Feb 06 '13 at 03:25