perl onliners, vim and iso2709
tips of this post are cool even if you don’t deal with iso2709.
i wrote a post to explain how sweet it can be to use vim to deal with stdout. Now i want to see the content of a iso2709 file.
as the record separator of iso2709 is « \x1d » but vim
- can’t define a separator different from the standard ones ( combining « \r » and « \n » )
- is very bad to manage very long lines (what an iso2709 file actually is for it)
to navigate in your file, it would be cool to append a « \n » just after « \1xe » (field separator) but with no side effect on the original file. Perl do it easily by mixing -E -0 and -n.
-e flag is to execute perl code ( -E to include perl 5.10 features )
perl -E 'say "hello"'
-n make this code executed for each lines of a read file ( $_ : line content, $. : line number )
cat /etc/hosts # can be written perl -nEprint /etc/hosts
grep -n localhost /etc/hosts # can be written perl -nE '/localhost/ and print "$. : $_"' /etc/hosts
-0 can change the record separator so
perl -0x1e -nEsay biblio.iso2709
read the file field by field ( -0×1xe ) and append a « \n » (say does it) at the end of each one. Pipe it to vi and you’ll have a fast way to walk through your file.
perl -0x1e -nEsay biblio.iso2709 | vim -
also, set dy (display) to uhex to see the hex codes of the separators ( « \x1d » for the record, « \x1e » for field, « \x1f » for subfield)
:set dy=uhex
also note that perl -n accepts some sed style range
# just see the records 10 to 23 perl -0x1d -nE' 10..23 and print' biblio.iso2709 | vim -
# just see from record 10 to the record that contains the word 'plan9' perl -0x1d -nE' 10../plan9/ and print' biblio.iso2709 | vim -
and the awk style BEGIN and END blocks
# count the number of records
perl -0x1d -nE 'END { say $. }' biblio.iso2709