typestar

grep in Bash

Search, count and invert with extended regexes.

log='GET /api 200
POST /login 401
GET /docs 200
POST /api 500'

# -E is extended regex; -c counts; -v inverts
echo "$log" | grep -E '^(GET|POST) /api'
echo "errors: $(echo "$log" | grep -cE ' [45][0-9]{2}$')"
echo "$log" | grep -v 200

# -o prints only the match itself
echo "$log" | grep -oE '[0-9]{3}$' | sort -u

How it works

  1. -E enables (GET|POST) alternation and {3} counts.
  2. -c counts matching lines; -v inverts the match.
  3. -o prints only the matched text itself.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
314
Tokens
49
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 39 seconds.

Type this snippet

Step 1 of 3 in Text tools, step 19 of 26 in Language basics.

← Previous Next →