2

I need a bash script for use in the Linux terminal which should go something like:

#!/bin/bash 

for textgrid_file in ./*.TextGrid and for wav_file in ./*.wav
do 
   praat --run pitch.praat "$textgrid_file" "$wav_file" >> output.txt
done

I.e. I need to iterate through pairs of files with extensions .textgrid and .wav because in my Praat script pitch.praat I have two arguments to pass. How can I implement it via bash scripting?

Inian
  • 80,270
  • 14
  • 142
  • 161
dgr379
  • 353
  • 3
  • 13
  • Iterate over one or the other and swap the extension. – Mad Physicist Mar 05 '18 at 05:28
  • Possible duplicate of [Matching files with various extensions using for loop](https://stackoverflow.com/q/6223817/608639), [for loop for multiple extension and do something with each file](https://stackoverflow.com/q/12259331/608639), etc. – jww Aug 19 '18 at 06:43

2 Answers2

2

You can use an array support to iterate over first glob pattern and use 2nd file from array:

waves=(*.wav)

k=0
for textgrid_file in *.TextGrid; do
    praat --run pitch.praat "$textgrid_file" "${waves[k++]}" >> output.txt
done
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

If I understand you right, both filename share the same basename.

#!/bin/bash 
for textgrid_file in ./*.TextGrid 
do 
    name=$(basename $textgrid_file .TextGrid)
    wfile="$name.wav"
    praat --run pitch.praat "$textgrid_file" "$wfile" >> output.txt
done

Extract the common basename with basename.

user unknown
  • 35,537
  • 11
  • 75
  • 121