Solved

Need to split program and save under new names

Posted on 2013-11-15
8
237 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 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