Text Processing, Regular Expressions, and JSON

Match a bounded format, preserve originals, and reconcile every record.

Choose the representation first

Input Suitable starting point
Literal substring grep -F
Text pattern grep -E
Simple delimiter-separated fields cut or awk
Stream substitution sed
Conditional counts and sums awk
Nested JSON values jq

A delimiter splitter is not a full CSV or JSON parser.

Literal matching and regex differ

printf 'host.log\nhostXlog\n' > names.txt
grep -F 'host.log' names.txt
grep -E 'host.log' names.txt

The fixed-string search returns only host.log. The regex dot also matches X. Quote the program so Bash does not interpret its punctuation.

Anchor the whole format

LC_ALL=C grep -E '^host-[0-9]+$' labels.txt

With the supplied fixture, only host-12 matches.

Anchors reject a matching substring inside a longer record. A shape check does not establish an allowed numeric range, host existence, or authorization.

Grep status 1 means no match; 2 means an error on the tested systems. Do not call both “no events.”

Record operations can discard information

cut -d '|' -f 1 events.txt | LC_ALL=C sort -u

Use the lesson's three simple event records. The result contains east and west, but loses original order and repetition.

Uniq alone combines adjacent duplicates. Numeric quantities need numeric sorting rather than lexicographic ordering.

Sed produces a derived view

sed '/^ERROR /s/pending/reviewed/' original.txt > reviewed.txt

With the supplied two-record fixture, INFO pending remains unchanged and ERROR pending becomes ERROR reviewed.

The original still contains pending. Never redirect a transformation back into its own input file: the shell truncates it before the tool reads it.

Awk connects conditions with fields

awk -F '|' '$2 == "fail" { rows++; total += $3 }
  END { printf "rows=%d events=%d\n", rows+0, total+0 }' events.txt

With the supplied fixture: rows=2 events=3.

Matching records and event totals answer different questions. Validate field count and numeric shape before adding untrusted fields. Report rejected rows explicitly.

JSON needs structure-aware selection

jq -c '.checks[] | select(.ok == false)' report.json

With the supplied object, the result is the failed login check object. The filter selects a nested field regardless of layout whitespace.

Use -r only when a raw string output fits the next interface. Embedded newlines in a string are not automatically safe record separators.

Bind data rather than splice a program

label='*.log'
jq -c --arg wanted "$label" '.[] | select(.host == $wanted)' hosts.json

The fixture selects the object whose host is literally *.log. The shell variable becomes jq data, not jq source text.

Interpret JSON result states separately

Case Evidence
Valid JSON, false result with -e Status 1 and false data
Valid JSON, no result with -e Status 4 on tested versions
Malformed JSON Nonzero status and parse diagnostic

Parse success does not establish a schema or complete collection. Empty results do not prove there were no events.

Reconcile on your VM

Then complete the graded lab.

Keep original fixtures, write derived views separately, count accepted and rejected rows, and compare text totals with structured selections. State every format assumption.

Source context: Linuxize grep/sed/awk references and the jq manual linked in the lessons. Fixtures, questions, and interpretations are original adaptations.