3

我想在匹配模式或行后打印特定数据。我有一个这样的文件:

#******************************    
List : car  
Design: S  
Date: Sun 10:10  
#******************************

b-black  
g-green
r-red  

Car Type              No.           color  
#-------------------------------------------    
N17              bg099            g  
#-------------------------------------------    
Total 1 car  

#****************************** 
List : car  
Design: L  
Date: Sun 10:20  
#******************************

b-black  
g-green
r-red  

Car Type            No.            color   
#-------------------------------------------    
A57            ft2233            b  
#-------------------------------------------    
Total 1 car  

#******************************    
List : car  
Design: M  
Date: Sun 12:10  
#******************************    

b-black  
g-green
r-red  

Car Type           No.             color  
#-------------------------------------------    
L45            nh669             g   
#-------------------------------------------    
Total 1 car  

#. .    
#. .     
#.    
#.    

例如,我想在 N17 和 bg099 行“Type....”和虚线“------”之后打印数据。我已经尝试过了,但它无法正常工作。

my @array;    

While (@array = <FILE>) {    

foreach my $line (@array) {    

if ($line =~ m/(Car)((.*))/) {      

my $a = $array[$i+2];    
push (@array, $a);    
}  

if ($array[$i+2] =~ m/(.*)\s+(.*)\s+(.*)/) {    
my $car_type = "$1";      
print "$car_type\n";      
}      
}    
}    

预期输出:

Car Type            No.  
   N17             bg099  
   A57             ft2233    
   L45             nh669  
   ..              ..  
   .               . 
4

7 回答 7

6
while (<FILE>) { #read line by line
    if ($_ =~ /^Car/) { #if the line starts with 'Car'
        <FILE> or die "Bad car file format"; #read the first line after a Car line, which is '---', in order to get to the next line
        my $model = <FILE>; #assign the second line after Car to $model, this is the line we're interested in.

        $model =~ /^([^\s]+)\s+([^\s]+)/; #no need for if, assuming correct file format #capture the first two words. You can replace [^\s] with \w, but I prefer the first option.
        print "$1 $2\n";
    }
}

或者,如果您更喜欢更紧凑的解决方案:

while (<FILE>) { 
    if ($_ =~ /^Car/) { 
        <FILE> or die "Bad car file format"; 
        print join(" ",(<FILE> =~ /(\w+)\s+(\w+)/))."\n";
    } 
} 
于 2013-03-11T05:58:16.707 回答
4

这是另一种选择:

use strict;
use warnings;

print "Car Type\tNo.\n";

while (<>) {
    if (/#-{32}/) {
        print "$1\t$2\n" if <> =~ /(\S+)\s+(\S+)/;
        <>;
    }
}

输出:

Car Type    No.
N17 bg099
A57 ft2233
L45 nh669

用法:perl script.pl inFile [>outFile]

Edit: Simplified

于 2013-03-11T06:23:13.383 回答
2

我让您的代码可以进行一些小的调整。它仍然不完美,但它有效。

  • “while”应该是小写的。
  • 你永远不会增加 $i。
  • 你重用@array 的方式充其量是令人困惑的,但如果你只输出 $a 你会得到你的汽车数据。

代码:

$file_to_get = "input_file.txt";
open (FILE, $file_to_get) or die $!;

my @array;

while (@array = <FILE>) {
    $i = 0;

    foreach my $line (@array) {

        if ($line =~ m/(Car)((.*))/) {
            my $a = $array[$i+2];
            push (@array, $a);
            print $a;
        }

        $i++;
    }
}
close(FILE);
于 2013-03-11T05:57:21.987 回答
2

Something like this:

while (my $line = <>) {
    next unless $line =~ /Car\s+Type/;
    next unless $line = <> and $line =~ /^#----/;
    next unless $line = <>;
    my @fields = split ' ', $line;
    print "@fields[0,1]\n";
}
于 2013-03-11T09:50:05.103 回答
1
perl -lne 'if(/Type/){$a=<>;$a=<>;$a=~m/^([^\s]*)\s*([^\s]*)\s/g; print $1." ".$2}' your_file

测试:

> perl -lne 'if(/Type/){$a=<>;$a=<>;$a=~m/^([^\s]*)\s*([^\s]*)\s/g; print $1." ".$2}' temp
N17 bg099
A57 ft2233
L45 nh669

如果你想使用 awk,你可以这样做:

> awk '/Type/{getline;if($0~/^#---*/){getline;print $1,$2}}' your_file
N17 bg099
A57 ft2233
L45 nh669
于 2013-03-11T05:59:22.917 回答
1

一个外壳单线做同样的事情

echo "Car Type            No.  "; \
    grep -A 2 Type data.txt \
    | grep -v -E '(Type|-)' \
    | grep -o -E '(\w+ *\w+)'
于 2013-03-11T06:19:32.490 回答
1

Solution using Perl flip-flop operator. Assumption from the input that you always have Total line at the end of the block of interest

perl -ne '$a=/^#--/;$b=/^Total/;print if(($a .. $b) && !$a && !$b);' file
于 2015-09-28T22:19:38.807 回答