I have a parent directory that contains a succession of daughter directories called:
1
2
3
4 # <-- This one is used in an example below.
...
Each daughter directory contains a file called input
and a grandchild directory called data
. Within "data" is a file named by multiplying the daughter directory name by 1000. For example:
Parent directory / daughter directory #4 / input
Parent directory / daughter directory #4 / data / 4000.dat
Here is what I need to do:
Within each input
file, replace the term "old_file_name" with the name of the file in the corresponding data
directory. So in the example above, I need to find the term "old_file_name" and replace it with "4000.dat."
Here is what I have tried, with notes on what I want to be doing to each input
file:
find_replace_old_file_name="old_file_name"
i=$((daughter)); # This is the directories called "1," "2," "3," etc.
daughter2=$((1000*daughter)) # Multiply by 1000 to get correct filename in /data.
l=$(sed "${i}q;d" parent/$daughter/data/$daughter2.dat) # Get filename of file in /data.
echo ${l%}
find parent/${i} -type f -exec sed -i -e "s/${find_replace_old_file_name}/${l}/g" {} +
I am doing something wrong at the "l=" step because in each input
file, the term old_file_name
is being replaced with the contents of the .dat files, not their names. So in the example above, I want 4000.dat
to replace old_file_name
in the corresponding input
file, but instead I'm getting the contents of 4000.dat.
Does anyone know what I'm doing wrong?