如何使用Perl从文件中的第6列到最后行打印文本?

时间:2018-02-09 10:52:38

标签: perl

文件:

;; LABEL "EN-F" "Callhome Female" "Callhome Female Caller"
;; LABEL "EN-M" "Callhome Male" "Callhome Male Caller"
;; LABEL "SW-F" "Switchboard Female" "Switchboard Female Caller"
;; LABEL "SW-M" "Switchboard Male" "Switchboard Male Caller"
en_4156 A en_4156_A 301.85 302.48 <O,en,F,en-F>  OH YEAH
en_4156 A en_4156_A 304.71 306.72 <O,en,F,en-F>  WELL I AM GOING TO HAVE MINE IN TWO MORE CLASSES
en_4156 A en_4156_A 307.63 311.16 <O,en,F,en-F>  NO I AM NOT WELL THEN I HAVE TO TAKE MY EXAMS MY ORALS BUT
en_4156 A en_4156_A 313.34 315.37 <O,en,F,en-F>  THAT IS KIND OF WHAT I WOULD LIKE TO DO
en_4156 A en_4156_A 316.83 319.20 <O,en,F,en-F>  I MIGHT EVEN WANT TO GO ON AND GET MY P H D
en_4156 A en_4156_A 321.55 322.16 <O,en,F,en-F>  IT IS JUST THAT
en_4156 A en_4156_A 323.05 327.38 <O,en,F,en-F>  I DO NOT KNOW IT IS SO COMFORTABLE WHERE I AM AND AND IF I

输出文件:

;; LABEL "EN-F" "Callhome Female" "Callhome Female Caller"
;; LABEL "EN-M" "Callhome Male" "Callhome Male Caller"
;; LABEL "SW-F" "Switchboard Female" "Switchboard Female Caller"
;; LABEL "SW-M" "Switchboard Male" "Switchboard Male Caller"
en_4156  <O,en,F,en-F>  OH YEAH
en_4156  <O,en,F,en-F>  WELL I AM GOING TO HAVE MINE IN TWO MORE CLASSES
en_4156  <O,en,F,en-F>  NO I AM NOT WELL THEN I HAVE TO TAKE MY EXAMS MY ORALS BUT
en_4156  <O,en,F,en-F>  THAT IS KIND OF WHAT I WOULD LIKE TO DO
en_4156  <O,en,F,en-F>  I MIGHT EVEN WANT TO GO ON AND GET MY P H D
en_4156  <O,en,F,en-F>  IT IS JUST THAT
en_4156  <O,en,F,en-F>  I DO NOT KNOW IT IS SO COMFORTABLE WHERE I AM AND AND IF I

代码:

#!/usr/bin/perl

my ($file, $new_file)=@ARGV;
open(FILE, "<$file") or die "Unable to open $file";
open(NEW, ">$new_file") or die "Unable to open $new_file";
while (<FILE>) {
  @line=split(" ", $_);
  push @wav_s, $line[0];
  push @channel_s, $line[1];
  push @start_s, $line[3];
  push @end_s, $line[4];
  push @label, $line[5];
  push @txt, $line[6..$#line];
  if ($wav_s eq ";;") {
    print NEW $_;
  }
}
$ls=scalar @start_s;

for ($i=0; $i<$ls; $i++) {
  while ($wav_s[$i] eq ";;") {
    $i++;
  }
  print NEW "$wav_s[$i] $label[$i] $txt[$i]\n";
}

我想在另一个文件中打印第0列,第5列以及第6列到文件末尾的所有字符。 这个问题的正确代码是什么?

  

任何人都可以填写此代码。

1 个答案:

答案 0 :(得分:0)

你让它变得比它需要的更复杂。看起来你有固定宽度的数据,为什么不用unpack提取它?

$('#night-mode').on('click', function(){

    $('#team-select').css('background', '#313131');
    $('.color').css('background', '#313131');
    $('html .game-cards, body .game-cards').css('background', '#737373');
    $('html .menu li a, body .menu li a').css('color', '#9ecd1a');
    $('html .menu select, body .menu select').css('background', '#9ecd1a');
    if ($('input.night-mode').prop('checked', false)){
        alert('hello')
    } else if($('input.night-mode').prop('checked', true)){
        alert('not-checked')
    }
});

输出:

use strict;
use warnings;

while (<DATA>) {
    if (/^;;/) {
        print;
        next;
    }

    my @fields = unpack('A8 A2 A10 A7 A7 A15 A*');
    print join('  ', @fields[0, 5, 6]),  "\n";
}

__DATA__
;; LABEL "EN-F" "Callhome Female" "Callhome Female Caller"
;; LABEL "EN-M" "Callhome Male" "Callhome Male Caller"
;; LABEL "SW-F" "Switchboard Female" "Switchboard Female Caller"
;; LABEL "SW-M" "Switchboard Male" "Switchboard Male Caller"
en_4156 A en_4156_A 301.85 302.48 <O,en,F,en-F>  OH YEAH
en_4156 A en_4156_A 304.71 306.72 <O,en,F,en-F>  WELL I AM GOING TO HAVE MINE IN TWO MORE CLASSES
en_4156 A en_4156_A 307.63 311.16 <O,en,F,en-F>  NO I AM NOT WELL THEN I HAVE TO TAKE MY EXAMS MY ORALS BUT
en_4156 A en_4156_A 313.34 315.37 <O,en,F,en-F>  THAT IS KIND OF WHAT I WOULD LIKE TO DO
en_4156 A en_4156_A 316.83 319.20 <O,en,F,en-F>  I MIGHT EVEN WANT TO GO ON AND GET MY P H D
en_4156 A en_4156_A 321.55 322.16 <O,en,F,en-F>  IT IS JUST THAT
en_4156 A en_4156_A 323.05 327.38 <O,en,F,en-F>  I DO NOT KNOW IT IS SO COMFORTABLE WHERE I AM AND AND IF I