I’m looping over an array of file directories. these files contain references to each other within the contents of each file. I need to match the filename based on the file reference to use later on. I’ve been so close for way too long and can’t get what I need to work because of scoping I think.
here is the code
for ref in $refs; do
value="<$ref>"
unset matchedFile
for i in $filesArray; do
if [[ $i == *$ref* ]]
then
$matchedFile=$i #This seems to only sometimes get properly set to use later.
echo Matched!!! $i
break
fi
done
#matchedFile= "${filesArray[@]}" | find $value I tried grep and find and I couldn't keep it from searching the file contents instead of just the file name.
#if [[ ${filesArray[@]} =~ $value ]] This worked but didn't return found value
if [ -z "${matchedFile}" ]
then
echo $matchedFile #Again only seems to sometimes get set.
else
echo "no match"
fi
done
I just need to find the filename of the matched file in the array given the partial path to the file.
so for example if I had the following files named similarly to my/long/file/path/to/my/file/file1.txt with different paths.
with contents
file2.txt
file4.txt
etc
filesArray=$(find . -type f -name "*.txt")
for file in $filsArray; do
regex=#a regex that would find the correct filenames in file contents.
refs=$(grep -Po $regex $file | cut -d '"' -f 2 | cut -d ',' -f 1)
for ref in $refs; do
# find the file name in array with path based on reference
done
done