Profile picture
Sidharth R
  • Home
  • Posts
  • Journal
  • Labs
  • Home
  • Posts
  • Journal
  • Search
  1. Home
  2. Labs
  3. Linux labs
  4. Practical Grep: Filtering Errors and Patterns in Log Files

Practical Grep: Filtering Errors and Patterns in Log Files

EASY Jun 2026 ~5 min labex ↗ loading... views

Introduction

This walkthrough covers using grep to search and filter log files for errors, specific requests, and multi-pattern matches.

Tasks

  1. Find all ERROR messages in system.log and count how many there are.
  2. Find all lines containing POST requests in access.log.
  3. Find all lines in application.log that contain both WARNING and query (case-insensitive).
  4. Search for all user authentications (containing User authenticated) across all log files.

Requirements

  • Use grep for all searches (combining with other commands like wc where needed).
  • Save each task’s output to its own file: task1_output.txt, task2_output.txt, task3_output.txt, task4_output.txt.
  • Original log files must not be modified.

Solution

Task 1

BASH
grep -c "ERROR" system.log > task1_output.txt

grep -c counts the number of matching lines instead of printing them. Redirecting the output saves the count to task1_output.txt.

Task 2

BASH
grep POST access.log > task2_output.txt
$ head -n 2 task2_output.txt
241.98.70.154 - - [31/Jan/2026:19:18:15 +0000] "POST /api/v1/resource12 HTTP/1.1" 204 319
210.20.0.7 - - [08/May/2026:19:18:15 +0000] "POST /api/v1/resource9 HTTP/1.1" 201 8

Task 3

BASH
grep WARNING application.log | grep -i query > task3_output.txt

The first grep filters for WARNING entries. The second grep searches those results for query, using -i to ignore case.

$ head -n 2 task3_output.txt
2025-09-22 19:18:17 WARNING Slow query detected. Execution time: 8528ms
2026-06-20 19:18:17 WARNING Slow query detected. Execution time: 3800ms

Task 4

BASH
grep "User authenticated" *.log > task4_output.txt
$ head -n 2 task4_output.txt 
application.log:2025-07-06 19:18:17 INFO User authenticated for session #8869
application.log:2025-12-23 19:18:17 INFO User authenticated for session #3109

Because multiple files are searched (*.log), grep prefixes each matching line with the filename.

Key Takeaways

  • Use grep -c to count matching lines.
  • Pipe grep with | to apply multiple filters.
  • Use -i for case-insensitive searches.
  • Use wildcard patterns such as *.log to search multiple files.
  • Redirect output using > to save results.

Nerdsid.com

Links
  • Home
  • Contact
  • About
  • Posts
  • Journal
  • Quotes
  • Links worth your time
  • Notes
  • Style guide
© 2026 Sidharth R.
All content on this website is licensed under CC BY-NC-SA 4.0.