Extracting the file name from the specified URL

Hello Everyone,

I am trying to write a shell script(or Perl Script) that would do the following:

I have a file that contains the following lines:
File:

https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt -r860
https://ims-svnus.com/dev/DB/trunk/feeds/perl/templates/perl_script.txt -r867
https://ims-svnus.com/dev/DB/trunk/cfg/temlpates/cfg_file.txt -r868
...so on

Script:

#!/bin/ksh
file=$1
path=/home/filter/
while read line
do
echo "Getting $line"
svn export $line /home/filter/feed/templates/<filename>   --> need to Extract the file name from the url
done < $file
exit

Here:
I need to extract the file name from the above file that contains the lines:
using perl or shell script:

https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt -r860
https://ims-svnus.com/dev/DB/trunk/feeds/perl/templates/perl_script.txt -r867
https://ims-svnus.com/dev/DB/trunk/cfg/temlpates/cfg_file.txt -r868
...so on

Example:

if s/templates/
<then extract the file name as:>
shell_script.txt
so that the script would save along with the file name in that directory. 

Could someone please help me out to extract the filename from the above url.

It would be greatly appreciated for your time and help.

cat FILE
https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt -r860
https://ims-svnus.com/dev/DB/trunk/feeds/perl/templates/perl_script.txt -r867
https://ims-svnus.com/dev/DB/trunk/cfg/temlpates/cfg_file.txt -r868

perl -lpe 's!.*/(.*)\s+.*!$1!' FILE
shell_script.txt
perl_script.txt
cfg_file.txt

The same with

sed 's!.*/\(.*\)  *.*!\1!' FILE
1 Like

Try:

while read url rev; do
  echo "${url##*/}"
done < "$file"
1 Like

You can use the basename command for the same purpose very conveniently.Here is its syntax:

basename <absolute pathname>

It will extract the filename from the absolute path.

Example:

$$ basename /home/user/abc/def/ghi/...../aa.txt
aa.txt

Thus the desired output is obtained.

1 Like

Thanks a lot for all your quick replies.This would really help me out.

Seems like my script is still to be performed:

In the file , I would have the following:

https://ims-svnus.com/dev/DB/trunk/bin/feeds/templates/shell_script.txt -r860
https://ims-svnus.com/dev/DB/trunk/bin/perl/templates/perl_script.txt -r867
https://ims-svnus.com/dev/DB/trunk/cfg/temlpates/cfg_file.txt -r868
...so on..

I will have the following directories already created:
/home/filter/prod/bin
/home/filter/prod/cfg

I need to read line by line from the above file and do the following:
1) Somehow split the line based on the word "Templates" and then store the filename(ex: shell_script.txt) into a variable.($filename)
2) The other part of the splitted line should be stored in another variable.($path)
3) $path = reverse $path -- to extract the directory feeds, cfg ($1) (The directory should be created if it doesn't exists. i.e. /home/filter/prod/bin/feeds
4) then svn export <$line> <save to /home/filter/prod/$path/$filename)
i.e. the extracted file should save under that directory.

Script

$ver=$opts{"v"};
$environment=$opts{"e"};

$vob_dir="https://ims-svnus.com/dev/DB/trunk/"
$dest_tmpl_path="/home/filter/prod/";
$dest_ovrd_path="/home/filter/prod/ovrd/";

($full_path, $version) = split('trunk', $ver);
        $temp = reverse $full_path;
 ($file) = split (/\//, $temp);
        $file = reverse $file;
$full_path =~ /^${vob_dir}(.*)${file}$/;  -- not sure whether this is correct
        $full_path = $1;
        $full_path =~ s/templates\///;
$dest_tmpl_path .= "$full_path";
        $dest_ovrd_path .= "$full_path";

        `mkdir -p $dest_tmpl_path` if (! -e $dest_tmpl_path );
        `mkdir -p $dest_ovrd_path` if (! -e $dest_ovrd_path );
}

Could someone please help out in this. Would really appreciate your help.

Try:

while read url rev
do 
  name=${url##*/templates/}
  base=${url##*/trunk/}
  dir=${base%%/templates/*}
  echo "$name  $dir"
done < "$file"

There's a small typo on the third line of your sample..

1 Like