using sed to replace a string in files which are in different folders

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








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?







share|improve this question


















  • 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














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?







share|improve this question


















  • 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












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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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 to cd and use find here. Use sed directly with $ii/*.mvw as argument.
    – don_crissti
    Aug 7 at 19:26












  • 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







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










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 whole sed argument.

  • Using + instead of ; will cause find to execute one sed 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





share|improve this answer






















  • 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

















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.






share|improve this answer




















  • 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

















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.






share|improve this answer






















  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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 whole sed argument.

  • Using + instead of ; will cause find to execute one sed 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





share|improve this answer






















  • 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














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 whole sed argument.

  • Using + instead of ; will cause find to execute one sed 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





share|improve this answer






















  • 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












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 whole sed argument.

  • Using + instead of ; will cause find to execute one sed 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





share|improve this answer














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 whole sed argument.

  • Using + instead of ; will cause find to execute one sed 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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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










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.






share|improve this answer






















  • 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














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.






share|improve this answer






















  • 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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 8 at 6:44

























answered Aug 8 at 6:14









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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

pylint3 and pip3 broken

Missing snmpget and snmpwalk

How to enroll fingerprints to Ubuntu 17.10 with VFS491