Skip to content

Instantly share code, notes, and snippets.

Last active November 1, 2016 10:25
What would you like to do?
Awk and Sed cheetsheet

awk & sed cheetsheet

Extract lines between start and end

between() { sed -n "/^$1/,/^$2/p" | sed '1d;$d'; }


awk '/^START=A$/,/^END$/ { print }’ data

Removes xml tags

alias noxml="sed -e 's/<[^>]*>//g'"

Delete quotes

alias delete-quotes="tr -d '\"'"

Gets the n line

Second line:

sed -n 2p

Adds prefix to each line

awk '$0="prefix"$0'

Reverse a string

echo "abc\n123" | rev

Reverse the order of lines

tail -r
sed '1!G;h;$!d'

Replace space by incremental number

perl -p -e 's/[.]{4}([.]+)/"".sprintf("%04d", ++$i)."$1"/ge'

Capitalize one word

perl -pe 's/\b(.+)/\u$1/'

Dasherize string

tr '[:upper:] ' '[:lower:]-'

Split a json list into separate json files

i=0;for f in `cat list.json | jq -r '.[].id'` ; do cat list.json | jq ".[$((i++))]" > $f.json; done

Sum column B,C,D in csv file

awk -F ',' '{ b+=$2;c+=$3;d+=$4;} END {print b,c,d}'

Concatenates letters

python -c 'z = [a+b  for a in "abc" for b in "zxc"]; print z'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment