Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 18 of 18
Originally Posted by sponge So I ran into another problem. The files that I need to modify are numbered so that they start: 001 002 003 and so on... So ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353

    Quote Originally Posted by sponge View Post
    So I ran into another problem. The files that I need to modify are numbered so that they start:

    001
    002
    003
    and so on...
    So if I understand, you have files like "ImgA.1" and you want to rename them to "ImgB.001"? If so, then try this method for calculating the new number:
    Code:
    #  newnum=$(( $num + 100 ))
      newnum=$(printf '%03d' $num)
    I adapted your script and executed it but im getting this issue:

    ' value too great for base '
    It would help to see your script.

    Would it make sense to make a bash version of this script or would I run into the same problem?
    This is a bash script, not sure what you mean.

  2. #12
    Just Joined!
    Join Date
    Feb 2013
    Posts
    37
    Quote Originally Posted by atreyu View Post
    So if I understand, you have files like "ImgA.1" and you want to rename them to "ImgB.001"? If so, then try this method for calculating the new number:
    Code:
    #  newnum=$(( $num + 100 ))
      newnum=$(printf '%03d' $num)
    It would help to see your script.


    This is a bash script, not sure what you mean.


    Thanks Atreyu,

    sorry I didnt explain myself well before. I will try again:


    My files are

    img_A_2.00001
    img_A_2.00002
    img_A_2.00003
    -----------------
    img_A_2.00099

    and I would like them to be:

    img_B_2.00101
    img_B_2.00102
    img_B_2.00103
    -----------------
    img_B_2.00199

    Here is the script after I modified yours:


    #!/bin/bash
    files=$(ls img_A_2.[0-9]*)
    for file in $files; do
    num=$(echo $file|cut -f2 -d.)
    newnum=$(( $num + 100 ))
    newfile=$(echo $file|sed -e "s|^img_A_2\.$num$ | img_B_2.$newnum|")
    echo mv$file $newfile
    done


    Cheers

  3. #13
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    237
    hi,

    Code:
    #!/bin/bash
    
    shopt -s extglob nullglob
    for f in ./* #In case ./ is empty, nullglob is needed so $f will never be litterally ./*
    do
       nf="${f##*.}"
       nbnf="${#nf}"
       nf="${nf/#+(0)}" #This needs extglob
       echo mv "$f" "$(printf "%s.%.${nbnf}d\n" "${f%.*}" $((nf+100)))"
    done
    a bit shorter (one line less ):
    Code:
    #!/bin/bash
    shopt -s extglob nullglob
    for f in ./*
    do
       nf="${f##*.}"
       printf -v nf "%.${#nf}d" $((${nf/#+(0)}+100))
       echo mv "$f" "${f%.*}.$nf"
    done
    Last edited by watael; 02-14-2013 at 03:30 AM.

  4. $spacer_open
    $spacer_close
  5. #14
    Just Joined!
    Join Date
    Feb 2013
    Posts
    37
    Quote Originally Posted by watael View Post
    hi,

    Code:
    #!/bin/bash
    
    shopt -s extglob nullglob
    for f in ./* #In case ./ is empty, nullglob is needed so $f will never be litterally ./*
    do
       nf="${f##*.}"
       nbnf="${#nf}"
       nf="${nf/#+(0)}" #This needs extglob
       echo mv "$f" "$(printf "%s.%.${nbnf}d\n" "${f%.*}" $((nf+100)))"
    done
    a bit shorter (one line less ):
    Code:
    #!/bin/bash
    shopt -s extglob nullglob
    for f in ./*
    do
       nf="${f##*.}"
       printf -v nf "%.${#nf}d" $((${nf/#+(0)}+100))
       echo mv "$f" "${f%.*}.$nf"
    done
    Thank you watael,

    Im less familiar with your script. Also, which part of it would take care of the A to B modification?
    Cheers

  6. #15
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    237
    hi,

    which part of it would take care of the A to B modification?
    none, I forgot it

    Code:
    #!/bin/bash
    shopt -s extglob nullglob
    for f in ./*
    do
       df="${f//_A_/_B_}" #Here it is!
       nf="${f##*.}"
       printf -v nf "%.${#nf}d" $((${nf/#+(0)}+100))
       echo mv "$f" "${df%.*}.$nf"
    done

  7. #16
    Just Joined!
    Join Date
    Feb 2013
    Posts
    37
    Quote Originally Posted by watael View Post
    hi,

    none, I forgot it

    Code:
    #!/bin/bash
    shopt -s extglob nullglob
    for f in ./*
    do
       df="${f//_A_/_B_}" #Here it is!
       nf="${f##*.}"
       printf -v nf "%.${#nf}d" $((${nf/#+(0)}+100))
       echo mv "$f" "${df%.*}.$nf"
    done
    Awesome, thanks! I will try it with my files!

  8. #17
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,304
    Hi.

    Using GNU/Linux rename:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate rename files, arithmetic.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C rename
    
    pl " Current names of form img_A_2.00001:"
    ls img_A*
    
    pl " Results:"
    rename -nv 's/img_A/img_B/;s/(\d+)$/sprintf("%05d",$1+100)/e' img_A*
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    rename - ( /usr/bin/rename, 2009-05-27 )
    
    -----
     Current names of form img_A_2.00001:
    img_A_2.00001  img_A_2.00002  img_A_2.00003  img_A_2.00099
    
    -----
     Results:
    img_A_2.00001 renamed as img_B_2.00101
    img_A_2.00002 renamed as img_B_2.00102
    img_A_2.00003 renamed as img_B_2.00103
    img_A_2.00099 renamed as img_B_2.00199
    It's short, but requires some knowledge of perl.

    See man rename for details.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  9. #18
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by sponge View Post
    Here is the script after I modified yours:
    I see what you mean now. Well, you've already been giving some good advise, but just for completeness, here's what I'd do to make that change to the number suffix:
    Code:
    #!/bin/bash
    for file in $(ls Img_A_*.*); do
      num=$(echo $file|cut -f2 -d.)
      newnum=$(printf "%05d" $((10#$num + 100)))
      newfile=$(echo $file|sed -e "s|^Img_A_\(.*\).$num$|Img_B_\1.$newnum|")
      echo mv $file $newfile
    done
    btw, that weird notation in the $newnum value generation (10#$num) is required to force the number suffix (i.e., 00001) to be treated as a decimal number (and thus, amenable to having 100 added to it) versus an octal number.

Page 2 of 2 FirstFirst 1 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •