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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern match a path anywhere in the line and replace it with new path
# 1  
Old 06-24-2015
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.
Code:
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?
# 2  
Old 06-24-2015
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:
Code:
pattern="/fldrA/fldrBaf/fldrCaa"
NewDir="/replacement/directory"
sed "s|$pattern/|$NewDir/|g" file

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 06-24-2015
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.
Code:
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?
Code:
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?.

Last edited by kchinnam; 06-24-2015 at 12:11 PM.. Reason: corrected content
# 4  
Old 06-24-2015
I wonder if this is what you are after.

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

This User Gave Thanks to Aia For This Post:
# 5  
Old 06-24-2015
Thanks Aia and Don
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace the path value in files, pattern is not full known.

Hi, I need to do find and replace, but the pattern is not full known. for example, my file has /proj/app-d1/sun or /data/site-d1/conf here app-d1 and site-d1 is not constant. It may be different in different files. common part is /proj/xx/sun and /data/xxx/conf i want to find where ever... (6 Replies)
Discussion started by: rbalaj16
6 Replies

2. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

4. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

7. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

8. Linux

Replace new line with path

Hi, The below is the output of multipath -ll |grep -i ddn | awk '{print $1}' | tr -s '\n' ',': /dev/mapper # multipath -ll |grep -i ddn | awk '{print $1}' | tr -s '\n' ','... (2 Replies)
Discussion started by: teav
2 Replies

9. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question