0

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?

Ant
  • 753
  • 1
  • 9
  • 24

1 Answers1

0

I'm not sure on the etiquette for responding to one's own question, but thought I'd post a solution in case it's useful for other people.

I came across this question (Getting "Permission denied" on dirname and basename) and adapted the answer to replace my "l=" line:

l=$(basename parent/$daughter/data/*.dat)

It solved my problem. I get the name of the file and can now put it in the input file.

Ant
  • 753
  • 1
  • 9
  • 24