Dylan Havens - Lab 4.3.1 - Searching Files With Grep
Dylan Havens - Lab 4.3.1 - Searching Files With Grep
Objective: Review and practice using the grep command. Investigate using regular expressions to
fine tune searches.
Instructions:
Part 1: Review:
We will start by making a new small file with cat.
1. Open a terminal.
2. Create a new file named foods.txt with the following command: cat > foods.txt
Remember, the > symbol is the redirection operator. After running the command, notice that you
don’t have a command prompt. Type 5-6 foods as shown:
4. Now use the cat command (without the redirection operator) to view your new
file
GALANTECH with
Distribution.
Copyright © 2024 Cyber Innovation Center 1 All Rights Reserved. Not for GARDEN STATE CYBER
6. View your file again with the cat command. Is it still sorted?
No its is not
What the grep program does is it copies a line from a file, checks if it matches the pattern, and if it is a
match it will print that line. The program repeats this process until it reaches the end of the file.
1. Use grep to search for banana in your foods.txt file (since you are not using an option here
you can skip that part of the command)
2. Now repeat the command with the option -n What does that do?
3. Now try the command with the option -vn What happened?
GALANTECH with
Distribution.
Copyright © 2024 Cyber Innovation Center 2 All Rights Reserved. Not for GARDEN STATE CYBER
/home/kali/CourseFiles/IntroCybersecurity/Greatbooks
2. To view a copy of “The Return of Sherlock Holmes” you would type cat sherlock.txt.You can
see that it is very long! It’s time to learn a few more commands that are helpful when working with
long text files such as nl, less, head, and tail. Try each of them out.
nl sherlock.txt Displays line numbers
GALANTECH with
Distribution.
Copyright © 2024 Cyber Innovation Center 3 All Rights Reserved. Not for GARDEN STATE CYBER
\ (Back Slash) turn off the special meaning of the next character, as in \^
1. Use the grep commands with the text file named sherlock.txt as follows:
Command What it does? Look carefully at the output?
grep ‘b[lr]’ sherlock.txt Shows lines with either b, bl, br, or brl or
blr
grep ‘\<men\>’ All lines that contain “men”
sherlock.txt
grep ‘^..e’ sherlock.txt Any lines that have e as the third character
GALANTECH with
Copyright © 2024 Cyber Innovation Center 4 All Rights Reserved. Not for
Distribution.
GARDEN STATE CYBER