[clug] Thursday afternoon Perl/bash golf time

Eyal Lebedinsky eyal at eyal.emu.id.au
Thu Jun 11 01:16:13 MDT 2015


On 11/06/15 16:26, Andrew Janke wrote:
> I have a perl script, one of its jobs is to concatenate a large number
> of (binary) files together.
>
> I cheat and use the shell
>
> # create rawfile
> @args = ('cat', @catfiles, '>', "$opt{'tmpdir'}/all.raw");
> &do_cmd(join(' ', @args));
>
> (from: http://git.io/vIM2p)
>
> Of course now someone tried to use it with a large number of input
> files and it failed. So what to do?
>
> 1) for($i=0; $i<$#catfiles; $i+=100){
>           system("cat @catfiles[$i..$i+99] /tmp/cat-$i");
>           }
>
>       # cat the bits.
>
>
> 2) Do it in bash
>
>       for i in infiles.txt; do cat $i >> result; done

Assuming infiles.txt is a list of file names to cat then
	while read f ; do
		cat "$f"
	done <infiles.txt >result

Or if you can otherwise 'find' the files
	find [selection] -exec cat "{}" \; >result

cheers

> 3) Do it in perl
>
>
> 4) Something far more clever.
>
> Suggestions welcome, my brain is slowing.
>
> ta
>
>
>
> a
>

-- 
Eyal Lebedinsky (eyal at eyal.emu.id.au)


More information about the linux mailing list