Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created October 5, 2011 14:23
Show Gist options
  • Save thinkerbot/1264548 to your computer and use it in GitHub Desktop.
Save thinkerbot/1264548 to your computer and use it in GitHub Desktop.
shell tricks
h2d () {
for hex in $*; do
HEX=$(echo "$hex" | tr [:lower:] [:upper:])
echo "ibase=16;$HEX" | bc
done;
}
d2h () {
for DEC in $*; do
echo "obase=16;$DEC" | bc
done;
}
# Watch traffic coming back from a server on localhost:3000
tcpdump -s 4096 -Ani lo0 src port 3000
# list processes with PGID
ps j
# signal all processes in current PGID
kill [SIG] 0
# check to see if a url returns 200 in the response header, ex 'HTTP/1.1 200 OK'
curl -sI URL | head -n 1 | grep -q ' 200 '
# perform a sed operation and only print the results of matching lines
# note that on OSX sed does not like multiple commands separated by
# semicolons - so separate them with -e for portability or use a long
# form.
sed -e 's/abc/xyz/' -e 't p' -e 'd' -e ':p' << DOC
abc
abcdef
defabc
def
DOC
# or just...
sed -ne 's/abc/xyz/p' << DOC
abc
abcdef
defabc
def
DOC
sed -e 's/abc/xyz/
t p
d
:p
' <<DOC
abc
abcdef
defabc
def
DOC
# Paste tables together like a bad-ass
cat > one.txt <<DOC
abc
pqr
xyz
DOC
cat > two.txt <<DOC
123
456
789
DOC
paste one.txt two.txt
# abc 123
# pqr 456
# xyz 789
cat one.txt two.txt | paste - - -
# abc pqr xyz
# 123 456 789
# sed character classes
echo 'documents/421450709.xml' | sed -e 's/[[:digit:]]\{1,\}/ID/'
# curl with IPv6 (http://tech.bluesmoon.info/2011/12/using-curl-with-ipv6-addresses.html)
curl "http://\[2600:xxx:yyy::zzz\]/page.html"
# remove whitespace lines, in place, for all files in pwd
#
# sed -e 's/ \{1,\}$//' -i '' *
#
# but do something like this for goodness sake!
find lib -type f | grep -v -E '^.git' | xargs sed -e 's/ \{1,\}$//' -i ''
# prefix lines matching a pattern
sed -e 's/.*b.*/. &/' -e 't z' -e 's/./x &/' -e ':z' <<DOC
abc
bcd
cde
DOC
# partition a file by regexp
sed -e '/b/ {
w one
d
}' > two <<DOC
abc
bcd
cde
DOC
# print a stream of entries by a pattern
# * branch to show for new entries
# * append the line to the hold buffer if not a new entry
# * branch to the end of the file unless the last line (ie show on the last line)
#
# then in show x to start a new entry in the hold buffer and retrieve the entry
# currently in it. then print the entry if it matches some pattern
sed -ne '
/==/ b show
H
$ !b
:show
x
/d/p
' << DOC
==
abc
==
bcd
==
cde
DOC
# man pages in mate
# modified from: http://jasonrudolph.com/blog/2008/03/14/manning-up-textmate-meets-man-pages/
man ab | col -bx | mate
# multiple heredocs
cat <<DOC | while read line; do echo $line; cat <<DOX; done
abc
xyz
DOC
123
456
DOX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment