using sed to replace a string in files which are in different folders
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
3
down vote
favorite
I have a file which is placed in different folders(with file extension .mvw). I want to write a script which goes to each folder and replace a string(in the file *.mvw) "D3PLOT_1DForce" with the name of the folders.
folder Structure is as below-
- XYZ_DV_L02_P01
- XYZ_DV_L02_P02
- XYZ_DV_L02_P03
I have written the following script-
#!/bin/sh
for ii in XYZ_DV_L02* ; do
(cd "$ii")
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/"$ii"/g" "" ;
done
It works and replaces the string with the name of the folder but it is replacing the name of the 1st folder in all the files. I think sed is not going in for loop.
How can I put the sed to work in the loop so that respective folder names are used to replace the string in the file?
linux shell-script sed find directory
add a comment |Â
up vote
3
down vote
favorite
I have a file which is placed in different folders(with file extension .mvw). I want to write a script which goes to each folder and replace a string(in the file *.mvw) "D3PLOT_1DForce" with the name of the folders.
folder Structure is as below-
- XYZ_DV_L02_P01
- XYZ_DV_L02_P02
- XYZ_DV_L02_P03
I have written the following script-
#!/bin/sh
for ii in XYZ_DV_L02* ; do
(cd "$ii")
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/"$ii"/g" "" ;
done
It works and replaces the string with the name of the folder but it is replacing the name of the 1st folder in all the files. I think sed is not going in for loop.
How can I put the sed to work in the loop so that respective folder names are used to replace the string in the file?
linux shell-script sed find directory
2
You don't need tocd
and usefind
here. Usesed
directly with$ii/*.mvw
as argument.
â don_crissti
Aug 7 at 19:26
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a file which is placed in different folders(with file extension .mvw). I want to write a script which goes to each folder and replace a string(in the file *.mvw) "D3PLOT_1DForce" with the name of the folders.
folder Structure is as below-
- XYZ_DV_L02_P01
- XYZ_DV_L02_P02
- XYZ_DV_L02_P03
I have written the following script-
#!/bin/sh
for ii in XYZ_DV_L02* ; do
(cd "$ii")
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/"$ii"/g" "" ;
done
It works and replaces the string with the name of the folder but it is replacing the name of the 1st folder in all the files. I think sed is not going in for loop.
How can I put the sed to work in the loop so that respective folder names are used to replace the string in the file?
linux shell-script sed find directory
I have a file which is placed in different folders(with file extension .mvw). I want to write a script which goes to each folder and replace a string(in the file *.mvw) "D3PLOT_1DForce" with the name of the folders.
folder Structure is as below-
- XYZ_DV_L02_P01
- XYZ_DV_L02_P02
- XYZ_DV_L02_P03
I have written the following script-
#!/bin/sh
for ii in XYZ_DV_L02* ; do
(cd "$ii")
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/"$ii"/g" "" ;
done
It works and replaces the string with the name of the folder but it is replacing the name of the 1st folder in all the files. I think sed is not going in for loop.
How can I put the sed to work in the loop so that respective folder names are used to replace the string in the file?
linux shell-script sed find directory
edited Aug 7 at 19:23
SivaPrasath
7,41443764
7,41443764
asked Aug 7 at 19:05
Pavan Raj Singh
161
161
2
You don't need tocd
and usefind
here. Usesed
directly with$ii/*.mvw
as argument.
â don_crissti
Aug 7 at 19:26
add a comment |Â
2
You don't need tocd
and usefind
here. Usesed
directly with$ii/*.mvw
as argument.
â don_crissti
Aug 7 at 19:26
2
2
You don't need to
cd
and use find
here. Use sed
directly with $ii/*.mvw
as argument.â don_crissti
Aug 7 at 19:26
You don't need to
cd
and use find
here. Use sed
directly with $ii/*.mvw
as argument.â don_crissti
Aug 7 at 19:26
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
The cd
command is executed in a sub shell and therefor effectively does nothing. So your find
command is executed from the parent directory, once for each of the sub directory names in XYZ_DV_L02*
. The first invocation of sed
changes the string in all files, the following invocations of sed
don't find the string but are performed anyway.
Try this:
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i "
s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/$ii/g" "" +
done
Thanks for the edit from Stéphane Chazelas
While I just copied the find
command from the question only fixing the path, it is worth explaining the changes:
$ii
was unquoted, now it is part of the quotes that contain the wholesed
argument.- Using
+
instead of;
will causefind
to execute onesed
command for multiple file, not one for each file.
Edit
To the request from the comment, this will replace every sequence of the set [-_a-zA-Z0-9]
followed by .h3d
with the replacement text, which is the filename including the .mvw
extension, which is how I understand the comment, but it doesn't seem useful. If you want to access the text in the pattern, you can use 1
. You may have to adapt the set for your particular needs.
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i -r "
s/([-_a-zA-Z0-9]+).h3d/$ii.h3d/g" "" +
done
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
add a comment |Â
up vote
3
down vote
Try this,
#!/bin/sh
for ii in XYZ_DV_L02* ; do
cd "$ii"
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce/"$ii"/g" "" ;
cd ..
done
It's fine to change the directory to the first input folder in for loop and finding the file. But we have to get back to the parent directory before running the next loop.
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
add a comment |Â
up vote
1
down vote
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:
- Understanding the -exec option of `find`
This answer assumes GNU sed
or some other sed
implementation that does in-place editing with -i
without an option-argument.
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The cd
command is executed in a sub shell and therefor effectively does nothing. So your find
command is executed from the parent directory, once for each of the sub directory names in XYZ_DV_L02*
. The first invocation of sed
changes the string in all files, the following invocations of sed
don't find the string but are performed anyway.
Try this:
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i "
s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/$ii/g" "" +
done
Thanks for the edit from Stéphane Chazelas
While I just copied the find
command from the question only fixing the path, it is worth explaining the changes:
$ii
was unquoted, now it is part of the quotes that contain the wholesed
argument.- Using
+
instead of;
will causefind
to execute onesed
command for multiple file, not one for each file.
Edit
To the request from the comment, this will replace every sequence of the set [-_a-zA-Z0-9]
followed by .h3d
with the replacement text, which is the filename including the .mvw
extension, which is how I understand the comment, but it doesn't seem useful. If you want to access the text in the pattern, you can use 1
. You may have to adapt the set for your particular needs.
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i -r "
s/([-_a-zA-Z0-9]+).h3d/$ii.h3d/g" "" +
done
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
add a comment |Â
up vote
4
down vote
The cd
command is executed in a sub shell and therefor effectively does nothing. So your find
command is executed from the parent directory, once for each of the sub directory names in XYZ_DV_L02*
. The first invocation of sed
changes the string in all files, the following invocations of sed
don't find the string but are performed anyway.
Try this:
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i "
s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/$ii/g" "" +
done
Thanks for the edit from Stéphane Chazelas
While I just copied the find
command from the question only fixing the path, it is worth explaining the changes:
$ii
was unquoted, now it is part of the quotes that contain the wholesed
argument.- Using
+
instead of;
will causefind
to execute onesed
command for multiple file, not one for each file.
Edit
To the request from the comment, this will replace every sequence of the set [-_a-zA-Z0-9]
followed by .h3d
with the replacement text, which is the filename including the .mvw
extension, which is how I understand the comment, but it doesn't seem useful. If you want to access the text in the pattern, you can use 1
. You may have to adapt the set for your particular needs.
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i -r "
s/([-_a-zA-Z0-9]+).h3d/$ii.h3d/g" "" +
done
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
add a comment |Â
up vote
4
down vote
up vote
4
down vote
The cd
command is executed in a sub shell and therefor effectively does nothing. So your find
command is executed from the parent directory, once for each of the sub directory names in XYZ_DV_L02*
. The first invocation of sed
changes the string in all files, the following invocations of sed
don't find the string but are performed anyway.
Try this:
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i "
s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/$ii/g" "" +
done
Thanks for the edit from Stéphane Chazelas
While I just copied the find
command from the question only fixing the path, it is worth explaining the changes:
$ii
was unquoted, now it is part of the quotes that contain the wholesed
argument.- Using
+
instead of;
will causefind
to execute onesed
command for multiple file, not one for each file.
Edit
To the request from the comment, this will replace every sequence of the set [-_a-zA-Z0-9]
followed by .h3d
with the replacement text, which is the filename including the .mvw
extension, which is how I understand the comment, but it doesn't seem useful. If you want to access the text in the pattern, you can use 1
. You may have to adapt the set for your particular needs.
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i -r "
s/([-_a-zA-Z0-9]+).h3d/$ii.h3d/g" "" +
done
The cd
command is executed in a sub shell and therefor effectively does nothing. So your find
command is executed from the parent directory, once for each of the sub directory names in XYZ_DV_L02*
. The first invocation of sed
changes the string in all files, the following invocations of sed
don't find the string but are performed anyway.
Try this:
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i "
s/D3PLOT_1DForce_EffPlStrainMax_VonMisesMax/$ii/g" "" +
done
Thanks for the edit from Stéphane Chazelas
While I just copied the find
command from the question only fixing the path, it is worth explaining the changes:
$ii
was unquoted, now it is part of the quotes that contain the wholesed
argument.- Using
+
instead of;
will causefind
to execute onesed
command for multiple file, not one for each file.
Edit
To the request from the comment, this will replace every sequence of the set [-_a-zA-Z0-9]
followed by .h3d
with the replacement text, which is the filename including the .mvw
extension, which is how I understand the comment, but it doesn't seem useful. If you want to access the text in the pattern, you can use 1
. You may have to adapt the set for your particular needs.
#!/bin/sh
for ii in XYZ_DV_L02* ; do
find "$ii" -iname "*.mvw" -type f -exec sed -i -r "
s/([-_a-zA-Z0-9]+).h3d/$ii.h3d/g" "" +
done
edited Aug 9 at 16:51
answered Aug 7 at 20:00
RalfFriedl
2,695419
2,695419
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
add a comment |Â
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be higly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:35
add a comment |Â
up vote
3
down vote
Try this,
#!/bin/sh
for ii in XYZ_DV_L02* ; do
cd "$ii"
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce/"$ii"/g" "" ;
cd ..
done
It's fine to change the directory to the first input folder in for loop and finding the file. But we have to get back to the parent directory before running the next loop.
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
add a comment |Â
up vote
3
down vote
Try this,
#!/bin/sh
for ii in XYZ_DV_L02* ; do
cd "$ii"
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce/"$ii"/g" "" ;
cd ..
done
It's fine to change the directory to the first input folder in for loop and finding the file. But we have to get back to the parent directory before running the next loop.
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Try this,
#!/bin/sh
for ii in XYZ_DV_L02* ; do
cd "$ii"
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce/"$ii"/g" "" ;
cd ..
done
It's fine to change the directory to the first input folder in for loop and finding the file. But we have to get back to the parent directory before running the next loop.
Try this,
#!/bin/sh
for ii in XYZ_DV_L02* ; do
cd "$ii"
find . -iname "*.mvw" -type f -exec sed -i "s/D3PLOT_1DForce/"$ii"/g" "" ;
cd ..
done
It's fine to change the directory to the first input folder in for loop and finding the file. But we have to get back to the parent directory before running the next loop.
answered Aug 7 at 19:22
SivaPrasath
7,41443764
7,41443764
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
add a comment |Â
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
It works well..! Thank you so much. Could you explain why you removed the brackets of the cd command?
â Pavan Raj Singh
Aug 9 at 7:39
add a comment |Â
up vote
1
down vote
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:
- Understanding the -exec option of `find`
This answer assumes GNU sed
or some other sed
implementation that does in-place editing with -i
without an option-argument.
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
add a comment |Â
up vote
1
down vote
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:
- Understanding the -exec option of `find`
This answer assumes GNU sed
or some other sed
implementation that does in-place editing with -i
without an option-argument.
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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:
- Understanding the -exec option of `find`
This answer assumes GNU sed
or some other sed
implementation that does in-place editing with -i
without an option-argument.
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:
- Understanding the -exec option of `find`
This answer assumes GNU sed
or some other sed
implementation that does in-place editing with -i
without an option-argument.
edited Aug 8 at 6:44
answered Aug 8 at 6:14
![](https://i.stack.imgur.com/scHTt.jpg?s=32&g=1)
![](https://i.stack.imgur.com/scHTt.jpg?s=32&g=1)
Kusalananda
103k13203321
103k13203321
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
add a comment |Â
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
Thank you so much...It works. The best part is that you have beautifully explained everything. I want further help to refine the script. I want to add a wildcard to pick the string to be replaced, meaning, I only want to write *.h3d instead of (D3PLOT_1DForce_EffPlStrainMax_VonMisesMax.h3d) and I want to add extension after the variable in sed like $ii.h3d. I want to replace everything in the file with .h3d extension.Your help will be highly appreciated ..!
â Pavan Raj Singh
Aug 9 at 7:47
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461144%2fusing-sed-to-replace-a-string-in-files-which-are-in-different-folders%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
You don't need to
cd
and usefind
here. Usesed
directly with$ii/*.mvw
as argument.â don_crissti
Aug 7 at 19:26