Back to commands

Apple Terminal

Read-only, can be slow

Search a Log for Errors With Context

A local log contains too many lines to manually scan for the relevant failure.

Command

grep -n -C 2 'ERROR' ./app.log

Before you run this

System impact: Read-only. Can create load on large logs, directories, filesystems, or process tables.

When not to use it: Do not use it for structured JSON analysis when jq would be more precise.

Expected output

Matching ERROR lines with two lines before and after each match.

System impact

Read-only, can be slow. Nothing changes. The command prints matching lines with line numbers and nearby context.

Scope this to the smallest useful path or service on busy systems.

Recovery / rollback: no state is changed.

When to use it

Use when debugging build logs, app logs, export logs, or CLI output saved to a file.

When not to use it

Do not use it for structured JSON analysis when jq would be more precise.

Watch this command run

Command transcript

This sanitized transcript shows the commands and output shape without exposing host details.

demo@lab:~$

$ nl -ba project/logs/app.log | sed -n '1,8p'

     1	server started
     2	loading config
     3	ERROR missing API_KEY
     4	retry disabled
     5	exit

$ grep -n -C 2 'ERROR' project/logs/app.log

1-server started
2-loading config
3:ERROR missing API_KEY
4-retry disabled
5-exit
View commands shown

These are the commands shown in the sanitized transcript.

Commands shown

  1. nl -ba project/logs/app.log | sed -n '1,8p'
  2. grep -n -C 2 'ERROR' project/logs/app.log

next steps

Related commands

Hosting Operations Can be slow

Scan Every CI Log for Error Lines

One grep pass can turn a log pile into a failure list.

grep -RInE 'error|failed|failure|exception|traceback' artifacts logs | head -50
Apple Terminal Stops processes

Flush macOS DNS Cache

Changed DNS but your Mac still visits the old place? Flush the resolver cache.

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Study mapping

Use this as independent command practice: read the notes, predict the output, then compare it with the example before using a real shell.

  • lpic1:103-gnu-unix-commands
  • lfcs:essential-commands
  • linuxplus:automation-scripting
  • linuxplus:provisional
  • linuxplus:troubleshooting
  • risk:macos-only
  • risk:read-only

Useful for

  • LPIC-1 style command-line practice
  • LFCS style performance tasks
  • Linux+ style troubleshooting review

Independent study support only. No affiliation, endorsement, exam dumps, or real exam questions.