Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find matching keywords in path (backward)

Status
Not open for further replies.

gsgb1

Programmer
Jul 31, 2004
21
CA
I want to move files from my source directory to target directory, and want to make sure that user is moving from designated directory, like from /dev/prj1 of source to /dev/prj1 of target.
How can I get last 2 directories /dev/prj1, considering directory path length may change. I want to start from back and grab last two. I want then to match last two dir pattern with target and if matching, then copy otherwise fail. Here is more info

I have source directory path as
src_path=/corp/thost/usr/deploy/dev/prj1
and my target path is as follows
tgt_path=/corp/thost/proj/prod/dev/prj1

And last two directories /dev/prj1 are changing for departments, and
 
Don't know how you're getting src_path and tgt_path, but once you've got them, split the 2 paths to arrays and compare the last 2 elements of the arrays.
Code:
BEGIN {
    OLD_FS = FS
    FS="/"
    srcn = split(src_path, s)
    tgtn = split(tgt_path, t)
    if (!(s[srcn] == t[tgtn] && s[srcn - 1] == t[tgtn - 1])) {
        print "Error in source/target directories"
        exit(1)
    }
    FS = OLD_FS
}

 

This script asks the user for the destination path and then makes sure that it meets your requirements.

[tt]
BEGIN{ src_path="/corp/thost/usr/deploy/dev/prj1"
printf "Destination? "
getline tgt_path
if ( end(tgt_path) != end(src_path) )
{ print "Destination must end with " end(src_path)
exit
}
com = "mv " src_path "/* " tgt_path "/"
system( com )
}
function end( s )
{ match( s, /\/[^\/]*\/[^\/]*$/ )
return substr( s, RSTART, RLENGTH )
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top