Data extraction in perl variable

HI,

i have variable in perl like below

 
$tmp="/home/sai/automation/work/TFP_GN.txt"
 
it can conatain any path its filled from config file.
 
now i want extarct the path upto this /home/sai/automation/work/    and put it in another variable
 
say $newtmp=="/home/sai/automation/work/"

i mean last any file name like TFP_GN.txt or dummy.csc etc should be removed and want only directory path like /home/sai/automation/work/

Any thought ?

Thanks,
Raghav

File::Spec - perldoc.perl.org

check rel2abs

You could do it with a regex

($path,$file)=$tmp=~/(\/[^\/]+\/)+([^\/]+)/;

or use a module which will catch cases you haven't thought of

use File::Basename;
$newtmp= dirname($tmp);

Thank you :slight_smile:

Use the regex below.

echo "/home/sai/automation/work/TFP_GN.txt" | perl -pe 's/\w+\.txt//g'
/home/sai/automation/work/