redirection and copying with same directory structure

Dear Experts,

    How can I solve this problem ?

I want to redirect with having the same directory structure as in my input.

for temp in `find ./CSV/ -name  "*.v"` 
do
  fname = `basename $temp`

./script.sh $temp  >  ./out/$fname 
      
done

But my problem here is all the *.v files are processed but get stored in the same directory.

I want to preserve the directory structure when redirecting to out directory here.

Ex-
Input file is inside ./CSV/GSM/ad0_conv.v
then after running the above script
it should be ./out/CSV/GSM/ad0_conv.v

Pls help...

Just stop using basename.
If you don't already have the directory structure created in ./out use mkdir -p 'dirname $temp` first.

Thanks Dragon for the reply and your time..

I did something like below:

for temp in `find ./CSV/ -name  "*.v"` 
do
   
     mkdir -p out/`dirname $temp` 
   ./transform.sh $temp  >  ./out/$temp
done

But Previously in this forum I found some related to do this like below

INDIR=./CSV
OUTDIR=./out

#copy only directories with "*cpp" filenames
find $INDIR -type f -name "*.file" -exec dirname {} \; |
  sed -e "s/^\($INDIR\)\(.*\)/$OUTDIR\2/g" | xargs mkdir -p

But I tried to implement xargs to achieve my goal but I am not suceded.
Can I do the same thing what I did in my code with sed and xargs.

Thanks in advance..

You certainly could use xargs but I think that second solution is a little overengineered.
If you are after a simpler commandline, try using the -exec option on find:
find ./CSV/ -name "*.v" -exec mkdir -p out/`dirname {}\;` -exec ./transform.sh {} > ./out/{} \;
(Untested)