So I know you can write Perl one-liners which read records with the non-default record separator with options like
perl -064 -ne '#... delimited by @'
Or the entire file in one line:
perl -0777 -ne '#... file at once'
I also know if you programatically set the record separator $\
to a reference to a number you can read fixed-width records.
perl -ne '$/ = \10; #... 10 chars at a time'
But what I can't find is any recipe for reading fixed width records using the -0
option. Is this possible?
There's no command-line switch.
From perlrun,
-0[octal/hexadecimal]
specifies the input record separator (
$/
) as an octal or hexadecimal number. If there are no digits, the null character is the separator. Other switches may precede or follow the digits. For example, if you have a version of find which can print filenames terminated by the null character, you can say this:find . -name '*.orig' -print0 | perl -n0e unlink
The special value 00 will cause Perl to slurp files in paragraph mode. Any value 0400 or above will cause Perl to slurp files whole, but by convention the value 0777 is the one normally used for this purpose.
You can also specify the separator character using hexadecimal notation: -0xHHH..., where the
H
are valid hexadecimal digits. Unlike the octal form, this one may be used to specify any Unicode character, even those beyond 0xFF. So if you really want a record separator of 0777, specify it as -0x1FF. (This means that you cannot use the -x option with a directory name that consists of hexadecimal digits, or else Perl will think you have specified a hex number to -0.)
You may, of course, use the following:
perl -ne'BEGIN { $/ = \10 } ...'
or
perl -e'$/ = \10; while (<>) { ... }'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With