我这里有日志文件我想提取以下信息
- 想要提取十六进制值。
- 如果该行有第二个括号{0-9},想要提取十进制值,首先转换为十六进制,然后提取(9-->0x09)
-
想要提取负值(-25)先转换为十六进制然后提取
例如-25-->FFE7-->想要提取-->0xFF,0XE7
如果值为 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 值并转换回十六进制。
我想我需要修改正则表达式。