无法从日志文件中提取特定信息

2024-05-17

我这里有日志文件我想提取以下信息

  1. 想要提取十六进制值。
  2. 如果该行有第二个括号{0-9},想要提取十进制值,首先转换为十六进制,然后提取(9-->0x09)
  3. 想要提取负值(-25)先转换为十六进制然后提取

    例如-25-->FFE7-->想要提取-->0xFF,0XE7

  4. 如果值为 0x2789,则拆分并连接 0x(2789--> 0x27,0x89)

我的输入数据

     my_info    0x2789  Uint16, unsigned short      
     param_id   0x14    Uint8,unsigned char     
     cell_id    0x05    Uint8,unsigned char     
     Indicator  0x0B    Uint8,unsigned char     
     filler1{3} { 0x00, 0x00, 0x00 }    Uint8,unsigned char     
     rscp_tap   -116    Sint8,signed char       
     filler2{3} { 0x01, 0x00, 0x00 }    Uint8,unsigned char     
     dsp    -101    Sint8,signed char       
     filler3{3} { 0x00, 0x00, 0x00 }    Uint8,unsigned char     
     system_fm_number   0x3601  Uint16, unsigned short      
     filler4{2} { 0x00, 0x00 }  Uint8,unsigned char 
     dsp_input      {7}

我的预期输出:

    0x27,0x89,0x14,0x05,0x0B,0x00,0x00,0x00,0xFF,
    0x8C,0x01,0x00,0x00,0xFF,0x9B,0x36,0x01,0x00,0x00,0x07

my_code

#! /usr/bin/env perl

use strict;
use warnings;

use List::MoreUtils 'true';

use feature qw(say);

use Data::Dumper;

# input variable pass as a input argument
my $variable_name = shift @ARGV;

# variable value pass as a input argument
my $variable_value = shift @ARGV;

#variable value need to be replaced with new value
my $Replacement_var = shift @ARGV;

# Name of the file the data is in
my $input_filename = 'input.txt';

# Name of the file you want to dump the output to
my $output_filename = 'output.txt';

# Open the file
open my $input_fh, "<", $input_filename or die $!;

# Open the output file
open my $output_fh, ">", $output_filename or die $!;

# Array to store the hex data
my @hex_array;
my @data_new;

# Loop over each of the lines of the file
while ( <$input_fh> ) {

    # Find all the matches and push them to the array

    if ( /$variable_name/ and /$variable_value/ ) {
        s/$variable_value/$Replacement_var/;
    }

    print $output_fh $_;

    #here  extracting only hex values from each line
    while ( $_ =~ m/(0x(\d+)(?:[0-9]|[A-f])+)/gi ) {
        push @hex_array, ( $1 );
    }
}

# Close the file
close $input_fh;

# Write the data to the file
@data_new = join( ", ", @hex_array );

print {$output_fh} @data_new;

# Close the file
close $output_fh;

# Exit
exit();

上面的代码适用于提取十六进制值,但不适用于提取 十进制 {0-9} 和负 -25 值并转换回十六进制。

我想我需要修改正则表达式。


“我一直在等待解决方案”

你迫切需要阅读和吸收如何询问 https://stackoverflow.com/questions/how-to-ask

我不明白为什么你有从中提取的三个变量@ARGV用于在每行输入中进行替换

此外,您的代码将每个修改的行复制到输出文件,但它不会出现在您的“预期输出”中

你需要做的不仅仅是在网上找到一些看起来可行的代码,然后对其进行一些修改并将其放在 Stack Overflow 上供其他人为你完成。您立即失去了许多人的尊重,并且您很可能很难获得进一步问题的答案

这个程序按照你说的做

use strict;
use warnings 'all';

my ( $infile, $outfile ) = qw/ input.txt output.txt /;

open my $fh, '<', $infile  or die $!;

my @data;

while ( <$fh> ) {

    my ($f2) = / \S \s+ ( \{ [^{}]+ \} | \S+ ) /x;

    while ( $f2 =~ / 0x ( \p{hex}+ ) | ( [+-]?\d+ ) /xg ) {
        push @data, $1 // sprintf '%04X', $2 & 0xFFFF;
    }
}

{
    my $data = join ',', map "0x$_", map { unpack '(A2)*' } @data;

    open my $fh, '>', $outfile or die $!;
    print $fh $data, "\n";
    close $fh;
}

output

0x27,0x89,0x14,0x05,0x0B,0x00,0x00,0x00,0xFF,0x8C,0x01,0x00,0x00,0xFF,0x9B,0x00,0x00,0x00,0x36,0x01,0x00,0x00,0x00,0x07
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法从日志文件中提取特定信息 的相关文章

随机推荐