Match over multiple lines perl regular expression - perl

I have a file like this:
01 00 01 14 c0 00 01 10 01 00 00 16 00 00 00 64
00 00 00 65 00 00 01 07 40 00 00 22 68 61 6c 2e
6f 70 65 6e 65 74 2e 63 6f 6d 3b 30 30 30 30 30
30 30 30 32 3b 30 00 00 00 00 01 08 40 00 00 1e
68 61 6c 2e 6f 70 65 6e 65 74 2d 74 65 6c 65 63
6f 6d 2e 6c 61 6e 00 00 00 00 01 28 40 00 00 21
72 65 61 6c 6d 31 2e 6f 70 65 6e 65 74 2d 74 65
6c 65 63 6f 6d 2e 6c 61 6e 00 00 00 00 00 01 25
40 00 00 1e 68 61 6c 2e 6f 70 65 6e 65 74 2d 74
65 6c 65 63 6f 6d 2e 6c 61 6e 00 00 00 00 01 1b
40 00 00 20 72 65 61 6c 6d 2e 6f 70 65 6e 65 74
2d 74 65 6c 65 63 6f 6d 2e 6c 61 6e 00 00 01 02
40 00 00 0c 01 00 00 16 00 00 01 a0 40 00 00 0c
00 00 00 01 00 00 01 9f 40 00 00 0c 00 00 00 00
00 00 01 16 40 00 00 0c 00 00 00 00 00 00 01 bb
40 00 00 28 00 00 01 c2 40 00 00 0c 00 00 00 00
00 00 01 bc 40 00 00 13 31 39 37 37 31 31 31 32
32 33 31 00
I am reading the file and then finding certain octets and replacing them with tags:
while(<FH>){
$line =~ s/(00 00 00 64)/<incr4> /g;
$line =~ s/(00 00 00 65)/<incr4> /g;
$line =~ s/(30 30 30 30 30 32)/<incr6ascii:999999:0>/g;
$line =~ s/(31 31 32 32 33 31)/<incr6ascii:999999:0>/g;
print OUTPUT $line;
}
So for example, 00 00 00 64 would be replaced by the <incr4> tag. This was working fine, but it doesn't seem to able to match over multiple lines any more. For example the pattern 31 31 32 32 33 31 runs over multiple lines, and the regular expression doesn't seem to catch it. I tried using /m /s pattern modifiers to ignore new lines but they didn't match it either. The only way around it I can come up with, is to read the whole file into a string using:
undef $/;
my $whole_file = <FH>;
my $line = $whole_file;
$line =~ s/(00 00 00 64)/<incr4> /g;
$line =~ s/(00 00 00 65)/<incr4> /g;
$line =~ s/(30 30 30 30 30 32)/<incr6ascii:999999:0>/g;
$line =~ s/(31 31 32 32 33 31)/<incr6ascii:999999:0>/g;
print OUTPUT $line;
This works, the tags get inserted correctly, but the structure of the file is radically altered. It is all dumped out on a single line. I would like to retain the structure of the file as it appears here. Any ideas as to how I might do this?
/john

The trick here is to match the class of all space like characters \s:
my $file = do {local (#ARGV, $/) = 'filename.txt'; <>}; # slurp file
my %tr = ( # setup a translation table
'00 00 00 64' => '<incr4>',
'00 00 00 65' => '<incr4>',
'00 30 30 30 30 32' => '<incr6ascii:999999:0>',
'31 31 32 32 33 31' => '<incr6ascii:999999:0>',
);
for (keys %tr) {
my $re = join '\s+' => split; # construct new regex
$file =~ s{($re)}{
$1 =~ /\n/ ? "\n$tr{$_}" : $tr{$_} # if octets contained \n, add \n
}ge # match multiple times, execute the replacement block as perl code
}
print $file;

Related

Why can't I find line with two character with select-line [duplicate]

This question already has answers here:
Powershell - Strange WSL output string encoding
(4 answers)
Closed last month.
To find every line with that "-" from the command wsl --help, theses lines work
wsl --help | Select-String -Pattern "-"
wsl --help | Select-String "-"
Now I try with more complicated pattern: "--"
wsl --help | Select-String -Pattern "--"
wsl --help | Select-String "--"
Nothing is return although there is line with this pattern. Why?
updated:
wsl --help | Select-String "--" -SimpleMatch
doesn't work either
Yep, wsl outputs utf16le or unicode. Even bytes are null.
wsl --help | select -first 1 | format-hex
Label: String (System.String) <09F5DDB6>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 43 00 6F 00 70 00 79 00 72 00 69 00 67 00 68 00 C o p y r i g h
0000000000000010 74 00 20 00 28 00 63 00 29 00 20 00 4D 00 69 00 t ( c ) M i
0000000000000020 63 00 72 00 6F 00 73 00 6F 00 66 00 74 00 20 00 c r o s o f t
0000000000000030 43 00 6F 00 72 00 70 00 6F 00 72 00 61 00 74 00 C o r p o r a t
0000000000000040 69 00 6F 00 6E 00 2E 00 20 00 41 00 6C 00 6C 00 i o n . A l l
0000000000000050 20 00 72 00 69 00 67 00 68 00 74 00 73 00 20 00 r i g h t s
0000000000000060 72 00 65 00 73 00 65 00 72 00 76 00 65 00 64 00 r e s e r v e d
0000000000000070 2E 00 .
"`0" means null. In powershell 7, the matches are highlighted.
wsl --help | Select-String -Pattern "-`0-" | select -first 1
--exec, -e <CommandLine>

How to correct encoding that went wrong

I have a VBscript code that processes utf-8 files.
It works perfectly. Except there is a problem with the source files in that sometimes the input (despite the script is clearly labeled to be used for utf-8 files) is unicode-LE.
This then creates a corrupt output of course. I am putting in a check for the BOM to ensure no Unicode-LE files are opened incorrectly. But I already have files that got corrupted this way.
Is there a way to seamlessly revert the damage? Meaning to read it back "incorrectly" and saving it correctly?
Here is the code:
Private Sub UnicodeToUTF8(ByVal InFName, ByVal OutFName)
Dim strText
With CreateObject("ADODB.Stream")
.Open
.Type = adTypeBinary
.LoadFromFile InFName
.Type = adTypeText
.Charset = "utf-8"
'Read Unicode source file
strText = .ReadText(adReadAll)
'Process file
strText = OffsetTCs(strText)
'Output UTF-8 file
.Position = 0
.SetEOS
.Charset = "utf-8"
.WriteText strText, adWriteChar
.SaveToFile OutFName, adSaveCreateOverWrite
.Close
End With
End Sub
Edit:
I tried this script to save the day, but it reports an error on the file.Write data line. It does show the ASCII content properly in the message box, but not the Chinese characters:
Dim fso, file, data
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("damaged_Test.sub", 1, False, -1)
data = ""
data = file.ReadAll
MsgBox(data)
Set file = fso.OpenTextFile("output.txt", 2, True)
file.Write data
Here is the hex dump of the damaged file:
EF BB BF EF BF BD EF BF BD 5B 00 53 00 63 00 72 00 69 00 70 00 74 00 20 00 49 00 6E 00 66 00 6F 00 5D 00 0D 00 0A 00 3B 00 0D 00 0A 00 54 00 69 00 74 00 6C 00 65 00 3A 00 20 00 20 00 28 00 29 00 0D 00 0A 00 4F 00 72 00 69 00 67 00 69 00 6E 00 61 00 6C 00 20 00 53 00 63 00 72 00 69 00 70 00 74 00 3A 00 20 00 0D 00 0A 00 4F 00 72 00 69 00 67 00 69 00 6E 00 61 00 6C 00 20 00 54 00 69 00 6D 00 69 00 6E 00 67 00 3A 00 20 00 0D 00 0A 00 53 00 63 00 72 00 69 00 70 00 74 00 54 00 79 00 70 00 65 00 3A 00 20 00 76 00 34 00 2E 00 30 00 38 00 0D 00 0A 00 43 00 6F 00 6C 00 6C 00 69 00 73 00 69 00 6F 00 6E 00 73 00 3A 00 20 00 4E 00 6F 00 72 00 6D 00 61 00 6C 00 0D 00 0A 00 50 00 6C 00 61 00 79 00 52 00 65 00 73 00 58 00 3A 00 20 00 31 00 32 00 38 00 30 00 0D 00 0A 00 50 00 6C 00 61 00 79 00 52 00 65 00 73 00 59 00 3A 00 20 00 37 00 32 00 30 00 0D 00 0A 00 50 00 6C 00 61 00 79 00 44 00 65 00 70 00 74 00 68 00 3A 00 20 00 30 00 0D 00 0A 00 54 00 69 00 6D 00 65 00 72 00 3A 00 20 00 31 00 30 00 30 00 2E 00 30 00 30 00 30 00 30 00 0D 00 0A 00 0D 00 0A 00 5B 00 56 00 34 00 20 00 53 00 74 00 79 00 6C 00 65 00 73 00 5D 00 0D 00 0A 00 46 00 6F 00 72 00 6D 00 61 00 74 00 3A 00 20 00 4E 00 61 00 6D 00 65 00 2C 00 20 00 46 00 6F 00 6E 00 74 00 6E 00 61 00 6D 00 65 00 2C 00 20 00 46 00 6F 00 6E 00 74 00 73 00 69 00 7A 00 65 00 2C 00 20 00 50 00 72 00 69 00 6D 00 61 00 72 00 79 00 43 00 6F 00 6C 00 6F 00 75 00 72 00 2C 00 20 00 53 00 65 00 63 00 6F 00 6E 00 64 00 61 00 72 00 79 00 43 00 6F 00 6C 00 6F 00 75 00 72 00 2C 00 20 00 54 00 65 00 72 00 74 00 69 00 61 00 72 00 79 00 43 00 6F 00 6C 00 6F 00 75 00 72 00 2C 00 20 00 42 00 61 00 63 00 6B 00 43 00 6F 00 6C 00 6F 00 75 00 72 00 2C 00 20 00 42 00 6F 00 6C 00 64 00 2C 00 20 00 49 00 74 00 61 00 6C 00 69 00 63 00 2C 00 20 00 42 00 6F 00 72 00 64 00 65 00 72 00 53 00 74 00 79 00 6C 00 65 00 2C 00 20 00 4F 00 75 00 74 00 6C 00 69 00 6E 00 65 00 2C 00 20 00 53 00 68 00 61 00 64 00 6F 00 77 00 2C 00 20 00 41 00 6C 00 69 00 67 00 6E 00 6D 00 65 00 6E 00 74 00 2C 00 20 00 4D 00 61 00 72 00 67 00 69 00 6E 00 4C 00 2C 00 20 00 4D 00 61 00 72 00 67 00 69 00 6E 00 52 00 2C 00 20 00 4D 00 61 00 72 00 67 00 69 00 6E 00 56 00 2C 00 20 00 41 00 6C 00 70 00 68 00 61 00 4C 00 65 00 76 00 65 00 6C 00 2C 00 20 00 45 00 6E 00 63 00 6F 00 64 00 69 00 6E 00 67 00 0D 00 0A 00 53 00 74 00 79 00 6C 00 65 00 3A 00 20 00 50 00 75 00 62 00 6C 00 69 00 63 00 2C 00 44 00 46 00 4B 00 61 00 69 00 53 00 68 00 75 00 20 00 53 00 74 00 64 00 20 00 57 00 35 00 2C 00 35 00 32 00 2C 00 31 00 32 00 35 00 37 00 31 00 38 00 37 00 32 00 2C 00 31 00 32 00 35 00 37 00 31 00 38 00 37 00 32 00 2C 00 31 00 32 00 35 00 37 00 31 00 38 00 37 00 32 00 2C 00 2D 00 32 00 31 00 34 00 37 00 34 00 38 00 33 00 36 00 34 00 30 00 2C 00 2D 00 31 00 2C 00 30 00 2C 00 31 00 2C 00 33 00 2C 00 33 00 2C 00 32 00 2C 00 31 00 32 00 38 00 2C 00 31 00 32 00 38 00 2C 00 37 00 32 00 2C 00 30 00 2C 00 31 00 33 00 36 00 0D 00 0A 00 0D 00 0A 00 5B 00 45 00 76 00 65 00 6E 00 74 00 73 00 5D 00 0D 00 0A 00 46 00 6F 00 72 00 6D 00 61 00 74 00 3A 00 20 00 4D 00 61 00 72 00 6B 00 65 00 64 00 2C 00 20 00 53 00 74 00 61 00 72 00 74 00 2C 00 20 00 45 00 6E 00 64 00 2C 00 20 00 53 00 74 00 79 00 6C 00 65 00 2C 00 20 00 4E 00 61 00 6D 00 65 00 2C 00 20 00 4D 00 61 00 72 00 67 00 69 00 6E 00 4C 00 2C 00 20 00 4D 00 61 00 72 00 67 00 69 00 6E 00 52 00 2C 00 20 00 4D 00 61 00 72 00 67 00 69 00 6E 00 56 00 2C 00 20 00 45 00 66 00 66 00 65 00 63 00 74 00 2C 00 20 00 54 00 65 00 78 00 74 00 0D 00 0A 00 44 00 69 00 61 00 6C 00 6F 00 67 00 75 00 65 00 3A 00 20 00 4D 00 61 00 72 00 6B 00 65 00 64 00 3D 00 30 00 2C 00 30 00 3A 00 30 00 30 00 3A 00 30 00 38 00 2E 00 36 00 30 00 2C 00 30 00 3A 00 30 00 30 00 3A 00 31 00 31 00 2E 00 31 00 30 00 2C 00 50 00 75 00 62 00 6C 00 69 00 63 00 2C 00 30 00 31 00 2C 00 30 00 30 00 30 00 30 00 2C 00 30 00 30 00 30 00 30 00 2C 00 30 00 30 00 30 00 30 00 2C 00 2C 00 EF BF BD 65 74 5E EF BF BD 5F 02 6A 0D 00 0A 00 44 00 69 00 61 00 6C 00 6F 00 67 00 75 00 65 00 3A 00 20 00 4D 00 61 00 72 00 6B 00 65 00 64 00 3D 00 30 00 2C 00 30 00 3A 00 30 00 30 00 3A 00 31 00 31 00 2E 00 32 00 30 00 2C 00 30 00 3A 00 30 00 30 00 3A 00 31 00 35 00 2E 00 31 00 33 00 2C 00 50 00 75 00 62 00 6C 00 69 00 63 00 2C 00 30 00 31 00 2C 00 30 00 30 00 30 00 30 00 2C 00 30 00 30 00 30 00 30 00 2C 00 30 00 30 00 30 00 30 00 2C 00 2C 00 EF BF BD 65 74 5E EF BF BD 5F 02 6A 0D 00 0A

perl seek and remove at varying offsets in binmode

This is my script I am writing.
#usr/bin/perl
use warnings;
open(my $infile, '<', "./file1.bin") or die "Cannot open file1.bin: $!";
binmode($infile);
open(my $outfile, '>', "./extracted data without 00's.bin") or die "Cannot create extracted data without 00's.bin: $!";
binmode($outfile);
local $/; $infile = <STDIN>;
print substr($infile, 0, 0x840, '');
$infile =~ s/\0{16}//;
print $outfile;
I'm loading a binary file in perl.
I have been able to seek and patch at certain offsets, but what I would like to do is, now be able to find any instance of "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" (16 bytes?) and remove it from the file, but no less than 16 bytes. Anything less than that I would want to leave. In some of the files the offset where the 00's start will be at different offsets, but if I am thinking correctly, if I can just search for 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 and remove any instance of it, then it won't matter what offset the 00's are at. I would extract the data first from specific offsets, then search the file and prune 00's from it. I can already extract the specific offsets I need, I just need to open the extracted file and shave off 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
EF 39 77 5B 14 9D E9 1E 94 A9 97 F2 6D E3 68 05
6F 7B 77 BB C4 99 67 B5 C9 71 12 30 9D ED 31 B6
AB 1F 81 66 E1 DD 29 4E 71 8D 54 F5 6C C8 86 0D
5B 72 AF A8 1F 26 DD 05 AF 78 13 EF A5 E0 76 BB
8A 59 9B 20 C5 58 95 7C E0 DB 44 6A EC 7E D0 10
09 42 B1 12 65 80 B3 EC 58 1A 2F 92 B9 32 D9 07
96 DE 32 51 4B 5F 3B 50 9A D1 09 37 F4 6D 7C 01
01 4A A4 24 04 DC 83 08 17 CB 34 2C E5 87 26 C1
35 38 F4 C4 E4 78 FE FC A2 BE 99 48 C9 CA 69 90
33 87 09 A8 27 BA 91 FC 4B 77 FA AB F5 1E 4E C0 I want to leave everything from
F2 78 6E 31 7D 16 3B 53 04 8A C1 A8 4B 70 39 22 <----- here up
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <----- I want to prune everything
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 from here on
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00<---- this IS the end of the file, and
just need to prune these few rows
of 00's
Say that "F2 78 6E" from the example above, is at offset 0x45000 BUT in another file the 00 00's will start at a different offset, how could I code it so the 00 00's would get pruned. In any file that I am opening?
If I need to be more specific, just ask.
Seems like I would peekk so far into the file until I hit a long 00 00 string, then prune any remaining lines. Does that make sense at all?
All I want to do is search the file for any instances of 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 and delete/prune/truncate it. I want to save everything but the 00's
EDIT #2
this did it:
open($infile, '<', './file1') or die "cannot open file1: $!";
binmode $infile;
open($outfile, '>', './file2') or die "cannot open file2: $!";
binmode $outfile;
local $/; $file = <$infile>;
$file =~ s/\0{16}//g;
print $outfile $file;
close ($infile);
close ($outfile);
Thank you ikegami for all your help and patience :)
No such thing as removing from a file. You have to either
copy the file without the undesired bits, or
read the rest of the file, seek back, print over the undesired bits, then truncate the file.
I went with option 1.
$ perl -e'
binmode STDIN;
binmode STDOUT;
local $/; $file = <STDIN>;
$file =~ s/\0{16}//;
print $file;
' <file.in >file.out
I'm loading the entire file into memory. Either option can be done in chunks, but it complicates things because your NULs could span two chunks.
In a poorly phrased update, you seem to have asked to avoid changes in the first 0x840 bytes. Two solutions:
$ perl -e'
binmode STDIN;
binmode STDOUT;
local $/; $file = <STDIN>;
substr($file, 0x840) =~ s/\0{16}//;
print $file;
' <file.in >file.out
$ perl -e'
binmode STDIN;
binmode STDOUT;
local $/; $file = <STDIN>;
print substr($file, 0, 0x840, '');
$file =~ s/\0{16}//;
print $file;
' <file.in >file.out

Why isn't my pack() packing my data?

What I am trying to do is simple. Here below.
my #arr = split(/\s+/,"50 00 9F 11 00 28 82 48 21 84 BC 00 01 02 01 00 09 01 38 00 23 05 08 01 01 02 00 00 18 00 50 05 00 00 00 00 00 00 00 00 02 00 0C FE CE 00 0F 00 FD FF 2D 00 00 00 00 00 04 01 0C FE");
my #hexData;
my $i=0;
foreach my $elem(#arr){
$hexData[$i]=hex($elem);
$i++;
}
my $data= pack ('C', #hexData);
print $data;
And its not working :( Would you please help?
TLP's solution is quite right, but pack actually has the ability to deal with hex.
my $data = "50 00 9F 11 00 28 82 48 21 84 BC 00 01 02 01 00 09 01 38 00 23 05 08 01 01 02 00 00 18 00 50 05 00 00 00 00 00 00 00 00 02 00 0C FE CE 00 0F 00 FD FF 2D 00 00 00 00 00 04 01 0C FE";
$data =~ tr/ //d; # Remove the spaces
print pack "H*", $data;
does the whole thing without the intermediate array.
I'm not terribly familiar with the pack function, but it seems to me that your template expects only one value.
Perhaps you should try
my $data = pack ('C*', #hexData);
And while you are at it, upgrade your code to something more perlish:
my #arr = qw(50 00 9F 11 00 28 82 48 21 84 BC 00 01 02 01 00 09 01 38 00 23 05
08 01 01 02 00 00 18 00 50 05 00 00 00 00 00 00 00 00 02 00 0C FE
CE 00 0F 00 FD FF 2D 00 00 00 00 00 04 01 0C FE);
my #hexData;
foreach my $elem (#arr) {
push #hexData, hex($elem);
}
my $data = pack ('C*', #hexData);
print $data;
Or even:
my $data = pack("C*", map(hex, #arr));

Extracting "plaintext" header from HEX file using Perl

Have a file that appears to have plaintext headers in them that I would like to extract and convert to plaintext.
Using HEXedit, this is what I'm seeing, which is in a file:
3a40 - 31 65 33 38 00 00 00 00 00 00 00 00 00 00 00 00 - 1e38............
3a50 - 00 00 00 00 00 00 00 00 00 00 0a 00 74 00 65 00 - ............t.e.
3a60 - 78 00 74 00 2f 00 61 00 73 00 63 00 69 00 69 00 - x.t./.a.s.c.i.i.
3a70 - 00 00 18 00 61 00 66 00 66 00 79 00 6d 00 65 00 - ....a.f.f.y.m.e
3a80 - 74 00 72 00 69 00 78 00 2d 00 61 00 72 00 72 00 - t.r.i.x.-.a.r.r
3a90 - 61 00 79 00 2d 00 62 00 61 00 72 00 63 00 6f 00 - a.y.-.b.a.r.c.o.
3aa0 - 64 00 65 00 00 00 64 00 40 00 35 00 32 00 30 00 - d.e...d.#.5.2.0.
3ab0 - 38 00 32 00 36 00 30 00 30 00 39 00 31 00 30 00 - 8.2.6.0.0.9.1.0.
3ac0 - 37 00 30 00 36 00 31 00 31 00 31 00 38 00 31 00 - 7.0.6.1.1.1.8.1.
3ad0 - 31 00 34 00 31 00 32 00 31 00 33 00 34 00 35 00 - 1.4.1.2.1.3.4.5.
3ae0 - 35 00 30 00 39 00 38 00 39 00 00 00 00 00 00 00 - 5.0.9.8.9.......
3af0 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - ................
3b00 - 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0a 00 - ................
and this is the output I'd like to get:
text/ascii affymetrix-array-barcode d#52082600910706111811412134550989
Try with the iconv command. Something like this should work:
tail -c +6 input.txt | iconv -f UTF16 -t ASCII >output.txt
Then split on the null bytes.
Granted, I'm no wiz, but this does the job if all your files look very similar to the one you just posted:
use strict;
open FILE, 'file.dat';
binmode FILE;
my ($chunk, $buf, $n);
seek FILE, 28, 0;
while (($n=read FILE, $chunk, 16)) { $buf .= $chunk; }
my #s=split(/\0\0/, $buf, 4);
print "$s[0] $s[1] $s[2]\n";
close (FILE);
A perl solution might be interesting, but wouldn't the unix strings command give you the plaintext portion of the file?