我在这里有日志文件我想提取以下信息
想要提取减值(-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
#! /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值并转换回十六进制。
我想我需要修改正则表达式。
答案 0 :(得分:3)
&#34;我在等待解决方案&#34;
您迫切需要阅读并吸收How to ask
我不明白为什么你从@ARGV
获取的三个变量用于在每行输入中进行替换
此外,您的代码会将每个修改后的行复制到输出文件中,但它不会出现在您预期的输出中#34;
你需要做的不仅仅是找到一些看起来可能有效的代码,然后将其破解并将其放在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;
}
0x27,0x89,0x14,0x05,0x0B,0x00,0x00,0x00,0xFF,0x8C,0x01,0x00,0x00,0xFF,0x9B,0x00,0x00,0x00,0x36,0x01,0x00,0x00,0x00,0x07