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:
Link: 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.
Learning Outcomes
By the end of this lab, you will be able to:
- Use grep to find lines that contain specific text
- Use grep switches to number, count, and exclude matching lines
- Display all or part of a file in different ways using head, tail, less, more, and od
- Use redirection to save output to a file and to append to it
Lab Instructions
We will use the /etc/passwd file for our example text file.
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 textbook) to see what switches are available.
Problems to solve
- Find lines with your name
- Find lines with the name of your teacher (aholdengouveia). This one must be done on the CIS server, using the login information you were given.
- 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, then show the contents of 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. Your screenshots must show your prompt with your username, or a note with your name and the term.
Resources
References, a video, a PowerPoint and some notes are available at my website Link: https://www.aholdengouveia.name/IntroLinux/grepandfriends.html
My FAQ Linux playlist on YouTube, with demos of many of the commands and scripts in this class Link: https://www.youtube.com/playlist?list=PLQOmuLC45eoQlIMH7oCNL6yFP7kWQubBo
My video with more examples of what you can do with grep, Grep: re-GREPable, ohh so re-GREPable... Link: https://www.youtube.com/watch?v=Iif-DjWYoWY