The article covers the best Linux tools to read files and use regular expressions to perform operations on the selected text. It also covers their most basic functionality and examples for better understanding.
1. grep
grep is a Linux text-manipulating utility that searches for a string of characters or patterns known as regular expressions in a file or text. The grep tool belongs to the family of utilities that include egrep, fgrep, and grep, among which fgrep is the fastest of all, while grep is the easiest.
The general syntax for using grep is as follows:
For example, to search for the word “root” in the /etc/passwd file:
Some standard command-line examples to get started are:
Similarly, you can use the ^ metacharacter with the grep command to display all the matching strings that begin with certain characters.
For instance, the following command pipes the env command output as an input to grep and displays variables that begin with “HO”:
2. awk
awk is a powerful scripting language and a command-line text-manipulation tool that can perform line-by-line scans and compare lines to patterns. The basic syntax of the awk command is an action defined between a single quotation mark and curly braces followed by the filename.
The utility searches the file using regular expressions and performs the function defined in the action parameter. awk executes the script on every line if you do not set a pattern, as shown below:
…where $1 displays the first field of the awk_examples.txt file.
The following command performs the print function on the given pattern by replacing the second field “World” with “Alice,” and displays the whole line ($0):
Output:
Similarly, you can use the function print $0 from the command above to emulate the grep functionality.
3. sort
sort is another Linux command-line utility that helps you display the content of the specified text file in a sorted format. For instance, you can pipe the output of the awk command as an input to the sort utility as follows:
Output:
4. sed
sed or stream editor takes input as a stream of characters and performs filtering and text transformations (delete, substitute, and replace) on the specified text.
You can use it in a script and edit files non-interactively. Hence, the most basic purpose of the utility is the substitution of string/characters. The general syntax is:
Create a file using random sentences to practice and understand the working of this utility.
Let’s replace the occurrence of the word “two” on every line of the file with “2” using the -g flag for global replacement, as follows:
Similarly, use the -d flag to delete a specific line from the file:
You can also replace the string by specifying a line number (4 s/two/2/p) and only printing the replaced line as follows:
The -n flag in the command above disables the automatic printing of the input stream to the output. You can use this option in your favor for replacing the grep utility functionality with sed.
For instance, you can modify the command above by only including a regex pattern /two/p such that the -p flag will only print the lines to the standard output stream.
5. cut
The cut is another command-line utility that cuts/extracts parts of text from a line or file. It cuts the text based on a specified field, character, or byte position and pipes the result to the standard output.
The utility takes in the following syntax:
Use the -b option to cut section or content using a specified byte or a range of bytes:
Use the -c flag to extract text by specifying the positions of characters:
Lastly, you can also extract text by specifying fields with the -f option and -d for space or field delimiter:
Here’s the list of ranges with examples and descriptions that you can utilize with the character -c and byte -b options:
Note that you cannot define the ranges for text extraction by using the field -f option.
Manipulating Text With Linux Commands
Linux offers many programs and tools for handling and working around files or text. Learning them all might not be required as you can easily fill the gap with another once you have a good grip over one, like using sed as grep or awk as grep, but this can’t be true for every tool.
Besides, Linux commands have a steep learning curve but once you develop the skill, they can prove to be very useful and effective in the life of any Linux user, especially a system administrator.