Look up file in list and write its path into another file

Hi

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

Can someone help please?

Cheers!

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.

Hmm...the filenames look the same except from their numeration:

testfile_1
testfile_2
testfile_3

So if I just want testfile_1 and testfile_3 I don't really know how to grep for these lines

---------- Post updated 07-30-11 at 09:04 AM ---------- Previous update was 07-29-11 at 12:54 PM ----------

Am still not any further. Is there noone who can help?

???

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

OR:

cat >list.file
testfile_1
testfile_3

touch testfile_1 testfile_2 testfile3

...

The results are the same though.

Hm...that doesn't seem to work for me...

When I do this:

cat >file
touch ./path/dir/*
for f in ./path/dir/* ; do
  if grep -qx "$f" file; then
    echo "$f"
  fi
done

nothing happens at all...

Oh... I wrote not exactly what you should do but just as an example to show that you can use "grep -qx ..." instead of "f in list".

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 :wink:

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