Mid-week nightly Perl useless magic and fun!

Last night, I was sitting at my desk, watching text scroll by across an IRC session open in a terminal screen set in black, when my eyes came to halt at Pixelized on ##slackware on FreeNode soliciting help with a particular use of grep. To cut the rather overly technical story short, what Pixelized wanted to do was grok all text in a file between the keywords “X1” and “.bat”, inclusive. Being a long time Perl guy, I quickly threw two Perl one-liners that did pretty much what he had asked for. Both forms work perfectly accurately for my selected data set, but appeared to be slightly misbehaving against Pixelized data set. It was late, and I was already drooping with sleep, carrying a cracking headache, so I never bothered asking him for the data set. Anyhow, I thought I would share the two Perl one-liners just for the heck of it here.

perl -e 'while (<>) { if (/X1/ .. /\.bat/) { s/.*(X1.*)/\1/; s/(.*\.bat).*/\1/; print $_; } }' dump.txt

perl -e '$c = 0; while (<>) { if (/X1/) { $c = 1; s/.*(X1.*)/\1/; } if (/\.bat/) { s/(.*\.bat).*/\1/; print $_; last; } print $_ if $c; }' dump.txt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s