PDF Version Available
This document is also available in PDF format: grepandfriends.pdf
The PDF version includes bookmarks for easy navigation and is optimized for printing.
Accessibility Notice
This document is also available in HTML format at:
https://aholdengouveia.name/IntroLinux/labs/grepandfriends.html
The HTML version provides enhanced accessibility features including keyboard navigation, screen reader support, responsive design, dark mode support, and high contrast options.
Objectives:
- Learn to use grep to filter text (locate lines with a specific text)
- Use head, tail, and less to display a text file
Complete the following problems
References, a video, a PowerPoint and some notes are available at my website https://www.aholdengouveia.name/IntroLinux/grepandfriends.html
We will use the /etc/passwd file for our example text file.
If you would like to see some examples of what else you can do with grep I have a video on my Linux FAQ playlist on grep, it's called Grep: re-GREPable, ohh so re-GREPable... and can be found at this URL https://www.youtube.com/embed/Iif-DjWYoWY
Grep can be used in many ways, but the two ways we'll work with are using a pipe or just passing arguments.
Basic form: \\ !cat /etc/passwd | grep pattern! \\ or \\ |grep pattern file| \\ or with a switch like -n \\ |grep -n pattern file|
Use man (or the text) to see what switches are available.
The lab submission will be the command and the output in a text file
Problems to solve
- Find lines with your name
- Find lines with the name of your teacher (aholdengouveia)
- Find any lines containing daemon
- Use the -n switch to display the number of the line, and find nologin
- Use the -c switch to count how many times daemon shows up
- Count the number of bash appearances
- Use the -v switch (and -c) to count the number of lines without bash
- Display the file using head
- Do it again showing 15 lines
- Display the last 10 lines
- Display the last 20
- Use both more and less to display /etc/passwd
- Use od /etc/passwd | more (to display only one page)
- use the redirect to send the first 10 lines of the /etc/passwd file to a file called file1
- Append the last ten lines of /etc/passwd to file1
Deliverables
Turn in one file that includes all answers as text and screenshots for all 15 questions. All clearly labeled and numbered with which question your answer is for. Make sure to include screenshots for each answer, each screenshot should be clearly focused on the solution to the question asked.