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
-Eenables(GET|POST)alternation and{3}counts.-ccounts matching lines;-vinverts the match.-oprints only the matched text itself.
Keywords and builtins used here
echo
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.
Step 1 of 3 in Text tools, step 19 of 26 in Language basics.