Abhishek Abhishek - 27 days ago 8x
Perl Question

Trouble extracting specific information from log file

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

  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

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

  4. If the value is 0x2789 then split and join 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}

my expected output:



#! /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/ ) {

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

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.


"I was waiting for the solutions"

You urgently need to read and absorb How to ask

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"

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

This program does what you say you want

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;