如何从no文本文件中搜索没有字符串在给定目录中

时间:2017-02-08 13:17:21

标签: perl

我想在consol中搜索我的字符串输入,即“VoLTE SIPTX:[SIPTX-SIP] ==> REGISTER”来自文件中的文件目录(文件的示例数据如下所示)但无法做到以下代码相同。

这是我的代码:

use strict;
use warnings;

print "Entering the value i.e. the IMS Message to compare with";

my $value       = '';
my $choice      = '';
my $loop        = '';
my $IMS_Message = '';
my $search      = '';
my $kw          = '';

print "\nThe script path & name is $0\n";
print "\nPlease enter desired number to select any of the following   

    (1) Start Comparing REGISTER message !!
    (2) Start Comparing SUBSCRIBE message
    (3) Start Comparing INVITE message \n";

$value = <STDIN>;

if ( $value == 1 ) {

    print "\n Start Comparing REGISTER message\n\n";
    $IMS_Message = "VoLTE SIPTX: [SIPTX-SIP] ==>  REGISTER";
}
elsif ( $value == 2 ) {

    print "\n SUBSCRIBE message Flow\n\n";
}
elsif ( $value == 3 ) {

    print "\n INVITE message Flow\n\n";
}
else {

    print "\nThe input is not valid!\n";
    print "\nDo you want to continue selecting a Automation Mode again (Y or N)?\n";

    $choice = <STDIN>;

    if ( $choice eq /[Yy]/ ) {
        test_loop();
    }
    else {
        exit;
    }

    $kw = $IMS_Message;

    #$search = qr/\Q$kw/;
    $search = quotemeta( $kw );

    for my $file ( grep {-f} glob '*main_log' ) {

        open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};

        while ( <$fh> ) {

            if ( /$search/ ) {

                printf "Found keyword %s in file %s, line %d: %s\n", $kw, $file, $., $_;
                last;
            }
        }
    }
}

以下是我想要搜索字符串的文件数据

**TempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp
*TempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp
VoLTE SIPTX: [SIPTX-SIP] ==> REGISTERTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp
tempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp
temptempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp
***tempTeTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp

以下代码与命令行不起作用

use strict;
use warnings;


    my $value = '';
    my $choice = '';
    my $loop = '';
    my $IMS_Message = '';

    print "\nThe script path & name is $0\n";
    print "\nPlease enter desired number to select any of the following   


    (1) Start Comparing REGISTER message !! !!

    (2) Start Comparing SUBSCRIBE message

        (3) Start Comparing INVITE message \n";;

    $value = <STDIN>;

    if ($value == 1 )
    {

        print "\n Start Comparing REGISTER message\n\n";
        my $IMS_Message = 'VoLTE SIPTX: [SIPTX-SIP] ==>  REGISTER';
        &IMS_Message_Check;
    }

    elsif ($value == 2)
    {

        print "\n SUBSCRIBE message Flow\n\n";

    }
    elsif ($value == 3)
    {

    print "\n INVITE message Flow\n\n";

    }
    else 
    {
        print "\nThe input is not valid!\n";
        print "\nDo you want to continue selecting a Automation Mode again (Y or N)?\n";

        $choice = <STDIN>;
        if( $choice =~ /[Yy]/) {        
            test_loop();
        } else {
            exit;
        }
    }


sub IMS_Message_Check{

print "\n";
print "Start Comparing REGISTER message\n\n";

#my $IMS_Message = 'VoLTE SIPTX: [SIPTX-SIP] ==>  REGISTER';


my $kw = $IMS_Message;
my $search = qr/\Q$kw/;
for my $file ( grep { -f } glob '*main_log' ) {
    open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};
    while ( <$fh> ) {
        if ( /$search/ ) {
            printf "Found keyword %s in file %s, line %d: %s\n", $kw, $file, $., $_;
            #last;
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

好的,您的代码存在两个主要问题

  • 您已将我的解决方案添加到连续if语句的 输入无效 分支中。这就是为什么我要求你保持你的代码整洁和正确缩进。如果你的缩进是有条理的,那么你自己会发现它

  • 您的程序正在搜索VoLTE SIPTX: [SIPTX-SIP] ==> REGISTER,而文件中的文字为VoLTE SIPTX: [SIPTX-SIP] ==> REGISTER(您的程序在REGISTER之前有两个空格,而您的数据只有一个)。难道不是一件明显的事情要检查吗?

我确信你可以为自己发现这些东西,你当然可以阅读How to create a Minimal, Complete, and Verifiable example而不是发布非常糟糕的代码而且首先没有样本数据

我忽略了您的命令行菜单(无论如何都不应该在代码中),包括您对不存在的test_loop的调用,并写了这个,表现得当。其中大部分是我提供给您上一个问题的解决方案的副本,您说它不起作用

use strict;
use warnings;

print "\n";
print "Start Comparing REGISTER message\n\n";

my $IMS_Message = 'VoLTE SIPTX: [SIPTX-SIP] ==> REGISTER';

my $kw = $IMS_Message;

my $search = qr/\Q$kw/;

for my $file ( grep { -f } glob '*main_log' ) {

    open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};

    while ( <$fh> ) {

        if ( /$search/ ) {

            printf "Found keyword %s in file %s, line %d: %s\n", $kw, $file, $., $_;
            last;
        }
    }
}

输出

Start Comparing REGISTER message

Found keyword VoLTE SIPTX: [SIPTX-SIP] ==> REGISTER in file xmain_log, line 3: VoLTE SIPTX: [SIPTX-SIP] ==> REGISTERTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTempTemp