[clug] Thursday afternoon Perl/bash golf time
Carlo Hamalainen
carlo at carlo-hamalainen.net
Thu Jun 11 18:31:56 MDT 2015
On 11/06/15 20:06, Andrew Janke wrote:
> Once day I'll get around to converting all these things to python, but
> for now I'm a happy Perl Dinosaur.
I would have recommended the Python library "fileinput" but it is
strongly oriented towards iterating over lines in a collection of files,
so joining binary files won't work.
Instead, read chunks from the input files in a list and write to the
output file.
https://gist.github.com/carlohamalainen/14aedb34be4f862334a4
It uses Python's open() and read() so no dramas with files or
directories with spaces. This is probably the one thing that makes me
write shell-type things in Python instead of bash. No escaping.
$ find dir\ with\ spaces/ | head
dir with spaces/
dir with spaces/split file with spaces too oh dear 89
dir with spaces/split file with spaces too oh dear 75
dir with spaces/split file with spaces too oh dear 27
dir with spaces/split file with spaces too oh dear 90
dir with spaces/split file with spaces too oh dear 49
dir with spaces/split file with spaces too oh dear 04
dir with spaces/split file with spaces too oh dear 02
dir with spaces/split file with spaces too oh dear 36
dir with spaces/split file with spaces too oh dear 64
$ find dir\ with\ spaces/ -type f | sort > files.txt
$ ./join.py files.txt 'joined with spaces.iso'
$ md5sum Downloads/ubuntu-15.04-desktop-amd64.iso 'joined with spaces.iso'
53c869eba8686007239a650d903847fd Downloads/ubuntu-15.04-desktop-amd64.iso
53c869eba8686007239a650d903847fd joined with spaces.iso
--
Carlo Hamalainen
http://carlo-hamalainen.net
More information about the linux
mailing list