sed and awk — editing and fields in a text stream
cheat sheet · Ubuntu 24.04 · GNU sed 4.9 · GNU awk 5.2 https://lab.dimsanych.ru/en/wiki/sed-awk/Cheat sheet
-
sed -n 's/old/new/p' app.inipreview the changed lines -
sed -i.bak 's/old/new/g' app.iniedit, keep app.ini.bak -
sed -n '5,10p' filelines 5 to 10 -
sed '/^ *#/d; /^$/d' filedrop comments and empty lines -
sed '/^ *#/! s/old/new/' filereplace outside comments -
awk '{print $1}' access.logthe first field of every line -
awk -F: '$3 >= 1000 {print $1}' /etc/passwdfilter by a field -
awk '{s += $4} END {print s}' log.txtsum of the fourth field
Options
The main options of both programs are in the table below. The full lists are in
man sed and man gawk.