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:

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

  1. Find lines with your name
  2. Find lines with the name of your teacher (aholdengouveia)
  3. Find any lines containing daemon
  4. Use the -n switch to display the number of the line, and find nologin
  5. Use the -c switch to count how many times daemon shows up
  6. Count the number of bash appearances
  7. Use the -v switch (and -c) to count the number of lines without bash
  8. Display the file using head
  9. Do it again showing 15 lines
  10. Display the last 10 lines
  11. Display the last 20
  12. Use both more and less to display /etc/passwd
  13. Use od /etc/passwd | more (to display only one page)
  14. use the redirect to send the first 10 lines of the /etc/passwd file to a file called file1
  15. 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.