Pattern match a path anywhere in the line and replace it with new path

I want to pattern match only path part from below and replace them with new path string.

LoadModule jk_module /fldrA/fldrBaf/fldrCaa/modules/mod_jk.so

JkWorkersFile /fldrA/fldrBaf/fldrCaa/config/OHS/ohs1/workers.properties

JkLogFile /fldrA/fldrBaf/fldrCaa/diagnostics/logs/OHS/ohs1/mod_jk.log

paths(ex:/fldrA/fldrBaf/fldrCaa) from above will be dynamic. How do I do that?

Assuming that neither the path you want to replace nor the replacement path contain any characters that are special in a regular express AND that the path you want to replace is the some number of initial directories in pathnames to be replaced (not an entire pathname), the following simple sed script should work:

pattern="/fldrA/fldrBaf/fldrCaa"
NewDir="/replacement/directory"
sed "s|$pattern/|$NewDir/|g" file
1 Like

I am looking for selective pattern matching for path at any place in a line. I know how to replace it with a second path variable.
I do not know what paths come in source file, that is what I need to find..

This is my source file, where I need to find paths and replace them with new paths.

LoadModule jk_module /fldrA/fldrBaf/fldrCaa/modules/mod_jk.so
JkWorkersFile /fldrA/fldrBaf/fldrCaa/config/OHS/ohs1/workers.properties
JkLogFile /fldrA/fldrBaf/fldrCaa/diagnostics/logs/OHS/ohs1/mod_jk.log

Outline of my sed looks like this.. what goes into patch pattern matching here?

sed '/^LoadModule jk_module/ s/<path match>/$newpathA/'
sed '/^JkWorkersFile/ s/<path match>/$newpathB/'
sed '/^JkLogFile/ s/<path match>/$newpathC/'

We generally do this kind of selective pattern matching with IP address or eMail can we do it for path string?.

I wonder if this is what you are after.

sed "s|\(^LoadModule jk_module \).*$|\1$newpathA|"
sed "s|\(^JkWorkersFile \).*$|\1$newpathB|"
sed "s|\(^JkLogFile \).*$|\1$newpathC|"
1 Like

Thanks Aia and Don