I have a script that writes the path of files into a text file. I'd like to extend it so that it writes only the paths of certain files, which are given on a list, into this text file.
The initial code is as follows:
for file in `ls /path/*.lab` ; do
base=`basename $file .lab` ;
wavfile=/otherpath/$base.wav ;
if [ -e $wavfile ] ; then
echo $wavfile >> train.testvoice_11.wav ;
echo $file >> train.testvoice_11.lab ;
fi
done
I wanted to define the list at the top and then write a loop like
list=xy.txt
for file in `ls /path/*.lab` ; do
if $file in list ; do
echo $file >> train.testvoice_11.lab ;
done
done
wavfile=/otherpath/$base.wav ;
if [ -e $wavfile ] ; then
echo $wavfile >> train.testvoice_11.wav ;
fi
Just take the comparable parts of each file name and grep for supporting lines in the list file. Make sure your pattern has ^ and $ or -x so you do hot hit on prefix or suffix matches.
cat >list.file
testfile_1
testfile_2
testfile_3
touch testfile_1 testfile_3
for f in testfile*; do
if grep -qx "$f" list.file; then
echo "$f"
fi
done
testfile_1
testfile_3
Ah, alright! Will try to relate that to my own code then.
If someone else has a more specific idea how I could change my code so that it works, I would appreciate that. I'm relatively new to shell scripting and still have problems to think around corners
If it bogs down, remember that this sort of "join" is fastest as a sort-merge, where you sort both lists and then filter in one pass through both lists, like in days of yore with tape and tiny memory.
An intermediate choice is to put your list in string addressable variables, so you can use the shell to look each up.
while read file_root
do
mylist[$file_root]=1
done < $list_file
find ... -type f \ while read file_name
do
file_root=$[file_name##*/}
file_root=${file_root %%.*}
if [ "$mylist[$file_root]" = 1 ]
then
...
else
...
fi