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.
Unix Video #21:
Table of Contents:
More Unix Filter Commands
Command | tr – 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 |
Command | tee – 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.
Command | awk – this command is a useful and powerful command used for pattern matching as well as for text processing. |
---|---|
Common Syntax | awk [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. |
Command | sed – 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] |
Example1 | sed -n ‘/hello/p’ file1 This command will display all the lines which contains hello |
Example2 | sed ‘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.