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

后端 未结 7 2022
刺人心
刺人心 2021-01-06 15:52

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

#******************************    
List : car  
Design: S           


        
7条回答
  •  情书的邮戳
    2021-01-06 15:53

    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

提交回复
热议问题