如何使用一个perl脚本的文本输出作为另一个perl脚本的输入?我似乎能够将它作为两个单独的脚本运行,但不能作为一个脚本运行

时间:2015-04-21 09:12:14

标签: perl substr

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

我运行它的命令就是这个(BTW 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"

输出看起来像这样(文本的mods:删除引号,插入逗号,连接成一行,删除最后一个逗号)“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
}

1 个答案:

答案 0 :(得分:1)

所以我认为你真正缺少的是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