开发者

How can I parse this file format in Perl?

Maybe someone have idea for perl script with very strong irregular expression that match only lines with uniqe rule ( [param_name] [separator] [any_value] )

Rules described in EXAMPLE1 and EXAMPLE2

The target of the script is to match the Valid Lines, described below (EXAMPLE1 – VALID lines and ignore the non valid lines described in EXAMPLE2 - non valid lines)

I will be glad if someone have a brilliant code that match the valid lines from file and print the valid lines in to /tmp/Valid.txt And print the non valid line in to /tmp/nonvalid.txt

Example how to run the script

./find_valid_lines.pl   file

Definition:

Valid line syntax:

[Parameter_name] [separato开发者_开发技巧r] [Value]

where:

  • Parameter_name: any string. but not number. or string that start with number/s
  • separator: is the "=" character (only one seperator each line)
  • Value: any string or number or character include Exceptional characters as $ * & ( _ { [ ' " ? < ~ ! and so on

But in case the line has the ; character, the script must take the line until ; only if line before ; is a valid line.

For example:

 MY__param = 100 ; 1000

Then

 MY__param = 100 
  • Remark: the script will ignore the line if "#" char exist in the line beginning

Examples of lines in files (valid lines – example1 and not valid lines – example2)

EXAMPLE1: Valid Lines (Valid parameter & Valid separator & Valid value)

 parameter=1 
 parameter.1=1 2 3
 MY_Name.2= A B C
 parameter =1 * * $ @
 Home_NAME =MOON
 param1=1 2 3 + * *
 Param_A = 73826.32863
 PARAM_STAR    =   23.84 (2.d) 12 & {0} (8) 100%
 Param120A = ~1.33454656
 @param =90%
 P1= -39546
 My_param=#
 My_P = # #123
 MY_Parameter =   <34> 2 3 4 5 6 7 8 9 A "ededefec" $100
 uniq_param=?
 T = 0
 GH.@=%
 PORT_NUM= 123 / 98775 /  554 / 34545 / 54
 ADDRESS  = 172.19.0.1
 FolderHome     = /dir746/dir87/file1 , /dir746/dir87/file2
 switch_from_LAN*=100G_SPEED

EXAMPLE2: Non Valid Lines (Non Valid parameter OR Non Valid separator OR Non Valid value)

 PARAM== 100
 Param = 276 =
 276 = 2652
 = 234
 Name = 
 PARAMETER = ;
 Param_is_file 123
 port 19463
 David_name
 243546
 =
 635A
 10Param
 *&^
 = &^#$>:
 A 123
 65MY_PARAM_NAME=10
 all_strings=; 75845
 switch_from_LAN*=


Here is one way:

#!/usr/bin/perl

use strict; use warnings;
use File::Slurp;

while ( my $line = <DATA> ) {
    my $save = $line;
    $line =~ s/\s*;.*\z//s; # ignore comments

    my ($p, $v) = map { s/^\s+//; s/\s+\z//; $_ } split /=/, $line, 2;

    if ( defined($v) and $p =~ /^[^0-9].*\z/ and $v =~ /^[^=]+\z/ ) {
        append_file 'valid.txt', \ $save;
    }
    else {
        append_file 'invalid.txt', \ $save;
    }
}

__DATA__
parameter=1
parameter.1=1 2 3
MY_Name.2= A B C
parameter =1 * * $ @
Home_NAME =MOON
param1=1 2 3 + * *
Param_A = 73826.32863
PARAM_STAR    =   23.84 (2.d) 12 & {0} (8) 100%
Param120A = ~1.33454656
@param =90%
P1= -39546
My_param=#
My_P = # #123
MY_Parameter =   <34> 2 3 4 5 6 7 8 9 A "ededefec" $100
uniq_param=?
T = 0
GH.@=%
PORT_NUM= 123 / 98775 /  554 / 34545 / 54
ADDRESS  = 172.19.0.1
PARAM== 100
Param = 276 =
276 = 2652
= 234
Name =
PARAMETER = ;
Param_is_file 123
port 19463
David_name
243546
=
635A
10Param
*&^
= &^#$>:
A 123
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜