#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    NJ, USA
    Posts
    6
    Rep Power
    0

    Batch rename using regexp, sed


    On Linux, I'm in the process of trying to rename (and learn to rename)
    Code:
    N=008x008_Beta=4.00_tol=1e-08_t=0000000_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0010000_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0020000_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0030000_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0040000_U.csv
    ...
    N=008x008_Beta=4.00_tol=1e-08_t=9990000_U.csv
    to
    Code:
    N=008x008_Beta=4.00_tol=1e-08_t=0000000_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0000001_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0000002_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0000003_U.csv
    N=008x008_Beta=4.00_tol=1e-08_t=0000004_U.csv
    ...
    N=008x008_Beta=4.00_tol=1e-08_t=0000999_U.csv

    So far, I have been able to match the pattern and remove the leading 3 digits using this
    Code:
    for i in N\=008x008_Beta\=4.00_tol\=1e-08*U.csv;
    do j=`echo $i | sed 's/_t=[0-9][0-9][0-9]0000_/_t=0000_/'`;
       echo mv $i $j;
    done
    I want to move the three digits back, which means I want to group them using regexp's parenthises. However, this command does not work
    Code:
    for i in N\=008x008_Beta\=4.00_tol\=1e-08*U.csv; 
    do j=`echo $i | sed 's/_t=([0-9][0-9][0-9])0000_/_t=0000_/'`; 
       echo mv $i $j; 
    done
    It just returns the same filename in $j as is already in $i.

    I don't think I did anything wrong. Why isn't it working?

    Note that I also can not get the following to work (even though I thought I had them working briefly before I took a break).
    Code:
    for i in N\=008x008_Beta\=4.00_tol\=1e-08*U.csv; 
    do j=`echo $i | sed 's/_t=\d\d\d0000_/_t=0000_/'`;
       echo mv $i $j; 
    done
    Code:
    for i in N\=008x008_Beta\=4.00_tol\=1e-08*U.csv; 
    do j=`echo $i | sed 's/_t=[0-9]{3}0000_/_t=0000_/'`;
       echo mv $i $j; 
    done
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    NJ, USA
    Posts
    6
    Rep Power
    0
    Well, I solved my proximate issue using redirection and emacs like this:

    Code:
    for i in N\=008x008_Beta\=4.00_tol\=1e-08_t\=*.csv; 
    do j=`echo $i | sed 's/_t=[0-9][0-9][0-9]0000_/_t=0000_/'`;
       echo mv $i $j;
    done >batchrename
    Then I did
    Code:
    chmod +x batchrename
    emacs batchrename
    In emacs, I used the kill-rectangle and yank-rectangle, as documented here: stackoverflow.com/questions/10486248/deleting-specific-columns-of-text-in-emacs-xemacs-using-the-keyboard. And finally I executed batchrename at the command prompt.

    This solution didn't even require regular expressions. However, I still want to learn what went wrong in my OP and how to fix it (but not quite the priority it had before).

IMN logo majestic logo threadwatch logo seochat tools logo