Difference Between sed and awk

The main difference between sed and awk is that sed is a command utility that works with streams of characters for searching, filtering and text processing while awk more powerful and robust than sed with sophisticated programming constructs such as if/else, while, do/while etc.

An operating system works as the interface between the user and hardware devices. The computer cannot perform any task without an operating system. There are many operating systems and UNIX is one of them. It is a stable operating system that supports multi-user environment. Linux is another popular operating system based on UNIX. Both UNIX and Linux provide powerful Command Line Interface (CLI). The users can enter the commands to the CLI to perform various tasks. There is a large number of commands, and sed and awk are two of them. These commands help filtering and text transformation.

Key Areas Covered

1. What is sed
     – Definition, Examples
2. What is awk
    – Definition, Examples
3. What is the Difference Between sed and awk
     – Comparison of Key Differences

Key Terms

sed, awk, Operating System

Difference Between sed and awk - Comparison Summary

What is sed

The command sed allows modifying and filtering text files.

Difference Between sed and awk

Figure 1: CLI to provide commands in Linux and UNIX

sed ‘1,3 d’ test1.txt

The above command will display the lines after line 3. It will not display 1, 2, 3 lines of the test1.txt.

sed   ‘ s /abc / cab/’ test1.txt

The above command will substitute cab instead of abc in the test1.txt file.

sed  ‘s/one/1/’  test1.txt

The above command will substitute 1 instead of one in the test1.txt file.

sed ‘1,2 ! d’  test1.txt

This will only display line 1 and line 2.

Those are few examples for sed. Overall, sed allows users to perform filtering and searching.

What is awk

The command awk allows searching data in a file and printing data on the console.  If a file contains multiple columns, then it is also possible to find data in specific columns. Furthermore, it helps tasks such as searching, conditional execution, updating and filtering.

awk  ‘{ print }’ file1.txt

The above command will display all the content in the file1.txt.  

Assume that the file1.txt file contains multiple columns.

awk   ‘{print $1}’ file1.txt

This command prints the first column of the file1.txt.  By default, the awk command considers a white space as the separator.

awk  ‘{print $1 “  ”  $3}’ file1.txt

The above command prints the first and the third column of the file1.txt with a space in between.

awk  ‘{print $1. $3}’ file1.txt

The above command will print the concatenation of first and the third column.

awk  ‘/example/ {print}’ file1.txt

The above command prints all the lines in the file1.txt file that contains the word example.

awk  ‘/ [0-9] / {print}’ file1.txt

The above command prints all the lines in the file1.txt file that contains numbers from 0 to 9.

awk  ‘/ ^ [0-9] / {print}’ file1.txt

This will print the lines in file1.txt that start with a number.

awk  ‘/   [0-9] $ / {print}’ file1.txt

This will print the lines in file1.txt that ends with a number.

It is also possible to check the conditionals as follows.

awk  ‘{ if ($1 ~  /123/ ) print}’ file1.txt

This will print the line if, the content starts with 123.

awk  ‘{ if ($2 ~  /[0-9] / ) print}’ file1.txt

This will print the lines in column two that start with a number.

Those are some examples of awk.

Overall, awk is a powerful command for processing and analyzing text files.

Difference Between sed and awk

Definition

The sed is a command line utility that parses and transforms text, using a simple, compact programming language. The awk is a command line utility designed for text processing that allows writing effective programs in the form of statements.

Functionality

Both sed and awk allow processing streams of characters for tasks such as text transformation. The awk is more powerful and robust than sed. It is similar to a programming language.

Complexity

The sed is simple and limited while awk is more complex and versatile.

Conclusion

The difference between sed and awk is that sed is a command utility that works with streams of characters for searching, filtering and text processing while awk more powerful and robust than sed with sophisticated programming constructs such as if/else, while, do/while etc.

Reference:

1. AWK Command : Session 1 : Select Column Data, Testing World, 8 Feb. 2017, Available here.
2. Using Linux AWK Utility, Frank the Programmer, 25 June 2011, Available here.
3. Linux/ Unix Tutorial || SED Commands in Linux by Shiva, Durga Software Solutions, 30 Oct. 2016, Available here.

Image Courtesy:

1. “Foremost on Xubuntu 11.04″ By Kris Kendall, Jesse Kornblum, and Nick Mikus – Screenshot of the -h output of foremost (Public Domain) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply