无法从日志文件中提取特定信息 [英] Trouble extracting specific information from log file

查看:77
本文介绍了无法从日志文件中提取特定信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the log file here I want to extract the following information

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

  1. Want to extract hex value.
  2. Want to extract the decimal value if the line has second bracket{0-9} first convert to hex then extract(9-->0x09)
  3. Want to extract minus value(-25) first convert to hex then extract

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

e.g.-25-->FFE7--> want to extract -->0xFF,0XE7

如果值为 0x2789 则拆分并加入 0x(2789--> 0x27,0x89)

If the value is 0x2789 then split and join 0x(2789--> 0x27,0x89)

my_input_data

     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

我的代码

#! /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 值并转换回十六进制.

The above code works for extracting the hex value, but not for extracting the decimal {0-9} and minus -25 value and converting back to hex.

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

I think I need to modify the regex expression.

推荐答案

我在等待解决方案"

您迫切需要阅读并吸收如何提问

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

I don't understand why you have the three variables that you pull from @ARGV which you use to make substitutions in each line of input

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

Also, your code copies each modified line to the output file, but it doesn't appear in your "expected output"

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

You need to do more than finding some code on line that looks like it may work, and then hacking it a bit and putting it on Stack Overflow for others to finish for you. You have instantly lost many people's respect, and you may well struggle to get answers to further questions

这个程序做你想做的事

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;
}

输出

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

这篇关于无法从日志文件中提取特定信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆