Skip to content
Work on part of a file

Scan, rewrite or mask part of a file

Read the first lines of a file, its last, or a range of them, and scan, rewrite or mask that part alone. A part at the end is read backward from the end, so how large the file is does not matter, and each match keeps the file’s own line and offset. The selections are on windows.

$ cat app.log
2026-09-27T09:00:01Z INFO GET /v2/users from 10.0.0.5 took 120ms
2026-09-27T09:00:02Z WARN retrying the primary after 3 failures
2026-09-27T09:00:03Z INFO GET /v2/orders from 10.0.0.7 took 1450ms
2026-09-27T09:00:04Z ERROR payment for bob@x.com failed: card 4111 1111 1111 1111 declined
2026-09-27T09:00:05Z INFO GET /v2/users from 10.0.0.5 took 95ms

Read the end of a file

tail reads backward from the end until it holds the lines asked for, and -n numbers them as the file does:

$ trex tail 3 -n app.log
3:2026-09-27T09:00:03Z INFO GET /v2/orders from 10.0.0.7 took 1450ms
4:2026-09-27T09:00:04Z ERROR payment for bob@x.com failed: card 4111 1111 1111 1111 declined
5:2026-09-27T09:00:05Z INFO GET /v2/users from 10.0.0.5 took 95ms

Scan only the last lines

--tail N scans only the last N lines, read from the end, and a match reports its line and offset in the whole file:

$ trex scan '\R{>1s}:took' app.log --tail 3 -H
app.log:3:61: "1450ms"  captures: took="1450ms"

Rewrite the first lines

--head N rewrites the first N lines and prints them alone:

$ trex rewrite '\R{>1s}:took' '${took} (slow)' app.log --head 3
2026-09-27T09:00:01Z INFO GET /v2/users from 10.0.0.5 took 120ms
2026-09-27T09:00:02Z WARN retrying the primary after 3 failures
2026-09-27T09:00:03Z INFO GET /v2/orders from 10.0.0.7 took 1450ms (slow)

Mask a range

--lines A..B masks that range and prints it alone, so nothing outside it reaches the output:

$ trex redact '(\E:email | \{card}:card)' --keep 'card:last4, email:domain' app.log --lines 4..5
2026-09-27T09:00:04Z ERROR payment for ****x.com failed: card ***************1111 declined
2026-09-27T09:00:05Z INFO GET /v2/users from 10.0.0.5 took 95ms

--record UNIT counts paragraphs or another record unit in place of lines, on every command above.