0% found this document useful (0 votes)
8 views5 pages

Dylan Havens - Lab 4.3.1 - Searching Files With Grep

This document provides a detailed lab exercise focused on using the grep command in Linux or Unix systems for searching files. It includes instructions for creating a text file, practicing various grep options, and utilizing regular expressions to enhance search capabilities. Additionally, it covers commands for viewing long text files and demonstrates the power of grep through practical examples.

Uploaded by

dhavens26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Dylan Havens - Lab 4.3.1 - Searching Files With Grep

This document provides a detailed lab exercise focused on using the grep command in Linux or Unix systems for searching files. It includes instructions for creating a text file, practicing various grep options, and utilizing regular expressions to enhance search capabilities. Additionally, it covers commands for viewing long text files and demonstrates the power of grep through practical examples.

Uploaded by

dhavens26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 4.3.

1 - Searching Files With Grep


Name: Dylan Havens Date: Class:

Objective: Review and practice using the grep command. Investigate using regular expressions to
fine tune searches.

Instructions:

• Go to CYBER.ORG Range (https://apps.cyber.org)

• Click on the Range tab, then click on Launch Kali.

• Once the status changes to booted, click open.

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:

3. To save the file, press Ctrl+D

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

5. Try using the command: sort foods.txt What happened?


It sorted them in alphabetical order

6. View your file again with the cat command. Is it still sorted?

No its is not

Part 2: Using grep Command:


In Linux or Unix systems, grep (global regular expression print) is a small family of commands that
search input files for a search string and print any lines that match it. These commands are useful for
searching for strings and pattersn of characters in a life, in a group of files, or even within files nested
in sub-directories. The grep command is considered one of the most useful commands on any
Linux or Unix system.

The general pattern of the grep command is:

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)

It shows banana from the file

2. Now repeat the command with the option -n What does that do?

3:banana, shows the line number

3. Now try the command with the option -vn What happened?

Shows all the lines with line numbers besides banana

4. Now try with option -c What is this option is used for?

Shows how many times banana appears in the file

5. There are a couple more options that are helpful also:


• To ignore case, use -i
• To get a whole word match, use -w

GALANTECH with
Distribution.
Copyright © 2024 Cyber Innovation Center 2 All Rights Reserved. Not for GARDEN STATE CYBER

Part 3: More Practice with Files:


When working with very long files it is helpful to know a few commands that make it easier to view
the file. We will practice with the sherlock.txt file we have used before.

1. Change directories to the GreatBooks directory. It is in the IntroCybersecurity directory which


is located inside the CourseFiles directory. Use the absolute path to the directory and enter
it here.

/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

less sherlock.txt Read page by page (q to quit)

head -5 sherlock.txt Displays the first 5 lines (can change the


number)

head sherlock.txt Displays the first 10 lines

tail sherlock.txt Displays the last 10 lines

tail -5 sherlock.txt Displays the last 5 lines (can change the


number)

tac sherlock.txt Displays the file in reverse order

GALANTECH with
Distribution.
Copyright © 2024 Cyber Innovation Center 3 All Rights Reserved. Not for GARDEN STATE CYBER

Part 4: More Practice with grep:


So far, we’ve used the grep command with literal strings. But the real power of grep is in its ability to
use regular expressions to find just about anything. What are regular expressions? A regular
expression is a sequence of characters that defines a search pattern.
Special Character Meaning
^ (Caret) match expression at the start of a line, as in ^A

$ (Dollar Sign) match expression at the end of a line, as in A$

\< match the beginning of a word

\> match the end of a word

\ (Back Slash) turn off the special meaning of the next character, as in \^

[ ] (Brackets) match any one of the enclosed characters, as in [aeiou]

[^ ] match any one character except those enclosed in [ ], as in


[^0-9]

. (Period) match a single character of any value, except end of line

* (Asterisk) match zero or more of the preceding character or expression

\{x,y\} match x to y occurrences of the preceding

\{x\} match exactly x occurrences of the preceding

\{x,\} match x or more occurrences of the preceding

1. Use the grep commands with the text file named sherlock.txt as follows:
Command What it does? Look carefully at the output?

grep ‘^a’ sherlock.txt Shows lines that start with a

grep ‘^the’ sherlock.txt Shows lines that start with the

grep ‘men’ sherlock.txt Shows likes that contain “men”

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

You might also like