Pattern Matching and extracting the required fields in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Matching and extracting the required fields in Perl
# 1  
Old 04-12-2012
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:

Code:
#!/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:
Code:
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:
Code:
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:
Code:
#!/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.
# 2  
Old 04-12-2012
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 ----------

Code:
#!/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:

Code:
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]
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 04-13-2012
Quote:
Originally Posted by filter
...
The file file_svn contains the following:
Code:
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:
Code:
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
...
...
Maybe something like this?

Code:
$
$ 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
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed pattern matching help required

Hi I am trying to pattern match with sed, if it finds a match I need it to insert characters at the 28th position, its working but its also adding an extra space and I don't know why, below is the code. sed 's/715023044/\n&/g' $asn | sed '/^71502304413-000/s/./B4/28' | sed -e :a -e... (4 Replies)
Discussion started by: firefox2k2
4 Replies

2. UNIX for Dummies Questions & Answers

Extracting sub-string matching the pattern.

Hi, I have a string looks like the following: USERS 32767.9844 UNDOTBS1 32767.9844 SYSAUX 32767.9844 SYSTEM 32767.9844 EMS 8192 EMS 8192 EMS_INDEXES 4096 EMS_INDEXES 4096 8 rows selected. How do I extract a sub-string to get the expected output as following: EMS 8192 EMS_INDEXES 4096 ... (3 Replies)
Discussion started by: NetBear
3 Replies

3. Shell Programming and Scripting

Korn Shell for pattern matching and extracting

Guys, i'm new to shell scripting. Here's what i need. I need a shell script which would read a file containing only 1 line which never changes. File containts - SQL_Mgd_Svc_ELONMCL54496 |EMEA\brookkev, EMEA\fieldgra, EMEA\tidmamar, EMEA\attfiste, EMEA\baldogar, EMEA\clarkia2, EMEA\conwasha,... (9 Replies)
Discussion started by: butterfly20
9 Replies

4. Shell Programming and Scripting

Extracting a string matching a pattern from a line

Hi All, I am pretty new to pattern matching and extraction using shell scripting. Could anyone please help me in extracting the word matching a pattern from a line in bash. Input Sample (can vary between any of the 3 samples below): 1) Adaptec SCSI RAID 5445 2) Adaptec SCSI 5445S RAID 3)... (8 Replies)
Discussion started by: jharish
8 Replies

5. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

6. Shell Programming and Scripting

Perl Pattern Matching

Hello experts, I have a file containing the following text(shortened here). File Begin ---------- < # Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 < Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 --- > # Billboard.d3fc1302a677.imagePath=S:\\efcm_Cassini >... (2 Replies)
Discussion started by: nmattam
2 Replies

7. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

8. Shell Programming and Scripting

Problem extracting just a part of a matching pattern

Hello everyone, this is my first post so please give me a hand. I apologize for my English, I'll try to be clear with my request. I need to write a script (Bash) which finds all the variables defined in the file .h of the folder and then writes the name of the files .c where these variables are... (1 Reply)
Discussion started by: paxilpaz
1 Replies

9. Shell Programming and Scripting

Pattern matching extracting urls from rss, shell scripts

Hi all, how could i do ? I have a Rss file, i want to extract only the Urls (many) matching http://www.xxx.com/trailers/ from that file and copy into another file. like " <pubDate>Wed, 29 Apr 2009 00:00:00 PST</pubDate> <content:encoded><!Apple - Movie Trailers - The Hangover"><img... (3 Replies)
Discussion started by: BremboloIV
3 Replies

10. Shell Programming and Scripting

perl pattern matching

hi i am trying to get digits inside brackes from file , whose structure is defined below CREATE TABLE TELM (SOC_NO CHAR (3) NOT NULL, TXN_AMOUNT NUMBER (17,3) SIGN_ON_TIME CHAR (8) TELLER_APP_LIMIT NUMBER (17,3) FIL01 ... (2 Replies)
Discussion started by: zedex
2 Replies
Login or Register to Ask a Question