Search this site


Page 1 of 2  [next]

Metadata

Articles

Projects

Presentations

Strip XML comments with sed

sed -ne '/<!--/ { :c; /-->/! { N; b c; }; /-->/s/<!--.*-->//g }; /^  *$/!p;'
You might consider stripping blanklines and/or filtering through xmllint --format to make the xml pretty printed.

Site move soon, perhaps.

I'll be moving this site to my new domain as soon as I get things setup there. I won't bother disclosing the name at this point, seeing as how there's nothing there.

As a hint, however, I will say I came up with the name using this perl one-liner:

perl -Mre=eval -ne 'print if m/^s(.)(??{"[^$1]+$1"}){2}$/' words
(are your eyes bleeding, yet?)

That regex will find all valid sed substitution expressions in the file 'words' (freebsd's dictionary file). So, anything printed by that perl code will be valid as a sed command. ie: "streetlet" is valid as

% echo "ree" | sed -e "streetlet"
le
I learned a new word in the process of running this script. syzygy

Nothing quite like using perl to choose a domain name for you. Yeah, I'm a dork.

precedence sorting with perl's sort()

This isn't terribly complicated, but it may be useful to you. Mostly I'm putting it here for my own future reference.
sort { $users{$b} <=> $users{$a} || $a cmp $b } keys(%users)
This will sort first numerically by the values stored in %users and then by the keys of %users. This means that the return value of sort is the keys sorted by data values first, and then alphabetically when both data values are equal.

grep -R

Boredom + lacking grep with -R...

find ./ -type f | xapply -f 'grep "SOMESTRING" "%1" | sed -e "s!^!%1: !"' -

perl makes addition fun!

I like calculators...

perl -lne 'print $a+=$_'

happy perl obfuscation #425234569807


perl -e '$foo = "hello there"; $foo =~ /(?{chop}){4}/; print "$foo\n"'

Nuff said.

You may have to look up what (?{...}) does in perldoc perlre

boredom + apache

Here's a silly oneliner that'll attempt to calculate per-file usage from an apache log.

awk '{print $7}' - | perl -e 'while (<>) { chomp; s!^/([^/]+)!/.html_pages!; 
$u = $1; next if ($u !~ s/^~//); @a = getpwuid(getpwnam($u)); $_ = $a[7] . $_;
$f{$_} += (stat($_))[7] }; map { print $f{$_} . " $_\n" if ($f{$_}) } keys(%f)'

Reads the log data from stdin. Output is unsorted. I'd make it smaller but I'm lazy and tired.

Yet another random number generator

I was bored...

dd if=/dev/urandom bs=1 count=30 2> /dev/null | \
perl -e 'read(STDIN,$foo,5); print unpack("J",$foo) % 6 . "\n";'

Removing duplicates from arrays in Perl

After needing to do this in a project of mine, I went googling and found, as expected, a wide variety of solutions. Solutions ranged from using map, foreach, grep, etc.. All using things like a temporary hash to count instances and ensure uniqueness - but I remembered that hashes have unique keys and that hashes are often treated the same way as arrays in perl, so my solution is as follows:

my %foo;  #Temp var
my @a = qw (hello there hello how are you today there what now hello hello hello);

%foo = @a;
@a = keys(%foo);

I also have a one-liner version:

# Assumedly, @a is already defined and has stuff in it, perhaps...

@a = do { my %foo = @a; keys(%foo) };

<3 xapply.

xapply 'ping -t 1 -c 1 %1 > /dev/null 2>&1; A=$?; echo -n "%1 - "; [ $A -eq 0 ] && echo "ONLINE" || echo "***DOWN***"' `cat hostlist`

Outputs:

project1 - ONLINE
mokey - ***DOWN***
boober - ONLINE
wimbley - ONLINE
doozer - ONLINE
felix - ONLINE
red - ***DOWN***
sprocket - ONLINE
henchy - ONLINE
falcon - ONLINE
talon - ONLINE
junior - ONLINE
doc - ONLINE
eagle - ***DOWN***