📖 Scenario: You are working as a system administrator managing server logs. You want to quickly find and summarize information from text files using Linux command-line tools.
🎯 Goal: Learn how to use basic Linux text processing commands to filter, count, and display information from text files.
📋 What You'll Learn
Use the cat command to display file contents
Use the grep command to filter lines containing specific text
Use the wc command to count lines
Use pipes | to combine commands
💡 Why This Matters
🌍 Real World
System administrators and developers often need to quickly find and summarize information from large text files like logs. Linux text processing commands make this fast and easy.
💼 Career
Knowing how to use grep, wc, and shell variables is essential for troubleshooting, monitoring, and automating tasks in IT and software development roles.
Progress0 / 4 steps
1
Create a sample log file
Create a file called server.log with these exact lines: INFO User login successful ERROR Disk space low INFO Scheduled backup completed ERROR Network timeout INFO User logout
Linux CLI
Hint
Use the echo command with -e and redirect output to create the file.
2
Set a variable for the error keyword
Create a variable called keyword and set it to the string ERROR.
Linux CLI
Hint
Use simple variable assignment without spaces.
3
Filter lines containing the keyword
Use grep with the variable keyword to filter lines from server.log that contain the word ERROR. Save the result to a file called error.log.
Linux CLI
Hint
Use double quotes around $keyword to expand the variable.
4
Count and display the number of error lines
Use wc -l on error.log to count the number of lines containing errors. Print the count with the message: Error count: X where X is the number of lines.
Linux CLI
Hint
Use command substitution $(...) to get the line count.
Practice
(1/5)
1. Which Linux command is mainly used to search for specific text patterns within files?
easy
A. sed
B. awk
C. grep
D. cat
Solution
Step 1: Understand the purpose of grep
grep searches files for lines matching a text pattern.
Step 2: Compare with other commands
sed edits text streams, awk processes text fields, cat displays file content.
Final Answer:
grep -> Option C
Quick Check:
Search text patterns = grep [OK]
Hint: Remember: grep means 'global regular expression print' [OK]
Common Mistakes:
Confusing sed with grep
Thinking awk is for simple text search
Using cat to find patterns
2. Which of the following is the correct syntax to replace all occurrences of 'apple' with 'orange' in a file named fruits.txt using sed?
easy
A. sed 'apple=orange' fruits.txt
B. sed 'replace apple with orange' fruits.txt
C. sed -r 'apple -> orange' fruits.txt
D. sed 's/apple/orange/g' fruits.txt
Solution
Step 1: Recall sed substitution syntax
The correct form is sed 's/old/new/g' to replace all occurrences.
Step 2: Match syntax to options
Only sed 's/apple/orange/g' fruits.txt uses the correct s/apple/orange/g pattern.
Final Answer:
sed 's/apple/orange/g' fruits.txt -> Option D
Quick Check:
sed substitution = s/old/new/g [OK]
Hint: Use s/old/new/g for global replace in sed [OK]
Common Mistakes:
Missing 's/' at start of substitution
Using wrong delimiters or syntax
Forgetting the 'g' flag for global replace
3. What will be the output of the following command?
echo -e "apple\nbanana\napple" | grep -c apple
medium
A. 2
B. 1
C. 3
D. 0
Solution
Step 1: Understand the input lines
The input has three lines: 'apple', 'banana', and 'apple'.
Step 2: Count lines matching 'apple' with grep -c
grep -c apple counts lines containing 'apple'. Two lines match.
Final Answer:
2 -> Option A
Quick Check:
Count lines with 'apple' = 2 [OK]
Hint: grep -c counts matching lines, not occurrences [OK]
Common Mistakes:
Counting total lines instead of matches
Confusing occurrences with line count
Ignoring the -c flag meaning
4. The command below is intended to print only lines containing 'error' from log.txt. What is wrong?
grep error log.txt | sed 's/error//'
medium
A. It filters lines but removes 'error' text from output
B. It causes a syntax error in sed
C. It removes 'error' but prints all lines, not filtered
D. It does not read the file
Solution
Step 1: Analyze the pipeline commands
grep error log.txt filters lines with 'error'. Then sed 's/error//' removes 'error' from those lines.
Step 2: Understand the effect on output
The output shows lines containing 'error' but with the word 'error' removed, which may be unintended.
Final Answer:
It filters lines but removes 'error' text from output -> Option A
Quick Check:
grep filters, sed edits text [OK]
Hint: Pipes chain commands; sed edits output after grep filters [OK]
Common Mistakes:
Thinking sed filters lines
Expecting grep to remove text
Assuming syntax error without checking
5. You want to extract the second column from a file data.csv where columns are separated by commas. Which command pipeline correctly does this?
hard
A. grep ',' data.csv | sed 'print $2'
B. cat data.csv | awk -F',' '{print $2}'
C. sed -n '2p' data.csv
D. awk '{print $2}' data.csv
Solution
Step 1: Identify the field separator and extraction tool
Columns are comma-separated, so awk with -F',' sets comma as delimiter.
Step 2: Check command correctness
cat data.csv | awk -F',' '{print $2}' uses awk -F',' '{print $2}' to print second column correctly.
Final Answer:
cat data.csv | awk -F',' '{print $2}' -> Option B
Quick Check:
awk -F',' prints columns by field [OK]
Hint: Use awk -F',' to split CSV columns [OK]
Common Mistakes:
Using sed to print columns incorrectly
Forgetting to set field separator in awk
Using grep which filters lines, not columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.