Skip to content

Instantly share code, notes, and snippets.

@mshock
mshock / switchboard.pl
Created June 14, 2013 13:47
golf - switchboard - 141 characters
#!perl -n
@z=/[^\n]/sg if/A/;
while(1) {
$i=index$_,'X';
($p,$t)=$i%2?($i-1,$i+1):($i-2,$i+2);
($z[$p],$z[$t])=($z[$t],$z[$p]);
s/X/ /
}
}{print@z
__DATA__
@mshock
mshock / switch_simple.pl
Created April 29, 2013 21:26
most basic CLI arg parsing for Perl scripts which use warnings and/or strict: perl -s script.pl -test
#! perl -ws
use strict;
our $test ||= '';
print $test;
@mshock
mshock / list_vols.bat
Created April 24, 2013 16:55
Windows cmd list disk volumes metadata
echo list volume | diskpart
@mshock
mshock / remove_dups.pl
Created April 22, 2013 04:49
remove duplicate records from tab delimited text file and log duplicates per file in directory TODO make dynamic
#! perl -w
use strict;
opendir( DIR, '.' );
my @files = readdir(DIR);
closedir DIR;
for my $file (@files) {
next unless $file =~ m/.*upd$/i;
print "$file...";
@mshock
mshock / julian2ymd.pl
Created April 21, 2013 00:36
Perl sub converts a Julian date number (JDN) into its Gregorian year, month, day, components not 100% precise for JDN too far in the past, but seems good for modern dates (algorithm & constants from Wikipedia)
# convert julian date number into Gregorian year, month, day parts
# not precise too far in the past... 1580-something?
sub jdn2greg {
my ($jdate) = @_;
# make sure formatted like JDN (DDDDDDD[.D*])
return if $jdate !~ m/^\d{7}\.?(\d*)$/;
# TODO add support for time here based on fractional day RH side of decimal place
# define conversion constants
my $y = 4716;
@mshock
mshock / julianify.pl
Created April 20, 2013 23:57
takes Gregorian (Y,M,D) and converts to Julian date number using purely arithmetic (from algorithm on wikipedia)
# calculate JDN from YMD
sub julianify {
my ( $year, $month, $day ) = @_;
my $a = int( ( 14 - $month ) / 12 );
my $y = $year + 4800 - $a;
my $m = $month + 12 * $a - 3;
return
$day
+ int( ( 153 * $m + 2 ) / 5 )
@mshock
mshock / schtasks.bat
Created April 18, 2013 15:23
Windows task manager: change created tasks using CLI
SCHTASKS /change /?
@mshock
mshock / nest_new.jss
Created March 19, 2013 18:11
Google script to nest Gmail labels on the fly as they are archived (I found that nesting labels in the inbox looks cluttered, the intended purpose is for searching/filtering archived mail anyway) requires a filter that applies a "New" label to all incoming e-mail this label can then be hidden
function nest_new() {
var new_label = GmailApp.getUserLabelByName('New');
var regex_nested = new RegExp(/^[\w\s]+\//);
var new_threads = new_label.getThreads();
for (var i = 0; i < new_threads.length; i++) {
Logger.log(1);
var thread_labels = new_threads[i].getLabels();
var inbox_flag = 0;
for (var k = 0; k < thread_labels.length; k++) {
if (thread_labels[j].getName() == 'Inbox') {
@mshock
mshock / kill_tasks.bat
Created March 14, 2013 21:02
force kill application by user in Windows
taskkill /fi "USERNAME eq [domain]/username" /im image_name /f
@mshock
mshock / rsync.bash
Created March 13, 2013 23:45
basic rsync syntax for a backup
#! bash
rsync -vvan --exclude-from=excludes.txt --ignore-existing source/path target/path
# extra verbose and dryrun, -vvn
# archive mode, -a
# create excludes.txt with skip patterns (and/or inversely, --include-from)
# don't overwrite files, --ignore-existing