[SOLVED] Rename multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [SOLVED] Rename multiple files
# 8  
Old 12-05-2012
I've extrapolated your requirements as best I can from the limited information. This should accomodate exponents to + or - 30 I hope. Requires the bash or ksh shell. It assumes the numbers are in the format [+- ]0.1234E[+- ]00

It works by converting numbers into a list like
Code:
-0.000000000000000000000000000020000000000000000000000000000000 t_-0.20000E+03.dat
+0.000000000000000000000000000000000000000000000000000000000000 t_ 0.0000_.dat
+0.000000000000000000000000000000000010000000000000000000000000 t_ 0.10000E-03.dat
+0.000000000000000000000000000000000020000000000000000000000000 t_ 0.20000E-03.dat
+0.000000000000000000000000000000000200000000000000000000000000 t_ 0.20000E-02.dat
+0.000000000000000000000000000000000210000000000000000000000000 t_ 0.21000E-02.dat
+0.000000000000000000000000000000000220000000000000000000000000 t_ 0.22000E-02.dat
+0.000000000000000000000000000000002000000000000000000000000000 t_ 0.20000E-01.dat
+0.000000000000000000000000000000002000000000000000000000000000 t_ 2.00000E-02.dat
+0.000000000000000000000000000000002000000000000000000000000000 t_ 20.00000E-03.dat
+0.000000000000000000000000000000020000000000000000000000000000 t_ 0.20000E-00.dat
+0.000000000000000000000000000000200000000000000000000000000000 t_ 0.20000E+01.dat
+0.000000000000000000000000000002000000000000000000000000000000 t_ 0.20000E+02.dat
+0.000000000000000000000000000020000000000000000000000000000000 t_ 0.20000E+03.dat

which will sort cleanly with sort -n then reading it in order to get the filenames and move them.

Code:
#!/bin/bash

OLDIFS="$IFS"
IFS=" _tdatE. "

N=0

for FILE in t_*.dat
do
        # Convert exponential notation into fixed point.
        # We do this by removing zeroes from the front
        # to increase the exponent, adding zeroes to the front
        # to decrease the exponent, and padding zeroes onto the
        # end to a fixed length.  This will produce a number
        # that can be sorted.

        set -- $FILE
        set -- $*

        SIGN="+"

        if [ "${1:0:1}" = "-" ]
        then
                SIGN="-"
                set -- ${1:1} ${2} ${3}
        fi

        if [ "${1:0:1}" = "+" ]
        then
                set -- ${1:1} $2 $3
        fi

        EXP=${3}
        VAL="000000000000000000000000000000${1}${2}"

        LEN="${#1}"
        while [ "$LEN" -gt 1 ]
        do
                VAL="${VAL:1}"
                let LEN=LEN-1
        done


        [ -z "$EXP" ] && EXP="0"

        # Strip leading zeroes from exponent
        while [ "${EXP:0:2}" = "-0" ] ; do EXP=${EXP/-0/-} ; done
        while [ "${EXP:0:2}" = "+0" ] ; do EXP=${EXP/+0/+} ; done
        [ "$EXP" = "-" -o "$EXP" = "+" ] && EXP=0

        # Strip zeroes off the front of the number for positive exponent
        while [ "$EXP" -gt 0 ]
        do
                VAL="${VAL:1}"
                let EXP=EXP-1
        done

        # Add zeroes to the front of the number for negative exponent
        while [ $EXP -lt 0 ]
        do
                VAL="0${VAL}"
                let EXP=EXP+1
        done

        while [ "${#VAL}" -lt 60 ] ; do VAL="${VAL}0" ; done

        printf "%s0.%s\t%s\n" ${SIGN} "${VAL}" "${FILE}"
done | sort -n | while IFS="$OLDIFS" read ORDER FILENAME
do
        NAME=$(printf "t%05d.dat" $N)
        let N=N+1
        echo mv "$FILENAME" "$NAME"
done

Code:
$ ./float.sh

mv t_-0.20000E+03.dat t00000.dat
mv t_ 0.0000_.dat t00001.dat
mv t_ 0.10000E-03.dat t00002.dat
mv t_ 0.20000E-03.dat t00003.dat
mv t_ 0.20000E-02.dat t00004.dat
mv t_ 0.21000E-02.dat t00005.dat
mv t_ 0.22000E-02.dat t00006.dat
mv t_ 0.20000E-01.dat t00007.dat
mv t_ 2.00000E-02.dat t00008.dat
mv t_ 20.00000E-03.dat t00009.dat
mv t_ 0.20000E-00.dat t00010.dat
mv t_ 0.20000E+01.dat t00011.dat
mv t_ 0.20000E+02.dat t00012.dat
mv t_ 0.20000E+03.dat t00013.dat

$

Instead of naming them t0, t1, t2, ... etc, which are names that don't sort easily, it names them t00000, t00001, t00002, etc.

Change 'echo mv' into 'mv' once you're sure it's doing what you want.

Last edited by Corona688; 12-05-2012 at 12:51 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 12-06-2012
Hi Corona688. That works! Thanks!
This User Gave Thanks to lost.identity For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

2. Shell Programming and Scripting

Rename a multiple files

I have multiple files in folder which i want to rename. hence I am using the below command in my script by I get an error: export XXX_LOG_DIR="${LOG_DIR}/${XXX_HOST}/xxx/${REPORT_DATE}" mv $XXX_LOG_DIR/*.audit.gz $XXX_LOG_DIR/*.audit.log.gz But I get the below error: mv: target... (5 Replies)
Discussion started by: karan8810
5 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Search, Extract and Rename Multiple Files

Hi, I want perl script for the below requirement. We have lot of files like below name in the directory 750464921-RE-file2.csv 750452173-RE-file1.csv 750385426-RE-file3.csv 750373470-RE-file4.csv And also we have another file as "Group.csv" in the same directory as per the below format... (9 Replies)
Discussion started by: armsaran
9 Replies

4. UNIX for Dummies Questions & Answers

Rename Multiple Files

Hey guys, I am the definition of a newbie. I am in the process of trying to rip all my dvds onto a new HTPC I setup. While doing this, I am also trying to organize a bunch of other files I already have to proper naming conventions. So far I have just been naming each file separately (I am on a... (4 Replies)
Discussion started by: Ralze34
4 Replies

5. Shell Programming and Scripting

Rename multiple files

hello: I have multiple files with names like: somestring_y2010m01d01 somestring_y2010m01d02 .......... somestring_y2010m12d31 How... (4 Replies)
Discussion started by: sylcam
4 Replies

6. Shell Programming and Scripting

Rename the multiple files

Hi I need to reanme the multiple file using unix script I have multiple file like: sample_YYYYMMDD.xls test new_YYYYMMDD.xls simple_YYYYMMDD.xls I need to rename this file sample.xls testnew.xls SIMPLE.xls thanks (8 Replies)
Discussion started by: murari83.ds
8 Replies

7. UNIX for Dummies Questions & Answers

help with multiple files rename...

Hi everyone, I'm very green in Linux. Please help me to solve my problem. I have thousands of files and I want to change their names. They have naming convection: prefix_date_date+1_suffix.nc prefix: ext-GLORY date_date+1: 20020101_20020102 and two types of suffix: gridV_R20020130 and... (3 Replies)
Discussion started by: makikicindy
3 Replies

8. UNIX for Dummies Questions & Answers

How to rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (4 Replies)
Discussion started by: a_dor8
4 Replies

9. Shell Programming and Scripting

rename multiple files

Hi all, I have some files like: pickup.0000043200.t001.t001.data pickup.0000043200.t001.t002.data pickup.0000043200.t002.t001.data pickup.0000043200.t002.t002.data pickup.0000043200.t003.t001.data pickup.0000043200.t003.t002.data I need to rename these files to ... (3 Replies)
Discussion started by: a_dor8
3 Replies

10. UNIX for Dummies Questions & Answers

Rename multiple files

Hello, I want to rename multiple files at a time and I don't know how to do it. I have various ".mp3" files, like "band name - music name.mp3" and I want to remove the "band name" from all files. Anybody knows how to do it using shell script or sed or even perl? Thanks (7 Replies)
Discussion started by: luiz_fer10
7 Replies
Login or Register to Ask a Question