Skip to content

Instantly share code, notes, and snippets.

View awwsmm's full-sized avatar

Andrew awwsmm

View GitHub Profile
@awwsmm
awwsmm / array_contains.sh
Last active June 22, 2019 23:05
Merge all Java-like "classpath"s in arguments into a single valid classpath, keep other arguments intact
#!/usr/bin/env bash
#-------------------------------------------------------------------------------
#
# array_contains - returns 0 if array contains the specified value, else 1
#
# $1 : term to search for
# $2+ : array to inspect
#
# sources:
@awwsmm
awwsmm / printargs.sh
Last active June 23, 2019 07:42
Easily print arguments passed to a `bash` script
#!/usr/bin/env bash
#-------------------------------------------------------------------------------
#
# print_args - easily inspect arguments passed to a bash script
#
# sources:
# https://unix.stackexchange.com/a/332126/183920
#
#-------------------------------------------------------------------------------
@awwsmm
awwsmm / allvars.sh
Last active June 6, 2019 14:25
Get all environment and shell variables
#!/bin/bash
set | grep "^[A-Z]" | sed -e 's/[[:space:]].*//' -e 's/=.*//'
# tested on bash, ksh, tcsh, zsh
# Sources:
# https://askubuntu.com/a/275972/360539
# https://stackoverflow.com/a/20348145/2925434
# https://www.tutorialspoint.com/unix/unix-regular-expressions.htm
@awwsmm
awwsmm / FixedWidthFile.java
Created March 1, 2019 12:29
Infers the column widths of a fixed-width flat text file and parses its lines into tokens
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
@awwsmm
awwsmm / JavadocPaths.java
Created December 17, 2018 15:12
Filesystem paths (Windows and Linux) in Javadoc
/**
* Javadoc for filesystem path edge cases.
*
* <hr style="height:1px;border:0;border-top:1px solid #CCC">
*
* <h2><strong>Edge Case 0: Inline Windows Filesystem Paths</strong></h2>
*
* <p>Here is an inline Windows fs path with backslashes:
* <code>"C:&#92;Users&#92;user&#92;file.txt"</code>. You must replace the
* {@code '\'} characters with {@code "&#92;"} in the source code, otherwise
@awwsmm
awwsmm / GetDerbyDBO.java
Created December 9, 2018 21:15
Determine / find the database owner ("dbo") and current user in an embedded Derby (JavaDB) database
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
/**
* Example class, showing how to find owner of an embedded Derby database.
*
@awwsmm
awwsmm / PNode.java
Created November 12, 2018 15:42
Ready-to-use node data structure for Java with examples in jshell
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@awwsmm
awwsmm / ParseCLArgs.java
Created November 11, 2018 22:08
Parse command-line arguments and categorise them as "arguments", "flags", "long flags", or "other". Return an easy-to-use List<Entry>.
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public class ParseCLArgs {
// try with
// $ java ParseCLArgs arg -short anotherArg --long - -- ---toolong
@awwsmm
awwsmm / ExpandPath.java
Last active May 6, 2020 05:24
Expand paths with globs '*', recursive double-globs '**', leading ~ or ~user home directories, or $ENV %VARS% (even on Windows!) to lists of files
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
@awwsmm
awwsmm / SplitXSV.java
Created November 10, 2018 12:00
Splits a line of sentinel-separated values into tokens
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class SplitXSV {
public static void main (String[] args) {