我有一个这样的字符串,我只需要从行中提取十六进制值,然后在新文件中复制十六进制值。
文件中的输入行:
ame (header(...)) 0x0D 0x0C 0x4A 0x00 0x01 0x00, 0x02 0x00, 0x0A 0x00,
0x04 0x00, 0x04 0x05 0x00 0x001f 0x001f 0x007f 0x00, 0x002b 0x007f 0x0000
0x00 0x0000 0xffffffaf 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x00,
(non_t_crmax 0x0D 0x00, TDD 0x5D 0x2760 Invalid 0x0000 0x02 0x00,
(rat_type (rat_type (rat_type (rat_type (rat_type (rat_type (rat_type
预期产出:
0x0D, 0x0C, 0x4A, 0x00, 0x01, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x04, 0x00,
0x04, 0x05,0x00, 0x001f, 0x001f, 0x007f, 0x00, 0x002b, 0x007f, 0x0000, 0x00
0x0000, 0xffffffaf,0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x0D,
0x00,0x5D 0x2760 0x0000 0x02 ,0x00,
答案 0 :(得分:2)
#!/usr/bin/perl
use strict;
use warnings;
my $input = << 'DATA'; # this is what you read from your file
ame (header(...)) 0x0D 0x0C 0x4A 0x00 0x01 0x00, 0x02 0x00, 0x0A 0x00,
0x04 0x00, 0x04 0x05 0x00 0x001f 0x001f 0x007f 0x00, 0x002b 0x007f 0x0000
0x00 0x0000 0xffffffaf 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x00,
(non_t_crmax 0x0D 0x00, TDD 0x5D 0x2760 Invalid 0x0000 0x02 0x00,
(rat_type (rat_type (rat_type (rat_type (rat_type (rat_type (rat_type
DATA
my @hexvals = $input =~ /(0x[\da-f]+)/ig;
print join ', ', @hexvals; # output
输出:
0x0D, 0x0C, 0x4A, 0x00, 0x01, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x04, 0x00,
0x04, 0x05, 0x00, 0x001f, 0x001f, 0x007f, 0x00, 0x002b, 0x007f, 0x0000,
0x00, 0x0000, 0xffffffaf, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
0x0D, 0x00, 0x5D, 0x2760, 0x0000, 0x02, 0x00
答案 1 :(得分:1)
这是一个简单的perl脚本(带注释),解析输入和输出你需要的东西。
#!/usr/bin/env perl
# buffer creation, will contain all desired values
my @buf;
# main loop from stdin
while (<>) {
# 2. push value in @buf if match desired regrex
map({ push(@buf,$_) if $_ =~ /^0x[0-9a-f]+$/i}
# 1. split every line on null char
split(/\s+/, $_));
}
# print comma separated result
print join(",", @buf)."\n";
示例:
chmod +x script.pl
cat data | ./script.pl
mycoolcommand | ./script.pl
输出:
0x0D,0x0C,0x4A,0x00,0x01,0x02,0x0A,0x04,0x04,0x05,0x00,0x001f,0x001f,0x007f,0x002b,0x007f,0x0000,0x00,0x0000,0xffffffaf,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0D,0x5D,0x2760,0x0000,0x02
答案 2 :(得分:1)
到目前为止,你所有的答案都是正确的,但它们似乎使得事情变得更加艰难。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my @hex;
push @hex, /(0x[0-9a-f]+)/ig while <>;
$" = ',';
say "@hex";
这是作为Unix过滤器编写的(比硬编码文件名更灵活),因此它从STDIN读取并写入STDOUT。这样称呼:
$ ./extract_hex < your_input_txt
实际上可以进一步简化并丢失中间@hex
数组。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
say join ',', map { /(0x[0-9a-f]+)/ig } <>;
但对许多人来说,这可能有点过于简洁: - )
答案 3 :(得分:0)
以下代码应该执行您想要的操作,其中$ input_filename是数据所在文件的名称/路径,$ output_filename是您要将数据写入的文件的名称/路径。永远记住使用严格;并使用警告;
#! /usr/bin/env perl
# Enforce good programming
use strict;
use warnings;
# 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 $!;
# Array to store the hex data
my @hex_array;
# Loop over each of the lines of the file
while (my $line = <$input_fh>){
# Find all the matches and push them to the array
while ($line =~ m/(0x(?:[0-9]|[A-f])+)/gi){
push @hex_array, $1;
}
}
# Close the file
close $input_fh;
# Open the output file
open my $output_fh, ">", $output_filename or die $!;
# Write the data to the file
print {$output_fh} join(", ", @hex_array);
# Close the file
close $output_fh;
# Exit
exit();