If you have a directory structure with only a number of subdirectories, each having this .mvw
file that you'd like to modify, then there is no need to use find
because you already know exactly where your files are.
for pathname in XYZ_DV_L02*/*.mvw; do
sed -i "s/D3PLOT_1DForce/${pathname%%/*}/g" "$pathname"
done
This iterates over all .mvw
files in the indicated subdirectories and replaces the string with the directory name. The parameter expansion ${pathname%%/*}
deletes everything after the first /
in the string in $pathname
.
Testing:
$ tree
.
|-- XYZ_DV_L02_P01
| `-- somefile.mvw
|-- XYZ_DV_L02_P02
| `-- somefile.mvw
`-- XYZ_DV_L02_P03
`-- somefile.mvw
3 directories, 3 files
$ cat XYZ_DV_L02_P0*/*.mvw
Hello D3PLOT_1DForce_EffPlStrainMax_VonMisesMax!
Hello D3PLOT_1DForce_EffPlStrainMax_VonMisesMax!
Hello D3PLOT_1DForce_EffPlStrainMax_VonMisesMax!
(running loop here)
$ cat XYZ_DV_L02_P0*/*.mvw
Hello XYZ_DV_L02_P01_EffPlStrainMax_VonMisesMax!
Hello XYZ_DV_L02_P02_EffPlStrainMax_VonMisesMax!
Hello XYZ_DV_L02_P03_EffPlStrainMax_VonMisesMax!
Obviously, if you have a deep hierarchy, then you may need to use find
anyway, but you can do the looping from within find
rather than outside of find
:
find XYZ_DV_L02*/ -type f -name '*.mvw' -exec sh -c '
for pathname do
sed -i "s/D3PLOT_1DForce/${pathname%%/*}/g" "$pathname"
done' sh {} +
Here, we find all the .mvw
files beneath any of the directories matching the pattern XYZ_DV_L02*/
. For these found files, we run a loop that does the same thing as the loop at the start of this answer. This would replace the string D3PLOT_1DForce
in all the .mvw
files anywhere beneath any of the directories with the corresponding XYZ_DV_L02*
directory name.
Related:
This answer assumes GNU sed
or some other sed
implementation that does in-place editing with -i
without an option-argument.
cd
and usefind
here. Usesed
directly with${ii}/*.mvw
as argument. – don_crissti Aug 07 '18 at 19:26