Where to start First steps track · 15 stages
Linux Labs

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.ini preview the changed lines
  • sed -i.bak 's/old/new/g' app.ini edit, keep app.ini.bak
  • sed -n '5,10p' file lines 5 to 10
  • sed '/^ *#/d; /^$/d' file drop comments and empty lines
  • sed '/^ *#/! s/old/new/' file replace outside comments
  • awk '{print $1}' access.log the first field of every line
  • awk -F: '$3 >= 1000 {print $1}' /etc/passwd filter by a field
  • awk '{s += $4} END {print s}' log.txt sum 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.

Loading…