awk vs. sed

Both awk and sed are command-line utilities that are used to transform text.

Let’s take a look at the major differences between them.

awk

  • It works with delimited fields on a per-line basis.

  • It is mainly used to search and print data from text files.

  • It has advanced programming constructs like for, while, do-while, and multi-dimensional arrays​.

  • It is more robust than sed.

  • Writing mathematical expressions in awk is easy.

sed

  • It works with characters on a per-line basis.

  • It is primarily used to filter and modify text files.

  • It has fairly limited programming constructs like pattern matching and simple conditionals​.

  • It is less robust than awk.

  • Writing mathematical expressions in sed is hard.

Commands

awk

The following command prints the first and second columns of myfile.txt with a ​space in between.

awk ‘{print $1 “ ”  $2}’ myfile.txt

sed

The following command replaces “helo” with “hello” in myfile.txt.

sed ‘s/helo/hello/’ myfile.txt
Copyright ©2024 Educative, Inc. All rights reserved