Use the GREP and Sort Commands Together
In this IT Pro Challenges virtual lab, learners are introduced to the skills required to manage file data. Exercises in this lab teach users how to extract, sort, add, line numbers using grep, sort, and NL commands. The topics covered in this lab are critical for learners to be effective in System administrator, SOC analyst roles in cybersecurity.
Suppose you are a Linux user, and you want to find specific information from a text file. To accomplish this task, you can use the option called the grep command. This command scans the entire file for the sought information and displays the result. On the other hand, the sort command sorts the contents of a file alphabetically.
In this hands-on lab, you will learn how to use the GREP and Sort commands together. First, you will extract lines from a file by using the grep command, and then you will sort the output in reverse numeric order. Next, you will search for content that contains a specific word, and then you will precede each line of the result by using the file name. Finally, you will perform a multiple column sort. The other guided challenges in this series are “Search for Data by Using Grep” and “Sort Files.”
Understand the Scenario
In this virtual lab, You are a Linux system administrator. Your job is to search for files and extract lines from files in your system. To accomplish this task, you will use the GREP and Sort command together. You will use a Linux Ubuntu distribution.
Create necessary files and directories
In this section of the lab, you will create necessary files and directories. First, you will Run the mkdir command to create the files and directories required in this challenge. Next, you will review the documentation on redirecting content to a new file and piping from one command to another by using bash and then review the documentation on numbering lines in a file by using the nl command. Finally, you will review the documentation on sorting lines in a file by using the sort command and then review the documentation on selected lines from a file by using the grep command.
Create a new file by using grep, nl, and sort
In this section of the virtual lab, learners will learn how to create a new file by using grep, nl, and sort. First, in a single command line, they will display all the lines in /etc/kernel/postinst.d/apt-auto-removal that contain sed and then number the lines by piping the grep command output to the nl command and pipe that output to the sort command and then sort in reverse numeric order to redirect the output of the sort command to a file named One. Next, they will review the documentation on selected lines from a file by using the grep command. You can use the -H option to precede each line selected by the grep command with the file name. This option is often used when running the grep command on several files at the same time. Finally, learners will verify that they searched by using the grep command, numbered the lines of the output, sorted the output in reverse numeric order, and then saved the output in a new file named One.
Display selected lines and the file name from a file
In this section of the lab, you will display selected lines and the file name from a file. First, you will find all of the lines in the /etc/kernel/postinst.d/apt-auto-removal file that contains awk by using the grep command and then precede each line in the output by using the file name, and then place these lines into a file named Two. Next, you will review the documentation on the password file to verify the format of the passwd file and then review the documentation on the options for the sort command. Finally, you will check and verify that you searched using the grep command and preceded each line in the output by using the file name and then saving the output in a new file named Two.
Sort the passwd file
After displaying selected lines and the file name from a file, in this section of the lab, you will learn how to sort the passwd file. First, you will sort the /etc/passwd file by the shell (the seventh column), and then by the user name (the first column), and then redirect the output to a file named ~/LCLF-009/Three. You will need to tell the sort command which keys to sort by, and that the field separator is a colon (:). Finally, you will check and verify that you sorted the /etc/passwd file by the seventh and first columns, and then stored the output in a file named Three.
Lab Summary Conclusion
After completing the “Use the GREP and Sort Commands Together” virtual lab, you will have accomplished the following:
- Extracted lines from a file by using the grep command.
- Sorted in reverse numeric order
- Sorted by multiple columns.
- Added line numbers to a file.
- Added the file name to the output of a search.
In this IT Pro Challenge virtual lab, you will demonstrate your ability to use advanced ...
Learn to quickly locate terms in file and command output through the Using GREP to ...