•Arnošt Havelka
Practice: Data Extraction
Combine grep, pipes, and redirection to extract and analyze data.
Start Interactive LessonTime to put it all together! Use grep, pipes, and redirection to extract and analyze real data.
The Scenario
You have a log file with thousands of lines. You need to find errors, count them, and save the results.
Try the command
Extract errors to a file!
Build the command
grep'pattern'file>output.txt
grepFind matching lines
>Save to a file
Terminal
C:\Users\User>grep 'pattern' file > output.txt
Step 1: Find Errors
Extract all ERROR lines from the log file.
Command Prompt
2024-04-06 10:00:00 INFO Started
2024-04-06 10:01:00 ERROR Database timeout
2024-04-06 10:02:00 INFO Request processed
2024-04-06 10:03:00 ERROR Connection failed
2024-04-06 10:04:00 WARNING Low memory
C:\Users\User>cat app.log
Step 2: Extract and Count
Combine pipes to find and count specific lines.
Try the command
Count matching lines!
Build the command
grep'pattern'file|wc -l
| wc -lCount the number of matching lines
Terminal
C:\Users\User>grep 'pattern' file | wc -l
Count with pipes:
Command Prompt
ERROR: 404 not found
ERROR: 500 server error
ERROR: timeout
INFO: success
ERROR: 403 forbidden
C:\Users\User>cat server.log
Step 3: Save Results
Extract, filter, and save all in one command.
Command Prompt
alice,25
bob,30
alice,28
carol,35
alice,26
C:\Users\User>cat data.txt
Step 4: Generate a Report
Create a summary report combining multiple extractions.
Command Prompt
ERROR: disk full
INFO: started
ERROR: memory leak
WARNING: slow query
ERROR: timeout
C:\Users\User>cat app.log
Key Patterns to Remember
- Extract:
grep 'pattern' file - Count:
grep 'pattern' file | wc -l - Save:
grep 'pattern' file > output.txt - Append:
echo 'text' >> file.txt - Chain:
grep X | grep Y | wc -l
Knowledge Check
1 / 3How do you extract errors to a file?
References
These documentation links provide authoritative details for the commands used in this article.