【问题标题】:how to combine the code to make the output is on the same line?如何结合代码使输出在同一行?
【发布时间】:2019-03-06 18:45:54
【问题描述】:

你能帮我把这两个程序结合起来,用两列连续显示输出吗?第一列为$1,第二列为$2

请帮我解决这个问题。谢谢你:)

这是我的代码 1。

#!/usr/local/bin/perl
#!/usr/bin/perl

use strict ;
use warnings ;
use IO::Uncompress::Gunzip qw(gunzip $GunzipError);

my $input = "par_disp_fabric.all_max_lowvcc_qor.rpt.gz";
my $output = "par_disp_fabric.all_max_lowvcc_qor.txt";


gunzip $input => $output
  or die "gunzip failed: $GunzipError\n";

open (FILE, '<',"$output") or die "Cannot open $output\n";

while (<FILE>) {
  my $line = $_;
  chomp ($line);

  if ($line=~ m/^\s+Timing Path Group \'(\S+)\'/) {
    $line = $1;
    print ("$1\n");
  }
}

close (FILE);

这是我的代码 2。

my $input = "par_disp_fabric.all_max_lowvcc_qor.rpt.gz";
my $output = "par_disp_fabric.all_max_lowvcc_qor.txt";

gunzip $input => $output
  or die "gunzip failed: $GunzipError\n";

open (FILE, '<',"$output") or die "Cannot open $output\n";

while (<FILE>) {
  my $line = $_;
  chomp ($line);

  if ($line=~ m/^\s+Levels of Logic:\s+(\S+)/) {
     $line = $1;
     print ("$1\n");
  }
}

close (FILE);

这是包含 26 行数据的代码 1 的输出:

**async_default**
**clock_gating_default**
Ddia_link_clk
Ddib_link_clk
Ddic_link_clk
Ddid_link_clk
FEEDTHROUGH
INPUTS
Lclk
OUTPUTS
VISA_HIP_visa_tcss_2000
ckpll_npk_npkclk
clstr_fscan_scanclk_pulsegen
clstr_fscan_scanclk_pulsegen_notdiv
clstr_fscan_scanclk_wavegen
idvfreqA
idvfreqB
psf5_primclk
sb_nondet4tclk
sb_nondetl2tclk
sb_nondett2lclk
sbclk_nondet
sbclk_sa_det
stfclk_scan
tap4tclk
tapclk

输出代码 1 也有相同的行数。

【问题讨论】:

  • 您能否分享示例输入文件和所需的输出以使您的问题更加清晰。
  • 你能确定两列高度一样吗?
  • 我又修正了你的缩进。请以后自己做。
  • 嗨@stack0114106。我的输出 code1 和 code2 有 26 行数据。我需要的输出是在输出code1旁边组合输出code2。这意味着最后一个输出有 2 列(o/p code1 和 o/p code2),共 26 行。很明显? tq
  • 嗨@HåkonHægland。是的,两列的高度相同。时长

标签: perl unix


【解决方案1】:

paste 对此很有用:假设你的 shell 是 bash,然后使用 process substitutions

paste <(perl script1.pl) <(perl script2.pl)

这会发出由制表符分隔的列。对于更漂亮的输出,您可以将 paste 的输出通过管道传输到 column

paste <(perl script1.pl) <(perl script2.pl) | column -t -s $'\t'

有了这个,你就不需要尝试“合并”你的 perl 程序了。

【讨论】:

    【解决方案2】:

    要合并两个脚本并在同一行输出两项数据,您需要等待文件结束(或直到您拥有两个数据项),然后一次输出。所以你需要将两个循环合二为一:

    #!/usr/bin/perl
    
    
    use strict ;
    use warnings ;
    use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
    
    my $input = "par_disp_fabric.all_max_lowvcc_qor.rpt.gz";
    my $output = "par_disp_fabric.all_max_lowvcc_qor.txt";
    
    
    gunzip $input => $output
        or die "gunzip failed: $GunzipError\n";
    
    open (FILE, '<',"$output") or die "Cannot open $output\n";
    
    my( $levels, $timing );
    while (<FILE>) {
        my $line = $_;
        chomp ($line);
    
        if ($line=~ m/^\s+Levels of Logic:\s+(\S+)/) {
            $levels = $1;
        }
        if ($line=~ m/^\s+Timing Path Group \'(\S+)\'/) {
            $timing = $1;
        }
    }
    print "$levels, $timing\n";
    
    close (FILE);
    

    【讨论】:

    • 嗨@Corion。 Tq 回答我的问题。但是为什么输出只打印最后一行数据呢?实际上,对于代码 1 和代码 2 的每个输出,我都有 26 行数据。
    • 这是我的代码 1 的输出,其中包含 26 行数据。---------------------------- ----- async_default clock_gating_default Ddia_link_clk Ddib_link_clk Ddic_link_clk Ddid_link_clk FEEDTHROUGH INPUTS LCLK OUTPUTS VISA_HIP_visa_tcss_2000 ckpll_npk_npkclk clstr_fscan_scanclk_pulsegen clstr_fscan_scanclk_pulsegen_notdiv clstr_fscan_scanclk_wavegen idvfreqA idvfreqB psf5_primclk sb_nondet4tclk sb_nondetl2tclk sb_nondett2lclk sbclk_nondet sbclk_sa_det stfclk_scan tap4tclk tapclk 跨度>
    【解决方案3】:

    您还没有向我们提供一项重要信息 - 输入数据是什么样的。最重要的是,您要查找的两条信息是否在同一行?

    [更新:更仔细地查看您的正则表达式,我发现两条信息可能位于同一行 - 因为它们都应该是该行的第一项。如果您在问题中对此更清楚,将会很有帮助。]

    我认为这会做正确的事情,无论您的问题的答案是什么。我还添加了我在对your previous question 的回答中建议的改进:

    #!/usr/bin/perl
    
    use strict ;
    use warnings ;
    use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
    
    my $zipped   = "par_disp_fabric.all_max_lowvcc_qor.rpt.gz";
    my $unzipped = "par_disp_fabric.all_max_lowvcc_qor.txt";
    
    gunzip $zipped => $unzipped
        or die "gunzip failed: $GunzipError\n";
    
    open (my $fh, '<', $unzipped) or die "Cannot open '$unzipped': $!\n";
    
    my ($levels, $timing);
    
    while (<$fh>) {
        chomp;
    
        if (m/^\s+Levels of Logic:\s+(\S+)/) {
            $levels = $1;
        }
        if (m/^\s+Timing Path Group \'(\S+)\'/) {
            $timing = $1;
        }
    
        # If we have both values, then print them out and
        # set the variables to 'undef' for the next iteration
        if ($levels and $timing) {
            print "$levels, $timing\n";
            undef $levels;
            undef $timing;
        }
    }
    
    close ($fh);
    

    【讨论】:

      猜你喜欢
      • 2012-03-15
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 2020-04-29
      • 2023-02-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      相关资源
      最近更新 更多