Results 1 to 6 of 6

Thread: Renaming file

  1. #1
    Join Date
    Nov 2011
    Posts
    7

    Unanswered: Renaming file

    I have rename a list of files in a directory :


    Old ------> New

    asdf_20140202bnc.ref --> asdfbnc_20140202.ref

    qwesaf_20140202_nmas_bw.ref --> qwesaf_nmas_bw_20140202.ref

    uqwrrtsqq_20140202_vb_bx_der.ref --> uqwrrtsqq_vb_bx_der_20140202.ref


    Please help me with a Unix command to do this task.

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    There are not enough samples to determine if there even is a relationship between the old file name and the new.
    Can you explain the rationale for the new file name?

  3. #3
    Join Date
    Nov 2011
    Posts
    7

    Clarificattion

    Before change there is a pattern _YYYYMMDD which is in the middle of the string(a_yyyymmdd_b).ext . We are renaming to newstring_YYYYMMDD(a_b_yyyymmdd).ref.

    Hope it clarifies your question.

    If it is not possible by one command help which a small shell script.


    Thanks in advance.
    Last edited by pp8771; 09-13-14 at 12:00. Reason: more comprehensive

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    # cat infile                            
    asdf_20140202bnc.ref                
    asdf_20140202_bnc.ref               
    qwesaf_20140202_nmas_bw.ref         
    uqwrrtsqq_20140202_vb_bx_der.ref    
    # cat t                             
    IFS=.                               
    while read file                     
    do                                  
            echo "mv $file \c"          
            ./t1 $file                  
    done <infile
                              
    # cat t1                            
    IFS=_                               
    ./t2 $1       
    
    # cat t                    
    #!/usr/bin/ksh             
    IFS=.                      
    while read file            
    do                         
            echo "mv $file \c" 
            ./t1 $file         
    done <t3      
    # ./t                                                                
    mv asdf_20140202bnc.ref asdf_20140202bnc_.ref                        
    mv asdf_20140202_bnc.ref asdf_bnc_20140202.ref                       
    mv qwesaf_20140202_nmas_bw.ref qwesaf_nmas_bw_20140202.ref           
    mv uqwrrtsqq_20140202_vb_bx_der.ref uqwrrtsqq_vb_bx_der_20140202.ref 
    #
    Was there a typo in your original first line of data, as not all the syllables of the file name are separated by an underscore.

    t strips the extension ".ref" from the file name, and submits the result to t1.
    t1 separates the file name into space separated syllables and passes that to t2 where $* is the number of syllables in the word, where each syllable that is not the date, is echoed without a linefeed, and finally the date is echoed with a line feed.

    t2

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    # cat infile                            
    asdf_20140202bnc.ref                
    asdf_20140202_bnc.ref               
    qwesaf_20140202_nmas_bw.ref         
    uqwrrtsqq_20140202_vb_bx_der.ref  
    
    # cat t                        
    #!/usr/bin/ksh                 
    IFS=.                          
    while read file ext            
    do                             
            echo "mv $file.ref \c" 
            ./t1 $file             
    done <infile                       
    #                                
    
                              
    # cat t1                            
    IFS=_                               
    ./t2 $1    
       
    # cat t2                          
    i=1                               
    while [ $i -le $# ]               
    do                                
            if [ "$1" != "20140202" ] 
            then                      
                    echo "$1_\c"      
            else                      
                    last=$1           
            fi                        
            shift                     
    done                              
    echo $last.ref                    
    
    # ./t                                                                
    mv asdf_20140202bnc.ref asdf_20140202bnc_.ref                        
    mv asdf_20140202_bnc.ref asdf_bnc_20140202.ref                       
    mv qwesaf_20140202_nmas_bw.ref qwesaf_nmas_bw_20140202.ref           
    mv uqwrrtsqq_20140202_vb_bx_der.ref uqwrrtsqq_vb_bx_der_20140202.ref 
    #
    Was there a typo in your original first line of data, as not all the syllables of the file name are separated by an underscore.

    t submits the result to t1.
    t1 separates the file name into space separated syllables and passes that to t2 where $* is the number of syllables in the word, where each syllable that is not the date, is echoed without a linefeed, and finally the date is echoed with a line feed.
    Last edited by kitaman; 09-14-14 at 00:01. Reason: fix typos

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Alternatively with bash:

    Code:
    # cat t4                                                   
    #!/bin/bash                                                
    d=$1 #date yyyymmdd                                        
    echo $d                                                    
    while read infile                                          
    do                                                         
            lf=${#infile}                                      
            f=${infile:0:lf-4}                                 
            f1=`expr ${#f} - 8`                                
            i=0                                                
            while [ $i -le $f1 ]                               
            do                                                 
            if [ "${f:i:8}" == "$d" ]                          
            then                                               
            i1=`expr $i - 1`                                   
            i8=`expr $i + 8`                                   
            i2=`expr $i - 1`                                   
            echo mv $infile ${f:0:$i1}${f:$i8}${f:$i2:9}".ref" 
            break                                              
            fi                                                 
            i=`expr $i + 1`                                    
            done                                               
    done <infile 
    
    # ./t4 20140202                                                      
    20140202                                                             
    mv asdf_20140202bnc.ref asdfbnc_20140202.ref                         
    mv asdf_20140202_bnc.ref asdf_bnc_20140202.ref                       
    mv qwesaf_20140202_nmas_bw.ref qwesaf_nmas_bw_20140202.ref           
    mv uqwrrtsqq_20140202_vb_bx_der.ref uqwrrtsqq_vb_bx_der_20140202.ref 
    #

Posting Permissions

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