Bash to update file on prefix match in two directories

I am trying to use bash to loop through a directory /path/to/data using a prefix match from /path/to/file . That match is obtained and works using the code below (in green)... what I can not seem to do is populate or update the corresponding prefix_file.txt in /path/to/data with the values in each of the data file in /path/to/data . The data file are always named the same way and have a single value in them.
I don't know if the portion in blue is helpful, but my thinking was on a prefix match each ${f}_file.txt is updated using the data file. Thank you :).

/path/to/data

ID-5_xx.number.txt    ID-5_xx.diff.txt
67722024              180

ID-5_xx.test.txt      ID-5_xx.common.txt
59733                 92.249581%

AA-1_yy.number.txt    AA-1_yy.diff.txt
46722024              138

AA-1_yy.test.txt      AA-1_yy.common.txt
50033                 82.248581%

current /path/to/file

ID-5_xx_file.txt
number=
test=
diff=
common=

AA-1_yy_file.txt
number=
test=
diff=
common=

desired /path/to/file

ID-5_xx_file.txt
number=67722024
test=59733
diff=180
common=92.249581%

AA-1_yy_file.txt
number=46722024
test=50033
diff=138
common=82.248581%

bash

for f in /path/to/data ; do  # strat process loop for each sample
  file=$(printf '%s' ${f}.*.txt)
     fname=`basename $file` # strip of path
       prefix=$(echo $fname | cut -d. -f1) # remove after .
   if [[ $f = $prefix ]] # only execute file prefix match

    cd /path/to/file # change directory to where ${f}_file.txt is
    then [[ ${f}_file.txt == # update each ${f}_file.txt with values from /path/to/data
       number=${sample}.number.txt
       test=$qc_dir/extract/${sample}.test.txt
       diff=$qc_dir/extract/${sample}.diff.txt
       common=${sample}.common.txt ]]
fi
done

How about getting your bash syntax right, first? What you post can't possibly run...

--- Post updated at 18:33 ---

OK, trying to guess what you're after, and making some assumptions, how about

TDIR="path/to/data"
for FN in ${TDIR}/*number*
  do    grep . ${FN%number*}* |
          awk ' {NFN = $0
                 sub (/\..*$/, "_file.txt", NFN)
                 sub (/^[^\.]*\./, "")
                 sub (/\.txt:/, "=")
                 print > NFN
                }'
   done




catfiles ${TDIR}/*file*
 
---------- data/AA-1_yy_file.txt: ----------

common=82.248581%
diff=138
number=46722024
test=50033

---------- data/ID-5_xx_file.txt: ----------

common=92.249581%
diff=180
number=67722024
test=59733

We assume a *number* file is always present, all files have a .txt suffix, and the order of entries being sorted by item is acceptable...

2 Likes

Thank you very much :).

That could even be condensed to

$ TDIR="path/to/data"
$ awk '
         {NFN = $0 = FILENAME ":" $0
         sub (/\..*$/, "_file.txt", NFN)
         sub (/^[^\.]*\./, "")
         sub (/\.txt:/, "=")
         print > NFN
        }' ${TDIR}/*

Be aware that for larger numbers of output files, you'd need to append to the files, and close them after each append.