Solved

Need to split program and save under new names

Posted on 2013-11-15
8
240 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:Alex Campbell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:Alex Campbell
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:Alex Campbell
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:Alex Campbell
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:Alex Campbell
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 …
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…
Six Sigma Control Plans

617 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