如何使用一个 perl 脚本的文本输出作为另一个 perl 脚本的输入?我似乎可以 运行 这作为两个独立的脚本,但不能作为一个

How to use text output from one perl script as input to another perl script? I seem to be able run this as two separate scripts but not as one

我有一个脚本可以重新格式化输入文件并创建输出文件。当我尝试读取脚本第二部分的输出文件时,它不起作用。但是,如果我将脚本分成两部分,它可以正常工作并提供我需要的输出。我不是程序员,我很惊讶我能走到这一步 - 我几天来一直在努力解决这个问题。

我对 运行 的命令是这样的(顺便说一句,temp.txt 只是一种强力解决方法,用于摆脱最后的逗号以获得​​我的最终输出文件 - 找不到其他解决方案):

c:\perl\bin\perl merge.pl F146.sel temp.txt F146H.txt

输入看起来像这样(来自另一个软件包)("F146.sel"):

/ Selected holes from the .\Mag_F146_Trimmed.gdb database.
"L12260"
"L12270"
"L12280"
"L12290"

输出看起来像这样(修改文本:删除引号,插入逗号,连接成一行,删除最后一个逗号)"F146H.txt":

L12260,L12270,L12280,L12290 

然后我想在脚本的下一部分中使用它作为输入,它基本上将这个输出插入到我可以在另一个软件包(我的 "merge.gs" 文件)中使用的一行代码中。如果我将我的脚本分成两部分,这是我得到的输出,但如果我将它作为一个部分来做,它只会给我一个空白(见下文)。

CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="L12260,L12270,L12280,L12290"
GX             mergline.gx

接下来是我的"merge.pl"。我做错了什么?

(实际上,问题可能是 - 我没有做错什么,因为这可能是您一段时间以来看到的最迟钝的代码。事实上,我敢打赌你们中的一些人可以完成整个操作用 10-15 行代码完成,而不是我的 90 行代码。提前致谢。)

# this reformats the SEL file to remove the first line and replace the " with nothing

$file = shift ;
$temp = shift ;
$linesH = shift ;

#open (Profiles, ">.\scripts\P2.gs")||die "couldn't open output .gs file";

open my $in,  '<', $file      or die "Can't read old file: Inappropriate I/O control operation";
open my $out, '>', $temp or die "Can't write new file: Inappropriate I/O control operation"; 

my $firstLine = 1;                                                                               

while( <$in> )                                                                                   
{                                                                                                
    if($firstLine)                                                                               
    {                                                                                            
        $firstLine = 0;                                                                          
    }                                                                                            
    else{                                                                                        
   s/"L/L/g; # replace "L with L                                                                 
   s/"/,/g; # replace " with, 
   s|\s+||; # concatenates it all into one line 
   print $out $_;  
    }
} 
close $out;


open (part1, "${temp}")||die "Couldn't open selection file";
open (part2, ">${linesH}")||die "Couldn't open selection file";
printitChomp();

sub printitChomp
{
print part2 <<ENDGS;
ENDGS
}

while ($temp = <part1> )
{
    print $temp;    
    printit();
}

sub printit 
{$string = substr (${temp}, 0,-1);
print part2 <<ENDGS;
$string
ENDGS
}                                                                                      

####Theoretically this creates the merge script from the output 
####file from the previous loop. However it only seems to work 
####if I split this into 2 perl scripts.
open (MergeScript, ">MergeScript.gs")||die "couldn't open output .gs file";
printitMerge();

open (SEL, "${linesH}")||die "Couldn't open selection file";  
sub printitMerge
#open .sel file                                                 

{
print MergeScript <<ENDGS;
ENDGS
}

#iterate over required files
while ( $line = <SEL> ){    
    chomp $line;            
    print STDOUT $line;     
    printitLines();              

}                           

sub printitLines 
{
print MergeScript <<ENDGS;
CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="${line}"
GX             mergline.gx

ENDGS
}

所以我认为你真正缺少的只是close(part2);允许它作为 SEL 重新打开..

#!/usr/bin/env perl

use strict;
use warnings;

# this reformats the SEL file to remove the first line and replace the " with nothing

my $file = shift;
my $temp = shift;
my $linesH = shift;

open my $in,  '<', $file      or die "Can't read old file: Inappropriate I/O control operation";
open my $out, '>', $temp or die "Can't write new file: Inappropriate I/O control operation"; 

my $firstLine = 1;                                                                               

while (my $line = <$in>){
  print "LINE: $line\n";
  if ($firstLine){                                                                                            
    $firstLine = 0;                                                                          
  } else {
    $line =~ s/"L/L/g; # replace "L with L
    $line =~ s/"/,/g;  # replace " with,
    $line =~ s/\s+//g; # concatenates it all into one line
    print $out $line;  
  }
} 
close $out;


open (part1, $temp) || die "Couldn't open selection file";
open (part2, ">", $linesH) || die "Couldn't open selection file";

while (my $temp_line = <part1>){
  print "TEMPLINE: $temp_line\n";    
  my $string = substr($temp_line, 0, -1);
  print part2 <<ENDGS;
$string
ENDGS
}

close(part2);

#### this creates the merge script from the output 
#### file from the previous loop.
open (MergeScript, ">MergeScript.gs")||die "couldn't open output .gs file";
open (SEL, $linesH) || die "Couldn't open selection file";  

#iterate over required files
while ( my $sel_line = <SEL> ){
  chomp $sel_line;            
  print STDOUT $sel_line;     
  print MergeScript <<"ENDGS";
CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="$sel_line"
GX             mergline.gx

ENDGS

}

还有一种替代方法..

#!/usr/bin/env perl
use strict;
use warnings;

my $file = shift;

open my $in,  '<', $file or die "Can't read old file: Inappropriate I/O control operation";
my @lines = <$in>;            # read in all the lines
shift @lines;                 # discard the first line
my $line = join(',', @lines); # join the lines with commas
$line =~ s/[\r\n"]+//g;       # remove the quotes and newlines

# print the line into the mergescript
open (MergeScript, ">MergeScript.gs")||die "couldn't open output .gs file";

print MergeScript <<"ENDGS";
CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="$line"
GX             mergline.gx

ENDGS