简体   繁体   中英

Perl regex over multiple lines

I have 2 input files.

$> cat file1.txt
! This is a comment in file1.txt
// Another comment and below line is an empty line

SIR 8 
    TDI(03)
    TDO(01)
    MASK(03);

and

$> cat file2.txt
! This is a comment in file2.txt
// Another comment and below line is an empty line

sir 8 tdi(03) tdo(01) mask(03);

Now, I'm trying to write a script that would harvest all those 'sir' lines. This is what I have:

while(<>) {
    # Skip over all lines that start with ! or are empty or start with //
    next unless !/^!/ and !/^\s*$/ and !/^\s*\/\//;

    # I'm using the modifier /i to be case insensitive
    if(/sir\s+\d+\s+tdi\(\d+\)\s+tdo\(\d+\)\s+mask\(\d+\)\s*;/i) {
        print $_;
    }
}

This matches now file2.txt which is on a single line but not file1.txt which is on multiple lines. I googled a lot and tried the modifiers /m /s and /g which were suggested but with no luck. Please can you help me to find the right syntax?

You are reading in a line at at time and matching against that, so you can't possibly match something that spans more than one line.

It's easiest to read the whole file at once by undefining $/ .

local $/;

while (<>) {
    while (/^sir\s+\d+\s+tdi\(\d+\)\s+tdo\(\d+\)\s+mask\(\d+\)\s*;/mig) {
        print "$&\n";
    }
}

The /m makes the ^ match the start of a line.

Replacing if (//) with while (//g) allows us to get all the matches.


As a one-liner,

perl -0777ne'CORE::say $& while /^SIR[^;]*;/mig'

Specifying file to process to Perl one-liner

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM