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 /proj/xxx/sun or /data/xxx/conf present and replace it with the path value i desire which i have constant values. please suggest how to do this.

Below is the one line which has the patterns to find, i want to replace it with differnt path. here cesite1-d1 is not constant, it may be different in other files.

if [ -f /data/cesite1-d1/conf/source.txt]; then . /data/cesite1-d1/conf/source.txt; fi

Would

ls -d data/*/conf | while read PN; do [ -f $PN/source.txt ] && . $PN/source.txt; done

(untested) do what you need? If not, supply more details.

Let me explain in different way.

i have two files, a.txt , b.txt

 more a.txt

root=/proj/app-d1/sun
data=/data/site-d1/conf

more b.txt

root=/proj/app-d2/sun
data=/data/index-i1/conf

now after find and replace, i need a.txt and b.txt to be like this.

more a.txt

root=/proj/app-i1/sun
data=/data/site-i1/conf

more b.txt

root=/proj/app-i1/sun
data=/data/site-i1/conf

though originally, a.txt and b.txt has different path values, after find and replace it is same.

And what would your code snippet in post#1 have done to those files a.txt and b.txt?

No it is not my code snippet, it is part of my a.txt. i just copied it from the file.sorry for the confusion. ignore that if statement code.

How far would this get you:

awk '
        {FN = FILENAME ".TMP"
         if (FN != FNO) close (FNO)
         FNO = FN
         sub ("/proj/[^/]*/sun", "/proj/app-i1/sun")
         sub ("/data/[^/]*/conf", "/data/site-i1/conf")
         print  >  FN
        }
' *.txt
for FN in *.TMP; do mv $FN ${FN%.TMP}; done
1 Like

Hi RudiC,

This works. Thanks a ton.