More Unix Filter and Awk, Sed Commands in Text Processing

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated October 13, 2024

Overview of more Unix filter commands and awk and sed commands for simple automated processing text:

In this tutorial, we will cover the remaining filter commands.

We also take a look at awk and sed, which are the two powerful commands for simple automated processing text. 

More Unix Filter and Awk, Sed Commands in Text Processing

Unix Video #21:

More Unix Filter Commands

Commandtr – this command is used to translate the characters in a file in some other forms like squeezing the repetitive characters and replacing it with the single occurrence of that character or deleting a character. This can be achieved by using different options available with the tee command.
Common Syntax:tr [OPTION] ……SET1[SET2]
Example1:tr -d ‘,’ < file1
This command will delete all the occurrence of comma(“,”) from the file file1
Example 2 $ tr -d “hello” < file1
This command will delete all the occurrence of any of the characters h or e or l or o from the file fle1
Commandtee – this command is used to read the input from stdin and redirect the output to the stdout i.e. display screen as well as to the specified files at the same time. It is primarily used in conjunction with pipes and filters.
Common Syntax: tee[OPTION]….[FILE]
Example $ cat file1|tee filenew
This command will display the contents of file1 on the display screen and it will also be saved in filenew which can be viewed by the command cat filenew

Unix sed and awk Text Processing Utilities

Unix provides sed and awk as two text processing utilities that work on a line-by-line basis. The sed program (stream editor) works well with character-based processing, and the awk program (Aho, Weinberger, Kernighan) works well with delimited field processing.

Both use regular expressions to find patterns and support commands to process the matches.

Commandawk – this command is a useful and powerful command used for pattern matching as well as for text processing.
Common Syntaxawk [options] ‘program text’ file
Example $ls -l | awk ‘{print $3}’
This command will display only the third column from the long listing of files and directories.
Commandsed – this is a powerful command for editing a ‘stream’ of text. It can read input from a text file or from piped input, and process the input in one pass..
Common Syntax sed[OPTION]…..[-f][file]
Example1sed -n ‘/hello/p’ file1
This command will display all the lines which contains hello
Example2sed ‘s/hello/HELLO/’ file1
This command will substitute hello with HELLO everywhere in the file.
Example3 sed ‘/hello/,+2d’ file1
This command will delete the two lines starting with the first match of ‘hello’

Our upcoming tutorial will brief you more on Command Line Parameters.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment