Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a file and save the content to different files

Status
Not open for further replies.
Apr 30, 2003
56
US
I have a file. The file content looks something like this:

Solution: 112329
SITUATION IDENTIFIED IN:
"Print Order Documents" (tisfc0408m000)
SITUATION DESCRIPTION:
After solution 143000 on the 2nd form, all fields are empty when starting the session. That is not correct. The fields should be
filled with the values of the related parameters for all situations.
SOLUTION DESCRIPTION:
Script changed. Function re-introduced to fill the fields with the parameters.

Solution: 117204
SITUATION IDENTIFIED IN:
"All Archiving sessions"
SITUATION DESCRIPTION:
The archiving functionality assumes that table field names of text fields have a maximum length of 13 characters. This is not
true for customizations/localizations e.g.. This causes that these fields are not archived correctly.
SOLUTION DESCRIPTION:
Now the length of the field name of text fields is set to 15 characters, based on the maximum length of fields in the
Software Coding Standards.

What I need to do is to write a perl script that will read this file content and when it reads the solution number, it will save the content following the solution number until it finds the next solution number to a new file under solution_number.dat Is there a way to achieve this? I am pretty new to perl programming. Thanks.
 
Yes, there is a way to achieve this. What have you tried and what are you having problems with?
 
I'm sure there is an better/easier way of doing this, but here's my version of it.

while (<DATA>) {
if ($_ =~ /^Solution:/) {
($junk,$number) = split (/:/,$_);
chomp $number;
@fileName = join ".","$number","dat";

foreach $file(@fileName) {
open FILE,"> ./$file" or die "Cannot Open New File $file:($!)\n";
while ($data = <DATA>) {
last if ($data =~ /^\s+/);
print FILE "$data";
}
close FILE;
}
}
}

__DATA__
Solution: 112329
SITUATION IDENTIFIED IN:
"Print Order Documents" (tisfc0408m000)
SITUATION DESCRIPTION:
After solution 143000 on the 2nd form, all fields are empty when starting the session. That is not correct. The fields should be
filled with the values of the related parameters for all situations.
SOLUTION DESCRIPTION:
Script changed. Function re-introduced to fill the fields with the parameters.

Solution: 117204
SITUATION IDENTIFIED IN:
"All Archiving sessions"
SITUATION DESCRIPTION:
The archiving functionality assumes that table field names of text fields have a maximum length of 13 characters. This is not
true for customizations/localizations e.g.. This causes that these fields are not archived correctly.
SOLUTION DESCRIPTION:
Now the length of the field name of text fields is set to 15 characters, based on the maximum length of fields in the
Software Coding Standards.
 
Instead of thinking line-by-line, think of each block as a record.

Code:
[red]{[/red]
  [url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]$/[/blue] = [red]"[/red][purple][purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
  [olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
    [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]$number[/blue],[blue]$content[/blue][red])[/red] = [red]/[/red][purple]^Solution: ([purple][b]\d[/b][/purple]+)[purple][b]\n[/b][/purple](.*)[/purple][red]/[/red][red]s[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] FILE, [red]"[/red][purple]>[blue]$number[/blue].dat[/purple][red]"[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Cannot Open New File [blue]$file[/blue]:([blue]$![/blue])[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] FILE [blue]$content[/blue][red];[/red]
    [url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url] FILE[red];[/red]
  [red]}[/red]  
[red]}[/red]

The outer braces {} are important, because we have changed the record separator ($/) and we want that to be scoped to just this code and not to the rest of whatever your script is doing.
 
Really good replies lately brigmar, and that sure is some pretty perl code [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
lately" ?!?!?
/mock haughtiness

Really, I'm just an amateur at perl. I've actually been learning by 'having a go' at some replies, reading others, and with the help of perldocs, CPAN. I work with a whole different bunch of file formats here, and love the munging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top