Zoe
Zoe

Reputation: 57

Perl: How to print next line after matching a pattern?

I would like to print specific data after matching a pattern or line. I have a file like this:

#******************************    
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  

#. .    
#. .     
#.    
#.    

I want to print the data for example after the lines "Type...." and dashes line"------" which is N17 and bg099. I have tried this but it cannot work.

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";      
}      
}    
}    

Expected Output:

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

Upvotes: 3

Views: 32797

Answers (7)

Sasha Pachev
Sasha Pachev

Reputation: 5336

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

Upvotes: 1

Kenosis
Kenosis

Reputation: 6204

Here's another option:

use strict;
use warnings;

print "Car Type\tNo.\n";

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

Output:

Car Type    No.
N17 bg099
A57 ft2233
L45 nh669

Usage: perl script.pl inFile [>outFile]

Edit: Simplified

Upvotes: 4

Borodin
Borodin

Reputation: 126762

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";
}

Upvotes: 2

Mattan
Mattan

Reputation: 753

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";
    }
}

Or if you prefer a more compact solution:

while (<FILE>) { 
    if ($_ =~ /^Car/) { 
        <FILE> or die "Bad car file format"; 
        print join(" ",(<FILE> =~ /(\w+)\s+(\w+)/))."\n";
    } 
} 

Upvotes: 6

Vijay
Vijay

Reputation: 67299

perl -lne 'if(/Type/){$a=<>;$a=<>;$a=~m/^([^\s]*)\s*([^\s]*)\s/g; print $1." ".$2}' your_file

tested:

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

if you want to use awk,you can do this as below:

> awk '/Type/{getline;if($0~/^#---*/){getline;print $1,$2}}' your_file
N17 bg099
A57 ft2233
L45 nh669

Upvotes: 1

Alec
Alec

Reputation: 1188

a shell one-liner to do the same thing

echo "Car Type            No.  "; \
    grep -A 2 Type data.txt \
    | grep -v -E '(Type|-)' \
    | grep -o -E '(\w+ *\w+)'

Upvotes: 1

bergie3000
bergie3000

Reputation: 1131

I got your code to work with a couple small tweaks. It's still not perfect but it works.

  • "while" should be lower case.
  • You never increment $i.
  • The way you reuse @array is confusing at best, but if you just output $a you'll get your car data.

Code:

$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);

Upvotes: 2

Related Questions