Solved

Need to split program and save under new names

Posted on 2013-11-15
8
238 Views
Last Modified: 2013-11-15
I have 30 text files. Each file has multiple SQL scripts.
These are four examples from one of the files.  
One of the files is attached.

The section should be copied from 2.1 to the line before 2.2 and created
into a new file named 2-1.sql.  

The next section would be copied and pasted into a new file named 2-2.sql.

The sections have the same format within each file.  

[Section name] 2-1. Declaring Variables.
[Blank line]
[Script] DECLARE @AddressLine1 nvarchar(60) = 'Heiderplatz';
[Script] SELECT AddressID, AddressLine1
[Script] FROM Person.Address
[Script] WHERE AddressLine1 LIKE '%' + @AddressLine1 + '%';
[Blank line]
[Blank line]
[Next section name] 2-2. Retrieving a Value Into A Variable


2-1. Declaring Variables.

DECLARE @AddressLine1 nvarchar(60) = 'Heiderplatz';
SELECT AddressID, AddressLine1
FROM Person.Address
WHERE AddressLine1 LIKE '%' + @AddressLine1 + '%';


2-2. Retrieving a Value Into A Variable

DECLARE @AddressLine1 nvarchar(60);
DECLARE @AddressLine2 nvarchar(60);
SELECT @AddressLine1 = AddressLine1, @AddressLine2 = AddressLine2
FROM Person.Address
WHERE AddressID = 66;
SELECT @AddressLine1 AS Address1, @AddressLine2 AS Address2;


2-3. Writin an IF...THEN...ELSE Statement

DECLARE @QuerySelector int = 3;
IF @QuerySelector = 1
BEGIN
   SELECT TOP 3 ProductID, Name, Color
   FROM Production.Product
   WHERE Color = 'Silver'
   ORDER BY Name
END
   ELSE
BEGIN
   SELECT TOP 3 ProductID, Name, Color
   FROM Production.Product
   WHERE Color = 'Black'
   ORDER BY Name
END;


2-4. Writing a Simple CASE Expression

SELECT DepartmentID AS DeptID, Name, GroupName,
       CASE GroupName
          WHEN 'Research and Development' THEN 'Room A'
          WHEN 'Sales and Marketing' THEN 'Room B'
          WHEN 'Manufacturing' THEN 'Room C'
       ELSE 'Room D'
       END AS ConfRoom
FROM HumanResources.Department
ch01.sql
0
Comment
Question by:Alex972
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 39652624
#!/usr/bin/perl

my $file = $ARGV[0];

if(!defined($file)) {
    print "Specify file name on command line\n";
    exit 1;
}

open FILE, "<$file";

open OUT, ">/dev/null";

while(<FILE>) {
    if(/^(\d+\.\d+)\. /) {
        # Found new file 
        # Close previous file.
        close OUT;
        # print "Found section $1\n";
        open OUT, ">$1.sql";
        next;
    }
    print OUT $_;
}
close OUT;
close FILE;

Open in new window

0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39652628
BTW your description says the format is
x-y. <desc> 

Open in new window

but in the example attached the format is
x.y. <desc>

Open in new window


I used the latter. You'll need to change the regex to accommodate the former.

Or it could be changed to handle both I guess.
0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39652630
And it wasn't clear if you wanted to include the line with the description in the new files.
If yes, then remove the "next" statement
        next;
at line 21

#!/usr/bin/perl

my $file = $ARGV[0];

if(!defined($file)) {
    print "Specify file name on command line\n";
    exit 1;
}

open FILE, "<$file";

open OUT, ">/dev/null";

while(<FILE>) {
    if(/^(\d+\.\d+)\. /) {
        # Found new file 
        # Close previous file.
        close OUT;
        # print "Found section $1\n";
        open OUT, ">$1.sql";
    }
    print OUT $_;
}
close OUT;
close FILE;

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Alex972
ID: 39652739
Just to clarify:

Of the section name "2-1. Declaring Variables." only 2-1 would be used.

The section should be copied from 2.1 to the line before 2.2 and created
into a new file named 2-1.sql.  

The next section would be copied and pasted into a new file named 2-2.sql.
0
 
LVL 1

Author Comment

by:Alex972
ID: 39652763
You are correct. I was silly enough to assume that the format was the same in ch01.sql as in the following .sql files. It changed to - in ch02.slq.
0
 
LVL 1

Author Comment

by:Alex972
ID: 39652772
I will give you all the points since you did fix the problem very well.

However, I would like to know how to handle the formats in the other files where they DO use a "-". See attached.  Do you want me to submit a separate question?

They have two dashes,  a space then the section number.

And, I do like the idea of including the first line of the section with the description in the file.

How is that commented out in perl.

Most of these seem to use the following format:

-- 3-1. Replacing NULL with an Alternate Value
SELECT  h.SalesOrderID,
        h.CreditCardApprovalCode,
        CreditApprovalCode_Display = ISNULL(h.CreditCardApprovalCode,
                                            '**NO APPROVAL**')
FROM    Sales.SalesOrderHeader h ;

SELECT  ISNULL(CAST(NULL AS CHAR(10)), '20 characters*******') ;

SELECT  ISNULL(1, 'String Value') ;

-- 3-2. Returning the First Non-NULL Value from a List

SELECT  c.CustomerID,
        SalesPersonPhone = spp.PhoneNumber,
        CustomerPhone = pp.PhoneNumber,
        PhoneNumber = COALESCE(pp.PhoneNumber, spp.PhoneNumber, '**NO PHONE**')
FROM    Sales.Customer c
        LEFT OUTER JOIN Sales.Store s
            ON c.StoreID = s.BusinessEntityID
        LEFT OUTER JOIN Person.PersonPhone spp
            ON s.SalesPersonID = spp.BusinessEntityID
        LEFT OUTER JOIN Person.PersonPhone pp
            ON c.CustomerID = pp.BusinessEntityID
ORDER BY CustomerID ;

Open in new window

ch02.sql
ch03.sql
ch05.sql
0
 
LVL 1

Author Closing Comment

by:Alex972
ID: 39652774
Thanks for the quick solution.
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39652886
This should handle both number formats:
(Tweak regex and $1 -> $2 extracted match.)

#!/usr/bin/perl

my $file = $ARGV[0];

if(!defined($file)) {
    print "Specify file name on command line\n";
    exit 1;
}

open FILE, "<$file";

open OUT, ">/dev/null";

while(<FILE>) {
    if(/^(-- )?(\d+[-\.]\d+)\. /) {
        # Found new file 
        # Close previous file.
        close OUT;
        # print "Found section $2\n";
        open OUT, ">$2.sql";
    }
    print OUT $_;
}
close OUT;
close FILE;

Open in new window



And, I do like the idea of including the first line of the section with the description in the file.

How is that commented out in perl.


I included this after my first submission, so the subsequent two should have it.
That was achieved by removing the "next" statement, which begins the loop again and doesn't write the matched heading line. Removing it allows the  matched line to be written to the output file.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question