简体   繁体   中英

Perl RegEx for matching char '*' in first position of the string

I am trying to match the line starting with a char '*'. The file mac-address.txt is as follows:

Legend: * - primary entry
    age - seconds since last seen
    n/a - not available

vlan   mac address     type    learn     age              ports
------+----------------+--------+-----+----------+--------------------------
*   14  782b.cb87.b085   dynamic  Yes          0   Gi4/39
*  400  c0ea.e459.913d   dynamic  Yes          0   Gi6/21
*  400  0017.c59a.23aa   dynamic  Yes          0   Gi3/37
*  400  d4be.d96a.050f   dynamic  Yes          0   Gi1/12

I have got something like this working.

#!/usr/bin/perl 
use strict;
use warnings;
my $line;
open FH, '<', "mac-address.txt";
while ($line = <FH>)
{
    if ($line =~ m/^\S/){
    print ("$line");
    }
}
close FH;

But it works along with some unwanted lines as follows:

Legend: * - primary entry
------+----------------+--------+-----+----------+--------------------------
*   14  782b.cb87.b085   dynamic  Yes          0   Gi4/39
*  400  c0ea.e459.913d   dynamic  Yes          0   Gi6/21
*  400  0017.c59a.23aa   dynamic  Yes          0   Gi3/37
*  400  d4be.d96a.050f   dynamic  Yes          0   Gi1/12

Please can you help me with some appropriate way such that only the line containing the * is preserved and the others are eliminated. Please inform me if something is wrong. Thank you.

您需要转义*因为它很脏

/^\*/

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