0

I have a Perl script (I didn't write it) that takes a POST from an html page and it displays a certain section of a txt file to a webpage. The problem is, now I need it to also make a text file of that section to a text file on our Unix server. Any help? Code below.

#!/usr/bin/perl
#
print "Content-type: text/html\n\n";
print '<pre>';
read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
#print "$buf\n";
#print "$REMOTE_USER = $REMOTE_USER\n";
@pairs = split(/&/, $buf);
#print "$pairs\n";
($txt_HRcode, $lc_HRcode) = split(/=/,$pairs[0]);
#print "$txt_HRcode\n";

#$HRcode = " HRcode:  E2PSYAA0";
$HRcode = " HRcode:  F8".uc($lc_HRcode)."0";
#print "$HRcode\n"; 

open(LINEFIND, "grep -n \"$HRcode\" /release/ucpmr/ucpmr.txt |") or die print "Can't Open File" ;
$line_num = <LINEFIND>;
#print "$line_num\n";
#if($line_num !~ m/$HRcode/) {print "SEQUENCE CODE NOT FOUND"; die()};
($sline, $hrd, $lin_text) = split(/:/, $line_num);
$beg_line = ($sline - 2);
$end_line = ($beg_line + 10000);
#print "$beg_line\n";
#print "$end_line\n";
close(LINEFIND);

open(DISP, "/release/ucpmr/ucpmr.txt") or die print "File is no longer in History. Press Back to Return";
for($incr=1; $incr <= $end_line; $incr +=1)
{$line = <DISP>;
    if($incr > $beg_line) {
    if($incr >$sline){ 
        if($line =~ m/HRcode:  F8/){
        if($line !~ m/$HRcode/) {$quit_line =  $incr-3 ; last;

close(DISP);}}}}}

open(PRINTFIND, "/release/ucpmr/ucpmr.txt") or die print "File is no longer in History. Press Back to Return";
for($incr=1; $incr <= $quit_line; $incr +=1)
{$line = <PRINTFIND>;
#$line =~ s/\d\d\d-\d\d-/XXX-XX-/;
    if($incr > $beg_line) {print"$line";}}
#print "quit line is : $quit_line\n";

print "</pre>";
2
  • 1
    Not very clear what you want to do. You want to save something to a text file? On the same server where this runs? In any case, this is an awful script. You're better off rewriting it nicely in your favorite language. Commented Dec 11, 2013 at 19:05
  • Yeah, this is part of a "Other Duties as Assigned". And the programmer that wrote this is long gone. But basically, this displays a certain part of a txt file to a web page. I also need it to put it in a text file in this directory /release/ucpmr/. Any help would be greatly appreciated. Commented Dec 11, 2013 at 19:17

2 Answers 2

1

Change the end part, starting from open(PRINTFIND, ... like this:

open(PRINTFIND, "/release/ucpmr/ucpmr.txt") or die print "File is no longer in History. Press Back to Return";
open(my $fh, '>/release/ucpmr/TEXT_FILE_NAME.txt');
for($incr=1; $incr <= $quit_line; $incr +=1)
{$line = <PRINTFIND>;
#$line =~ s/\d\d\d-\d\d-/XXX-XX-/;
    if($incr > $beg_line) {print"$line"; print $fh $line; }}
#print "quit line is : $quit_line\n";

print "</pre>";

... but dude, if you're a .NET guy, do yourself a favor and rewrite this mess in .NET, seriously...

Sign up to request clarification or add additional context in comments.

2 Comments

I am getting GLOB(0x4002662c) errors and nothing getting written to file? Not sure what is going on.
@user3092474 ah I made a mistake. There should be no comma in print $fh $line; there.
0
open(my $fh, '>', '/release/ucpmr/TEXT_FILE_NAME.txt');
print $fh FILE_CONTENT;
close $fh;

like this?

2 Comments

I'm a .NET guy so please bare with me, do I put this at the end of the script?
yes put it at the end of the script. replace TEXT_FILE_NAME with the name of the text file you wish to output, and replace FILE_CONTENT with whatever variable or content you wish to print to the file. 3rd line closes the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.