We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Linux Practices: Using grep and egrep
Practice 1: Searching for a Word in a File with grep
Objective: Use grep to search for a specific word in a file. Instructions: 1. Create a file named Heroes.txt with the following content: IronMan Thor Hulk BlackWidow CaptainAmerica 2. Search for Thor in the file: grep Thor Heroes.txt 3. Verify that only the line containing Thor is displayed.
Practice 2: Case-Insensitive Search with grep
Objective: Use grep to perform a case-insensitive search. Instructions: 1. Search for ironman in Heroes.txt, ignoring case: grep -i ironman Heroes.txt 2. Verify that the search matches IronMan regardless of case.
Practice 3: Displaying Line Numbers with grep
Objective: Use grep to display line numbers of matches. Instructions: 1. Search for Hulk in Heroes.txt and include line numbers: grep -n Hulk Heroes.txt 2. Verify that the output includes the line number of the match.
Practice 4: Inverting the Search with grep
Objective: Use grep to display lines that do not match a pattern. Instructions: 1. Display all lines that do not contain Thor in Heroes.txt: grep -v Thor Heroes.txt 2. Verify that Thor is excluded from the output.
Practice 5: Using egrep for Multiple Patterns
Objective: Use egrep to search for multiple patterns. Instructions: 1. Search for IronMan or Hulk in Heroes.txt: egrep "IronMan|Hulk" Heroes.txt 2. Verify that both IronMan and Hulk are included in the output.
Practice 6: Using Regular Expressions with egrep
Objective: Use egrep with regular expressions to match patterns. Instructions: 1. Search for lines in Heroes.txt that start with B: egrep "^B" Heroes.txt 2. Verify that only BlackWidow is displayed.
Practice 7: Matching Lines with a Specific Ending
Objective: Use egrep to find lines that end with a specific letter. Instructions: 1. Search for lines in Heroes.txt that end with n: egrep "n$" Heroes.txt 2. Verify that IronMan is displayed.
Practice 8: Counting Matches with grep
Objective: Use grep to count the number of matching lines. Instructions: 1. Count how many lines contain the letter a in Heroes.txt: grep -c a Heroes.txt 2. Verify that the count matches the expected number of lines.
Practice 9: Recursive Search with grep
Objective: Use grep to search for a pattern in multiple files. Instructions: 1. Create two files, Avengers.txt and Guardians.txt, with hero names. 2. Search for IronMan in all .txt files in the current directory: grep -r IronMan . 3. Verify that the output lists the matching file and line.
Practice 10: Highlighting Matches with grep
Objective: Use grep to highlight matches in the output. Instructions: 1. Search for Thor in Heroes.txt and highlight the matches: grep --color=always Thor Heroes.txt 2. Verify that Thor is highlighted in the output.