Pattern Matching and extracting the required fields in Perl

Hi All,

I am writing the following Perl Scrip and need your help in Pattern matching :

I have the following Shell Script that would read line by line from the file (file_svn) and would inturn calls the Perl Script:

#!/bin/bash
perl_path="/home/dev/filter"
version_file_path="/home/dev/filter"
version_file="file_svn"
perl_file="SVN_Extract_Script_1.pl"

while read line
do
echo "Printing  $line"
$perl_path/$perl_file -v $line    --> Included options -v , actually not necessary
done < "/home/dev/filter/file_svn"

The file file_svn contains the following:

https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt -r860
https://ims-svnus.com/dev/DB/trunk/db/sec_db/stored_procedures/alter.sql -r990
https://ims-svnus.com/dev/DB/trunk/com/bin/aim/filter.java -r456
..so on 

From the above,
1) Get the path after trunk and then create those directories under the /home/dev/filter/build/tmpl/ and /home/dev/filter/build/ovrd/ if they dont exists.
(excluding -r860 ..etc)

Example:
After Running the Perl Script for first line:

https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt -r860

The directories (/feeds/templates/) doesn't exists under /home/dev/filter/build/tmpl/ and /home/dev/filter/build/ovrd/ , then we have to create them.

Then the file should be saved as
/home/dev/filter/build/tmpl/feeds/templates/shell_script.txt

Fyi -> svn export (firstline) <<path>> would actually saves the file under the given path.

The Perl Script is below:

#!/usr/bin/perl
use Getopt::Std;
getopts('v:', %opts) || die "Invalid options passed\n";  --> not sure why this is not working
$ver=$opts{"v"};
$build_home="/home/dev/filter/build/";
$root_dir="/home/dev/filter/";
$vob_dir="https://ims-svnus.com/dev/DB/trunk/";
$dest_tmpl_path="/home/dev/filter/build/tmpl/";
$dest_ovrd_path="/home/dev/filter/build/ovrd/";

unless ( $ver =~ /\.java$/ )  -- If the line contains other than .java 
{
    ($full_path, $version) = split(' ',$ver); 
$temp = reverse $full_path;
($file) = split(/\//, $temp);
        $file = reverse $file;
$full_path =~ /^${vob_dir}(.*)${file}$/;
       $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 );

`svn export $ver $dest_tmpl_path$file`; --> $file should contain the entire path after trunk including the filename

<<<< I will perform someother things here >>>>
}

IF << .java>>> in theline 
then 
Perform the same things as above 
But << I will not performthe other things that I am doing in above step >>>
END

Could someone please help me out in finishing the above perl script.

I would really appreciate your help and time.

Do you really need a bash script and perl script? I could assist with completing the task in purely bash if I better understood what needs to be done.

---------- Post updated at 02:53 PM ---------- Previous update was at 02:44 PM ----------

#!/bin/bash
prefix1=/home/dev/filter/build/tmpl/
prefix2=/home/dev/filter/build/ovrd/
file_svn='file_svn'

while read -r url _; do
        # skip java files
        [[ $url = *.java ]] && continue
        rel=${url#*/trunk/}
        relpath=${rel%/*}
        [[ -d ${prefix1}$relpath ]] || echo mkdir -p "${prefix1}$relpath"
        [[ -d ${prefix2}$relpath ]] || echo mkdir -p "${prefix2}$relpath"
        echo svn export "$url" "${prefix1}$rel"
done < "$file_svn"

Sample run:

mkdir -p /home/dev/filter/build/tmpl/feeds/templates
mkdir -p /home/dev/filter/build/ovrd/feeds/templates
svn export https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt /home/dev/filter/build/tmpl/feeds/templates/shell_script.txt
mkdir -p /home/dev/filter/build/tmpl/db/sec_db/stored_procedures
mkdir -p /home/dev/filter/build/ovrd/db/sec_db/stored_procedures
svn export https://ims-svnus.com/dev/DB/trunk/db/sec_db/stored_procedures/alter.sql /home/dev/filter/build/tmpl/db/sec_db/stored_procedures/alter.sql

[/code]

1 Like

Maybe something like this?

$
$ cat file_svn
https://ims-svnus.com/dev/DB/trunk/feeds/templates/shell_script.txt -r860
https://ims-svnus.com/dev/DB/trunk/db/sec_db/stored_procedures/alter.sql -r990
https://ims-svnus.com/dev/DB/trunk/com/bin/aim/filter.java -r456
$
$
$ cat -n svn_extract.pl
     1  #!/usr/bin/perl -w
     2  use File::Path qw(make_path);
     3  use strict;
     4
     5  my @base_dirs = qw (/home/dev/filter/build/tmpl /home/dev/filter/build/ovrd);
     6  my $version_file = "file_svn";
     7
     8  open (FH, "<", $version_file) or die "Can't open $version_file: $!";
     9  while (<FH>) {
    10    m|^.*/trunk/(.*)/(.*?) -r\d+$|;
    11    my ($dir_path, $file_name) = ($1, $2);
    12    foreach my $base (@base_dirs) {
    13      my $dir_to_test  = "$base/$dir_path";
    14      my $file_to_test = "$base/$dir_path/$file_name";
    15      if (! -d $dir_to_test) {
    16        print "Creating dir and file: $file_to_test\n";
    17        make_path ($dir_to_test);
    18        open (F, '>', $file_to_test);
    19        close (F);
    20      } elsif (! -e $file_to_test) {
    21        print "Creating file: $file_to_test\n";
    22        open (F, '>', $file_to_test);
    23        close (F);
    24      } else {
    25        print "File: $file_to_test exists!\n";
    26      }
    27    }
    28  }
    29  close (FH) or die "Can't close $version_file: $!";
    30
$
$

tyler_durden

1 Like