Awk and Sed

awk and sed are text manipulation programs. You can use them for example to replace strings:

echo image.jpg | sed 's/\.jpg/.png/'
image.png

or change order of strings:

echo "hello world" | awk '{print $2, $1}'
world hello

awk and sed are harder to learn than some other basic linux cli tools because they contain their own small programming language.

awk basics

awk transforms lines of text (stdin) into any other text, using a set of instructions (called the awk program):

awk program input-files

a awk program can also contain one or more `actions`, for example calculating values or printing text. These `actions` run only when an input matches a `pattern`:
`pattern {action}`

typical patterns include:

  • `BEGIN` runs once before the awk processes any input
  • `END` run once after awk has processed all the input
  • A Regex surrounded by forward slashes, example: `/^[A-Z]/`
  • other awk specific expressions, example: `FNR>5` tells awk to skip the first five lines of input


A `pattern` with no `action` runs the default action `{print}` .
awk can also perform calculations such as these:

seq 1 100 | awk '{s+=$1} END {print s}'
5050

(sum numbers 1 to 100)

sed basics

sed like awk can be used to transform text from stdin to any other text using instructions called `sed scripts`:

sed script input-files

the most common use case for sed is text replacement, like in this example:

echo "Windows eats Linux for breakfast" | sed s/Windows/Linux/g | sed s/Linux/Windows/2