Compare and rename files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare and rename files
# 1  
Old 06-30-2011
Compare and rename files

Hi.

I have a bunch of wav files and corresponding files that contain the transcription of these wav files.

The names of the transcription files have the format

chp_testfile_00001_0.lab
chp_testfile_00004_0.lab

or

chp_testfile_00002_1.lab
chp_testfile_00003_1.lab

The names of the wav files have the format

chp_testfile_00001.wav
chp_testfile_00002.wav
chp_testfile_00003.wav
chp_testfile_00004-wav

Now, the names of the wav files have to look like the names of the lab files. Is there a way I can compare the lab files with the wav files, and if a lab file is in the format

chp_testfile_00001_0.lab

I can automatically rename the wav file chp_testfile_00001.wav as

chp_testfile_00001_0.wav ?

I hope that didn't sound too complicated.

Thanks in advance for any tips!

Kat
# 2  
Old 06-30-2011
Code:
skrynesaver@busybox:~/test$ ls
chp_testfile_00001_0.lab  chp_testfile_00002_1.lab  chp_testfile_00003_1.lab  chp_testfile_00004_0.lab
chp_testfile_00001.wav    chp_testfile_00002.wav    chp_testfile_00003.wav    chp_testfile_00004.wav
skrynesaver@busybox:~/test$ ls | perl -e 'while(<>){($base,$ext)=$_=~/^(chp_testfile_\d+)_(\d+).lab$/ if (/\.lab$/);rename("$base.wav", "${base}_$ext.wav") if (-e "$base.wav");}'
skrynesaver@busybox:~/test$ ls
chp_testfile_00001_0.lab  chp_testfile_00002_1.lab  chp_testfile_00003_1.lab  chp_testfile_00004_0.lab
chp_testfile_00001_0.wav  chp_testfile_00002_1.wav  chp_testfile_00003_1.wav  chp_testfile_00004_0.wav

# 3  
Old 06-30-2011
With shell:

Code:
for f in *lab; do
  [ -f "${f%.*}".wav ] || {
    set -- ${f%${f#??????????????????}}*wav
    [ -f "$1" ] && mv -- "$1" "${f%.*}".wav
    }
done

If you have more than one file beginning with same pattern, only the first one will be renamed.

The ?????????????????? represent your pattern: chp_testfile_nnnnn, so you may need to adapt its length in order to match your real file names.
This User Gave Thanks to radoulov For This Post:
# 4  
Old 07-01-2011
Thanks! I now have the problem that my lab files and my wav files are in different directories.

When I'm in the lab directory, the wav files are in .. ./wavs/audio

I tried several things, but couldn't quite figure out where to put the path!
# 5  
Old 07-01-2011
Code:
_audio_dir=../wavs/audio/

for f in *lab; do
  [ -f "$_audio_dir/${f%.*}".wav ] || {
    set -- "$_audio_dir/${f%${f#??????????????????}}"*wav
    [ -f "$1" ] && mv -- "$1" "$_audio_dir/${f%.*}".wav
    }
done

# 6  
Old 07-01-2011
Sorry, I'm feeling completely stupid now.

I am in the lab directory, pasted the code into the terminal and the wav files didn't change...they have the same format and the number of ??? is correct.

Is there anything else I have to do?
# 7  
Old 07-01-2011
Did you set the _audio_dir?

Code:
_audio_dir=../wavs/audio/

Could you post part of the output of the following command, executed from the lab dir:

Code:
ls -l ../wavs/audio/

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 files

I am getting some files with this mask: aaaa_SP_bbb.txt aaaa_FX_bbbb.txt aaaabbbb.txt I want to rename the files containing: _SP_ -> Rename - >> fileSP.TXT _FX_ -> Rename - >> fileFX.txt and other file that does not contain these words rename them: filenamexx.txt as I... (4 Replies)
Discussion started by: Jomeaide
4 Replies

3. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

4. UNIX for Dummies Questions & Answers

Rename all .sh files to .pl

I have various .sh and .pl files in one directory. I want to rename all the .sh files to .pl i.e testscript.sh --> testscript.pl I am trying to use mv *.sh *.pl It doesnt work though!! (3 Replies)
Discussion started by: chrisjones
3 Replies

5. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

6. Shell Programming and Scripting

Rename files

Hi, I am new to Unix and i have a requirement where i need to write a shell script where i have to loop through various files present in a directory and rename them based on below criteria. Files in the folder are in the following format. _YYYYMMDD.dat] SDL_V1_20100530.dat... (6 Replies)
Discussion started by: bishoo
6 Replies

7. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

8. Shell Programming and Scripting

Rename files

Hi, I wanna rename bunch of files which has ":" to -. ie. rename file named file1:file1 to file1-file1. any ideas? (2 Replies)
Discussion started by: linuxaddict7
2 Replies

9. Solaris

rename many files

how to rename many files with one command package_cypdba.acapx_p.sql package_cypdba.ccapx_p.sql package_cypdba.bcapx_p.sql these to be renamed as acapx_p.sql ccapx_p.sql bcapx_p.sql thanx (4 Replies)
Discussion started by: fsmadi
4 Replies

10. UNIX for Dummies Questions & Answers

How to rename files?

:confused: How can i rename a file 'x.log' to 'x_20020512 072909.log' :eek: i'm using perl, with system command from a unix web server, and need to timestamp my logs if the above format (filename _ year month day hr min sec .log) (9 Replies)
Discussion started by: CompuTelSystem
9 Replies
Login or Register to Ask a Question