Answer questions about a log file with a pipeline
A pipeline turns raw log lines into counts by extracting fields, sorting and counting. The access log is synthetic; its numbers are not traffic data.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-48-A: a log pipeline over a synthetic file returns the counts that the synthetic data implies
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'GET /a 200\nGET /b 200\nPOST /a 500\nGET /a 200\n' > access.logCommand:
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -2; echo codes=$(awk '{print $3}' access.log | sort -u | tr '\n' ' ')Recorded output:
3 GET
1 POST
codes=200 500TE-48-B: awk with a field test and grep -c count the lines with status 500 and with status 200
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'GET /a 200\nGET /b 200\nPOST /a 500\nGET /a 200\n' > access.logCommand:
awk '$3==500' access.log | wc -l; grep -c ' 200$' access.logRecorded output:
1
3Exit status: 0. This status and the output match what was written before the run.
TE-48-C: expected non-zero exit: grep -c on a log file that does not exist exits with status 2
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'GET /a 200\nGET /b 200\nPOST /a 500\nGET /a 200\n' > access.logCommand:
grep -c 200 missing.logRecorded output: none, standard output was empty.
Standard error from the same run:
grep: missing.log: No such file or directoryExit status: 2. This status and the output match what was written before the run.
TE-48-B: by the setup, one line has status 500 and three lines end in 200. awk printed a count of 1 for the 500 line, and grep -c printed 3. TE-48-C: grep -c 200 missing.log exited with status 2, nothing was printed to standard output, and standard error reads grep: missing.log: No such file or directory.
Limits
The access log is synthetic; its numbers are not traffic data.
Source
Source: https://www.gnu.org/software/gawk/manual/
Evidence
TE-48-A: a log pipeline over a synthetic file returns the counts that the synthetic data implies
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-48-A | a log pipeline over a synthetic file returns the counts that the synthetic data implies | 0 | PASS | f5bf7ca04ee9 | | TE-48-B | awk with a field test and grep -c count the lines with status 500 and with status 200 | 0 | PASS | af6dd763e790 | | TE-48-C | expected non-zero exit: grep -c on a log file that does not exist exits with status 2 | 2 | PASS | 1bc665b56f1c |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides