Solved

Need USE's found and added in following text

Posted on 2013-11-24
9
246 Views
Last Modified: 2013-12-04
In the attached file, each section needs a USE statement that is picked up from the previous section. It will change through the different sections.

Original on Left, Desired on Right

Before and Aftersqlrecipes-original.txt
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
  • 5
  • 4
9 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39673714
s/(\/\*Sec.*?\/)/$1\nUSE AdventureWorks2008R2;\n/g;

Not tested with PERL (I do not have it here) but it works using replace with regex in Notepad++.

It will use AdventureWorks2008R2 for all sections. If that was not the intending result, try
s/(\/\*Sec-01.*?\/)/$1\nUSE AdventureWorks2008R2;\n/g; and it will only work for section 1.

HTH,
Dan
0
 
LVL 1

Author Comment

by:Alex Campbell
ID: 39674359
Thanks

Adventureworks2008r2 is just one of them.
Most of the sections are Adventureworks2012, but there are several others.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39674391
OK, I don't see in your example file where the "USE" statement is picked up from, so I guess you have somewhere a list and will modify the regex either programatically inside a loop or by hand (30 sections is not that much - probably faster by hand than writing the code)
0
Technology Partners: 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!

 
LVL 1

Author Comment

by:Alex Campbell
ID: 39674990
There actually 380 sections.  
Whenever you see the word 'use' a new database is being used. It can change several times within a section.
There are 30 chapters. The first number is the chapter, the second number is the exercise within the chapter.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39676682
Not very familiar with PERL, so here how to do it in Powershell:

$originalName="\path\to\original\file"
$copyName="\path\to\copy\file"

$use = ""
echo "" > $copyName #empty copy file
foreach ($line in get-content $originalName) {
  echo $line >> $copyName
  if ($regex = $line -match "\/\*Sec.*?\/") {echo `n`n$use`n >> $copyName}
  elseif ($regex = $line -match "^USE.*?;") {$use = $line}
}


It will do a line by line search of $originalName and copy the result to $copyName
- update $use for every line with "USE" at the beginning
- append $use to every line that has a section in it
- send every other line unchanged

HTH,
Dan
0
 
LVL 1

Accepted Solution

by:
Alex Campbell earned 0 total points
ID: 39685951
I didn't receive a response for a perl solution.
I imported the code into Excel after adding line numbers and used a formula that looked at the lines. I sorted the lines so that only lines with USE or Section headers were grouped together.
If the line had USE, then it picked up the new one.
If the line did NOT have USE, then it used the one from the previous line.

Formula to Repeat USE Statements
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39686090
OK, so you chose to ignore a working solution (tested on your sample text) and created your own.

Whatever works for you, I guess.
0
 
LVL 1

Author Comment

by:Alex Campbell
ID: 39686159
"OK, so you chose to ignore a working solution (tested on your sample text) and created your own.

Whatever works for you, I guess. "

I was not able to use the solution in powershell at this time..
0
 
LVL 1

Author Closing Comment

by:Alex Campbell
ID: 39695010
In the instructions to delete my question, it didn't mention how to handle the points.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

726 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