Archive

Archive for April, 2009

Converting Null

April 24th, 2009 2 comments

Simple shell script fix this week – convert all newlines to null characters.

Since most Linux shells have a major problem with doing this in a sensible way, the following (on my box called simply ‘0’) takes any input (either from files or via a pipe) and converts the characters.

1
2
3
4
5
6
7
#!/usr/bin/perl
# From: http://hash-bang.net/2009/04/converting-nullconverting-null/
# Author: Matt Carter <m@ttcarter.com>
while (<>) {
	s/\n/\x00/g;
	print;
}

So now commands like:

find -type f | 0 | xargs -0 | mplayer

work perfectly (in the above case to play all files in directories recursively.

You may well ask why I don’t use the simple ‘-print0′ argument for the find program – because it doesn’t work when using later pipes.

For example if i wanted to sort the above, ‘sort’ would see the null symbol as a regular character.

So ‘0’ works nicely in this case:

find -type f | 0 | sort | xargs -0 | mplayer

Or even:

find -type f | 0 | shuffle | xargs -0 | mplayer

To play all files recursively, in random order. See the article on shuffle for the source code of that filter.

Categories: Fixes, HowTo's Tags: , , ,

Shuffling shell input

April 17th, 2009 2 comments

While the Fisher-Yates shuffle sounds like 1920’s dance move, it is the standard method for shuffling larger arrays of data.

The below operates like a simple Unix filter taking an array of data (separated by newlines) and returning a quickly sorted output.

?Download shuffle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/perl
# Usage: cat file | shuffle
# From: http://hash-bang.net/2009/04/shuffling-shell-inputshuffling-shell-input/
# Return a shuffled version of the input stream
# Author: Matt Carter <m@ttcarter.com>
 
sub fisher_yates_shuffle {
	my $array = shift;
	my $i;
	for ($i = @$array; --$i; ) {
		my $j = int rand ($i+1);
		next if $i == $j;
		@$array[$i,$j] = @$array[$j,$i];
	}
}
 
while(<>) {
	chomp;
	push @input, $_;
}
 
&fisher_yates_shuffle(\@input);
print join("\n",@input);

For example:

ls | shuffle

Returns a randomized list of files in the current directory.

Categories: HowTo's Tags: ,