Shell-Scripting Cheat Sheet


shflags - portable getops

If you ever need to port between different Unix derivates use shflags a Google library providing standard parameter handling. Example:
source shflags

DEFINE_string 'value' '0' 'an example value to pass with default value "0"' 'v'

FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"

echo "${FLAGS_value}!"

Advanced use of sed

Grouping with sed

Always use single quotes!
sed 's/^.*\(pattern\).*/\1/'

Single Quoting Single Quotes

If you want to do extraction and need a pattern based on single quotes use \x27 instead of trying to insert a single quote. For example:
sed 's/.*var=\x27\([^\x27]*\)\x27.*/\1/'
to extract "some string" from "var='some string'". Or if you don't know about the quoting, but know there are quotes
sed 's/.*var=.\([^"\x27]*\)..*/\1/'

Conditional Replace with sed

sed '/conditional pattern/{s/pattern/replacement/g}'

Removing Newlines with sed

The only way to remove new line is this:
sed ':a;N;$!ba;s/\n//g' file
Check out this explanation if you want to know why.

Selecting Blocks

sed '/first line/,/last line/!d' file

Advanced Use of awk

Negative Indizes

awk '{print $(NF - 1)}'

Date Handling

Convert Date To Unix Timestamp

date -d "$date" +%s
Note that this only works for American style dates. European "25.06.2014" like dates are not supported. The simple solution is to convert them first to "2014-06-25" for example with
sed 's/\([0-9]*\)\.\([0-9]*\)\.([0-9]*\)/\3-\2-\1/'

Convert From Unix Timestamp

date -d "1970-01-01 1234567890 sec GMT"

Calculate Last Day of Month

Found here:
cal $(date "+%M %y") | grep -v ^$ | tail -1 | sed 's/^.* \([0-9]*\)$/\1/'

Other Topics