Skip to content

Instantly share code, notes, and snippets.

@olih
Last active December 21, 2023 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save olih/74644a2e66b2344b9abaa07e1c94d252 to your computer and use it in GitHub Desktop.
Save olih/74644a2e66b2344b9abaa07e1c94d252 to your computer and use it in GitHub Desktop.
Awk and Sed cheetsheet

awk & sed cheetsheet

Extract lines between start and end

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

Also:

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
tac
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