Skip to main content

Command Palette

Search for a command to run...

Linux process text awk, sed and grep

Published
2 min read

The three most useful text processing utilities in Linux are grep, sed, and awk. Despite being completely independent technologies, their functionality appears to overlap in simple cases. For example, if we want to discover a pattern in a file and publish the matches to the standard output, we'll see that they all can.

However, if we go beyond this simple exercise, we will discover that grep is only useful for basic text matching and printing.

On the other hand, in addition to match and print text, sed provides additional text modification commands such as substitution.

Finally, awk, the most powerful of these tools, is a scripting language that includes many functionality not found in the other two.

  1. grep :

    The grep command searches for lines that match a regex pattern and writes them to standard output. It is useful when we need to quickly determine whether or not a specific pattern exists in the given data.

    Basic syntax:

     grep [OPTIONS] PATTERN [FILE...]
    

    Let's say we want to extract the INFO events from log.txt. We can do that with grep:

    In above snippet print those lines containing the word INFO to the standard output.

  2. sed :

    The sed command edits character streams. It is a more powerful tool than grep because it provides additional choices for text processing, including the substitution command, for which sed is well known.

    Basic syntax :

     sed [OPTIONS] SCRIPT FILE...
    

    In above snippets -n is provide for exact match to find out.

    In above command '=' showing the line number and second expression we print line

  3. awk :

    awk is working on formatted data for ex. csv

    Now we want to print first column