Merge all .wav files in a folder that start by the same prefix

1.3k views Asked by At

I have filelists that look like this:

S134_001.wav
S134_002.wav
S134_003.wav
S149_001.wav
S149_002.wav
S149_003.wav
S16_001.wav
S16_002.wav
S16_003.wav
S16_004.wav
S16_005.wav
S16_006.wav
S272_001.wav
S272_002.wav
S272_003.wav
S272_004.wav
S272_005.wav
S272_006.wav
S272_007.wav
S374_001.wav
S396_001.wav
S92_001.wav

And I want to merge S134_001.wav, S134_002.wav, S134_003.wav into S134.wav; S149_001.wav, S149_002.wav and S149_003.wav into S149.wav; and so on ( preferably using sox ).

I also want to delete the original files.

How do I achieve that? Bash is the preferred solution, but any programming language will do. Thanks.

1

There are 1 answers

10
Lars Fischer On

You can use a for loop like this:

for f in *_001.wav
do
    pre=${f%%_001.wav}
    # use either cat or sox or whatever works:
    #cat "${pre}_"*.wav > "${pre}.wav"
    sox -m "${pre}_"*.wav "${pre}.wav"
    #rm -rf "${pre}_"*.wav
done
  • Here we iterate over the *_001.wav files and
  • derive the prefix from the file
  • and then concat the files with the same prefix into a new file.
  • If everything works, you can remove the comment before the rm command.