CMD Master
Back to Blog
•Arnošt Havelka

Practice: Data Extraction

Combine grep, pipes, and redirection to extract and analyze data.

Start Interactive Lesson
Practice: Data Extraction

Time 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]>[target]
grep'ERROR'app.log>errors.txt
Terminal
~$grep 'ERROR' app.log > errors.txt

Step 1: Find Errors

Extract all ERROR lines from the log file.

Terminal
~$grep 'ERROR' app.log > errors.txt cat errors.txt

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
Terminal
~$grep 'ERROR' app.log | wc -l
1

Count with pipes:

Terminal
~$grep 'ERROR' server.log | wc -l

Step 3: Save Results

Extract, filter, and save all in one command.

Terminal
~$grep 'alice' data.txt > alice_records.txt cat alice_records.txt

Step 4: Generate a Report

Create a summary report combining multiple extractions.

Terminal
~$echo 'Log Summary Report' > report.txt echo 'Total Errors:' >> report.txt grep 'ERROR' app.log | wc -l >> report.txt cat report.txt

Key Patterns to Remember

  1. Extract: grep 'pattern' file
  2. Count: grep 'pattern' file | wc -l
  3. Save: grep 'pattern' file > output.txt
  4. Append: echo 'text' >> file.txt
  5. Chain: grep X | grep Y | wc -l

Knowledge Check

1 / 3

How do you extract errors to a file?

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

SORT - Order Data

Sort lines of text files.