Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read fixed-width records in Perl using the -0 option?

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?

like image 609
wu-lee Avatar asked Dec 05 '18 11:12

wu-lee


1 Answers

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 (<>) { ... }'
like image 125
ikegami Avatar answered Sep 28 '22 00:09

ikegami