Question: Part 1 Instructions: Demonstrate using regular expressions with the grep command to filter text by completing the tasks below. You will also be required to
Part 1
Instructions:
Demonstrate using regular expressions with the grep command to filter text by completing the tasks below. You will also be required to complete tasks previous covered in prior sections and in class. Perform the task being requested on your vSphere virtual machine. Provide the requested answer or screenshot as indicated in the task.
Task 6
Count the number of instances the user sysadmin is in the /var/log/secure file. Provide the command you used below.
Your answer below:
Task 7
Create a file called mydata.db with the following list.
Fred apples 20 Susy oranges 5 Mark watermellons 12 Robert pears 4 Terry oranges 9 Lisa peaches 7 Susy oranges 12 Mark grapes 39 Anne mangoes 7 Greg pineapples 3 Oliver rockmellons 2 Betty limes 14
Task 8
Pipe the output of the cat command to grep to filter for oranges in the mydata.db file, using the command, cat mydata | grep oranges
Task 9
Pipe the output of the dmesg command (display changes and and access to hardware device) to grep to find all instances where sda (the first hard drive) is referenced. Use the command, dmesg | grep sda
Task 10
Pipe the output of the df -h command to filter only the instances of tmpfs. Provide the command you used below.
Your answer below:
Task 11
Filter the output of the ps -aux command (list system processes) to only list the processes running bash. (This is done similar to tasks 8-10. Provide the command you used below.
Your answer below:
Task 12
To perform more complex searches with grep we need to use regular expressions (regex). It is recommended to use egrep instead of regular grep when using regex. For instance, perhaps we wanted to filter for both Susy and Greg from the mydata.db file created in task 7. Use the following command, egrep Susy|Greg mydata.db.
The pipe used between Susy and Greg is being used as a regex character by egrep. It is not functioning as a typical pipe which you have used with redirection. Egrep uses a | to represent or. In this case to filter for Susy or Greg.
Explanation of command
- egrep - a text filtering command that uses regular expressions (regex)
- Susy|Greg - egrep will find all instances of either Susy or Greg
- mydata.db - The data file.
Task 13
Use the concepts in Task 12 to find all instances of apples and oranges from the mydata.db file. Provide the command you used below.
Your answer below:
Task 14
Find all instances of Anne and Lisa from the mydata.db file. Provide the command you used below.
Your answer below:
Task 15
To narrow down filters use a . (dot) and a * (asterisk) together. For instance if you would like to only return the line where Susy has 12 oranges and not 5 oranges then use the command, egrep Susy.*12 mydata.db
Task 16
Use the concept above to only return the line where Mark has 39 grapes. Provide the command you used below.
Your answer below:
Task 17
The -i option with grep or egrep provides a case insensitive search, meaning it will match both lower and uppercase letters. For example, egrep -i t mydata.db
This returns Terry with an uppercase t and Betty with lowercase ts. Notice this search finds any instance of t regardless of whether it is at the beginning, middle or end of a line.
Task 18
The ^ (caret) symbol represents the beginning of a line. To search for only the lines that begin with t, use the following command, egrep -i ^t mydata.db
Task 19
The $ (dollar sign) represents the end of a line. To search for only the lines that end with the number 2, use the following command, egrep 2$ mydata.db
Task 21
Regex symbols can be mixed and matched, for example, egrep -i ^S.*2$ mydata.db .
Explain what this command is doing. You can reference other tasks, the chart at the beginning of this assignment or Online resources if needed.
Your answer below:
Task 22
Find all lines that start with M from the mydata.db file. Provide the command you used below.
Your answer below:
Task 23
Find all lines that end with a 7 from the mydata.db file. Provide the command you used below.
Your answer below:
Task 24
Square brackets [ ] can be used to match a single character with set letters or numbers. For example to search for any letter y or gor 3, use the command, egrep -i [y3g] mydata.db
Task 25
The square brackets can be combined with other regex symbols. For example, egrep -i ^[st] mydata.db
This will only return lines that start with s or t.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
