Friends,
I'm trying to generate dynamic sql script and replace string from the original query result.
Original: '/u02/DB/FILE/file01.dbf' or
Original: '+DATA/DB/FILE/file01.dbf'
Required : '/u01/data/dbname/file01.dbf'
- Start search from 2nd character from original, find 1st and 2nd appearance of '/' and replace with data (hopefully this covers both original scenario)
- Start search from 2nd character from original, find 2nd and 3rd appearance of '/' and replace with dbname
DB and FILE in original could be any location so can't really hardcode that.
I use below to replace FILE with dbname but that's not helping totally,
Somehow feels approach is too complex and there should be better/diferent way.
SELECT 'alter database rename file ''' || f.file_name || ''' to '''|| '/u01' ||
REGEXP_REPLACE(SUBSTR(f.file_name, INSTR(f.file_name, '/',2)), '/FILE/', '/dbname/', 1||''';' stmt
FROM ( SELECT name file_name, bytes
FROM v$tempfile
ORDER BY bytes ESC
)f;
Thanks and much appreciated.
regexp_replace('/u02/DB/FILE/file01.dbf', '^([^/]*/[^/]*/)[^/]*/[^/]*', '\1data/dbname').^to match the start of string, then[^/]*to match 0+ chars other than/and then/- that will be the first/, then add[^/]*to get to the 2nd/.