Linux Foundations / Find Evidence in Files

Build a small report from supplied records

Lesson 16 of 37. Open to read. No account needed.

A six-line report can still contain the wrong field, the wrong records or missing repeated values. To trust a small transformation, connect every output line to the source row that supplied it. A count is a useful check, but it is only one part of that evidence.

You will extract and sort owners from a deliberately simple comma-delimited input, preserve one owner occurrence per record, and produce a second report for completed records. The input contract is small enough for cut; it is not a promise to parse arbitrary CSV data.

1. Establish the input contract and owned workspace

Complete Locate files inside an owned directory tree first. Use the ordinary Bash user in the checked Ubuntu guest. Run commands one line at a time, inspect each result, and stop after unexpected errors before continuing.

Use the taught identity and origin checks:

id -u
id -un
ps -p $ -o comm=
type printf set mkdir cat cut sort env grep wc ls rm rmdir

Require the expected nonzero user ID, username and Bash. printf and set must be Bash builtins; the other tools must use their expected guest executables. Preserve an understood color-only listing alias. For grep, reuse the complete lookup checks from the search lesson:

type -a grep
type -P grep

Require only the understood alias adding --color=auto, if present, and the expected executable paths. Stop for a function, builtin, other alias arguments or an unexpected path; forced external lookup alone does not exclude a hidden function. Preserve the accepted configuration.

Use the taught read-only option listing:

set -o

Require errexit off so an observed no-match result can be interpreted without ending an altered practice shell. Preserve and reconcile a different setting instead of changing it here.

The task owns ~/linux-practice/summarize-delimited-records/. Inspect its parents and prospective name without trailing slashes:

ls -ld ~
ls -ld ~/linux-practice
ls -ld ~/linux-practice/summarize-delimited-records

Require ordinary parent directories owned by your recorded account, with no links or unrecognized identity. The exercise name must be absent for a fresh start; use the resume section if it already exists. Only after that check:

mkdir ~/linux-practice/summarize-delimited-records
ls -ld ~/linux-practice/summarize-delimited-records

Require the expected ordinary directory and owner, then enter it separately:

cd ~/linux-practice/summarize-delimited-records
pwd
ls -a

Require the recorded shared-root path followed by /summarize-delimited-records and only . and .. before creating the input.

Exactly three fields per record

Every supplied line has this meaning:

Field positionMeaningContract
1Record IDOne nonempty ID, with no comma.
2StatusOne nonempty lowercase status, with no comma.
3OwnerOne nonempty owner value, with no comma.

There is no header, blank line, extra field, embedded comma, CSV quoting or multiline field. Each record ends in a newline. Spaces could be part of an owner under this contract; a comma could not. The examples below use simple names so you can compare every row directly.

The taught printf '%s\n' supplies six complete records. records.txt must be absent before the taught > creates it:

printf '%s\n' 'D01,done,Nia' 'D02,queued,Omar' 'D03,done,Asha' 'D04,blocked,Nia' 'D05,done,Omar' 'D06,done,Asha' > records.txt

Inspect the saved input and its entire content:

ls -ld -- records.txt
cat records.txt
ls -a

Require a regular file, shown with leading -, one link and the expected owner. It must contain exactly:

D01,done,Nia
D02,queued,Omar
D03,done,Asha
D04,blocked,Nia
D05,done,Omar
D06,done,Asha

Check all three fields in every row. Keep this source unchanged. The guided outputs owners.txt, done-records.txt and done-owners.txt must remain absent until their first creation. The independent task supplies a separate variant input and three separate outputs.

2. Extract the field you actually need

cut selects portions of each input line. In cut -d ',' -f 3 records.txt, -d sets the single-character delimiter to a comma, and -f 3 selects the third field, counting from one. The following filename supplies the input. The comma's quotes are shell grouping; cut receives the comma itself.

Predict the owner sequence in source order before running:

cut -d ',' -f 3 records.txt

Expect:

Nia
Omar
Asha
Nia
Omar
Asha

There is one output line per supplied record. Nia, Omar and Asha each occur twice because two distinct records name each owner. Removing duplicates would answer a different question: unique owners rather than owner occurrences. This report preserves the repeated values.

The simple delimiter contract matters. Cut does not understand CSV quoting that protects an embedded comma. For example, the unsupported illustrative row below is not an input to run in this exercise:

D99,done,"Iyer, Mira"

Its comma inside quotes would still be a delimiter to this cut operation, so the third field would not be the complete intended owner. A header, blank line or extra delimiter also violates this task's contract. Recognize the changed format before choosing a parser; do not silently treat arbitrary CSV as these three-field records.

3. Sort deliberately without changing the session locale

sort reads lines and writes them in sorted order. With no filename operand, it reads stdin, which lets it consume the owner lines from cut. The default sort retains duplicate lines; this task adds no uniqueness option.

Text ordering depends on locale. We choose the C locale for each sorting command so this ASCII fixture has a documented bytewise order. env starts a command with specified environment values. In env LC_ALL=C sort, the LC_ALL=C argument is handled by env and supplied to the launched sort process. It is not a standalone Bash assignment to the parent shell.

LC_ALL overrides the other locale categories for that launched command. The parent shell's locale values and export attributes remain unchanged, including an existing readonly parent variable. Always give env the command shown here; running env alone would display the environment and is not part of this task. No profile edit, bare locale assignment or export is needed.

First inspect the combined read-only transformation:

cut -d ',' -f 3 records.txt | env LC_ALL=C sort

The pipe supplies the extracted owners to sort's stdin. Expect this exact order for the supplied names under the stated C condition:

Asha
Asha
Nia
Nia
Omar
Omar

Compare all six values with the source, including their multiplicities. The sorting step changes order, not which source records contributed values.

Recheck the input identity/content and inspect ls -a; require owners.txt absent before saving the report. Only after those checks:

cut -d ',' -f 3 records.txt | env LC_ALL=C sort > owners.txt

The taught > opens the new report before the command runs. Keep it distinct from the source and never use an input filename as a pipeline output. An upstream error or a plausible final status would not protect an existing output or prove the whole transformation succeeded.

Inspect the saved report and compare the source count:

ls -ld -- owners.txt
cat owners.txt
wc -l -- records.txt
wc -l -- owners.txt

Require the expected regular owned single-link output, the six sorted values above, and six newlines in each file. The counts agree because every source record supplied one owner. Counts alone would not reveal selection of the wrong field, so retain the complete text comparison.

4. Filter one exact status, then repeat the transformation

The taught grep -F searches a literal string. Under this exact three-field contract, the string ,done, identifies the middle status field: it has a comma on each side, and neither the first nor last field can contain an embedded comma. A loose search for done would express a weaker requirement. This delimiter-framed literal filter is valid because of the input contract, not because grep is a general field parser.

We omit line-number prefixes here because the saved intermediate report must retain the original raw records for the next field extraction. Predict the matching record IDs first. Require done-records.txt absent, with the original source still checked, before this new output:

grep -F -- ',done,' records.txt > done-records.txt
printf 'grep status: %s\n' "$?"

Expect status 0. Read it immediately after grep; another command would replace $?. Inspect the intermediate file before connecting another stage:

ls -ld -- done-records.txt
cat done-records.txt

Require a regular owned single-link file containing exactly these four rows:

D01,done,Nia
D03,done,Asha
D05,done,Omar
D06,done,Asha

If the filter returns no matching rows, read the source and immediate status before continuing. A valid no-match result and an input error differ; do not hide either by creating a plausible empty owner report.

Now inspect the extracted owners without saving another output yet:

cut -d ',' -f 3 done-records.txt

Expect Nia, Asha, Omar and Asha in that order. Then inspect ls -a and require done-owners.txt absent before creating the distinct sorted result:

cut -d ',' -f 3 done-records.txt | env LC_ALL=C sort > done-owners.txt

Inspect both the saved result and the selected-record count:

ls -ld -- done-owners.txt
cat done-owners.txt
wc -l -- done-records.txt
wc -l -- done-owners.txt
cat records.txt

The sorted owner report must be Asha, Asha, Nia, Omar, each on its own line. Both selected files have four newlines. Asha remains twice because two selected records name that owner. The original six-row source remains unchanged. The intermediate rows explain which records contributed to the report; the final sorted names alone cannot reconstruct those IDs.

Diagnose a plausible but wrong report

ObservationWhat may have changedSafe next check
Six output lines contain statuses instead of owners.The wrong field number was selected.Compare -f with the three-field map and inspect cut's output before sort.
Whole raw rows appear where owners were expected.The delimiter may be wrong.Compare the actual separators with -d; cut can pass through lines containing no selected delimiter under its default behavior.
An extra owner value appears.A header was included despite the header-free contract.Inspect the complete source before reusing this transformation.
A blank result line appears.A blank or empty-field record may have entered the input.Check every row and its newline/field boundaries against the contract.
Repeated owners disappeared.A uniqueness operation changed the question.Compare occurrences with source rows; this report keeps one value per record.
Order differs from the documented order.The specified locale command or data may differ.Check the exact env LC_ALL=C sort invocation, expected executables and source values; do not persistently change the session locale.
The filtered count is plausible but includes the wrong rows.The literal condition may not match the intended field or input contract.Inspect every selected raw row before extracting owners.

A source with embedded commas or CSV quoting needs a different parsing decision. Do not add increasingly complicated shell filters and claim that this bounded lesson has become a general CSV parser.

Independent task: a four-record variant

Keep all guided inputs and reports unchanged. Recheck the exact exercise path and known directory identity. Require these four new names absent: variant-records.txt, variant-owners.txt, variant-done-records.txt and variant-done-owners.txt.

Create the variant input with the same explained, newline-terminating format:

printf '%s\n' 'V01,done,Zara' 'V02,queued,Asha' 'V03,done,Zara' 'V04,done,Bo' > variant-records.txt

Inspect its identity and complete content before constructing reports:

ls -ld -- variant-records.txt
cat variant-records.txt

Require the regular owned single-link file and all four supplied records, again with exactly three fields and no header, blanks or embedded commas.

Before opening the answer:

  1. Produce the sorted all-owner report in variant-owners.txt, preserving one owner occurrence per record.
  2. Save only the literal done status rows in variant-done-records.txt, check their complete content, then produce variant-done-owners.txt.
  3. Check counts against source rows and compare complete sorted values. Predict which owner repeats and whether that repetition survives filtering.
  4. Diagnose this read-only wrong-field command before deciding whether its output is the requested report:
cut -d ',' -f 2 variant-records.txt | env LC_ALL=C sort

It changes no file, but its line count may still look reasonable. Explain the wrong field, the content it would emit, and the smallest correction. Also explain why an owner containing an embedded comma would invalidate the current input contract even if the source still appeared to have one record per line.

Success: the all-owner report contains Asha, Bo, Zara, Zara; the selected raw records are V01, V03 and V04; the done-owner report contains Bo, Zara, Zara. The respective record/report counts agree at four and three, repeated owners remain, and all guided files stay unchanged.

Graduated hints

  1. Separate the raw-record selection from owner-field extraction. Inspect the intermediate rows so the final count has an explainable source.
  2. Owners are field three. Both sorting commands use the same env LC_ALL=C condition, and all output names differ from their inputs.
  3. Use the literal filter ',done,' for the middle status under this contract. The wrong-field command uses -f 2, which selects status values rather than owners; sorting cannot repair that earlier selection.

Answer and Reasoning

From the verified exercise directory, with the variant source checked and its three output names still absent, one valid first report is:

cut -d ',' -f 3 variant-records.txt | env LC_ALL=C sort > variant-owners.txt

Inspect the report before continuing:

ls -ld -- variant-owners.txt
cat variant-owners.txt
wc -l -- variant-records.txt
wc -l -- variant-owners.txt

Require the expected ordinary owned single-link file containing Asha, Bo, Zara, Zara on four lines. Each of the four source records contributes once. Then create and immediately inspect the filtered raw records:

grep -F -- ',done,' variant-records.txt > variant-done-records.txt
printf 'grep status: %s\n' "$?"

Expect 0. Check the intermediate contents and identity:

ls -ld -- variant-done-records.txt
cat variant-done-records.txt

Require exactly:

V01,done,Zara
V03,done,Zara
V04,done,Bo

Only after that comparison, with the last output name still absent, create and inspect the final report:

cut -d ',' -f 3 variant-done-records.txt | env LC_ALL=C sort > variant-done-owners.txt
ls -ld -- variant-done-owners.txt
cat variant-done-owners.txt
wc -l -- variant-done-records.txt
wc -l -- variant-done-owners.txt

Require Bo, Zara, Zara on three lines, and count three in each selected file. The two Zara occurrences correspond to V01 and V03, so removing one would lose a record contribution. Reinspect the original guided files and confirm their six-row and four-row results have not changed.

The wrong-field command would sort the four status values into done, done, done, queued. Four lines would therefore agree with the source count while still answering the wrong question. Changing field 2 to 3 corrects the selection; changing sort options cannot turn statuses into owners.

For the parsing question, the chosen comma delimiter is reliable only because the contract excludes embedded commas, quoting and multiline fields. An owner containing a comma would be split by this operation even if quote characters surrounded it. Recognize the different format before attempting a report. Valid alternatives must preserve the same record contributions, raw selection, contents, locale condition and owned-output boundaries.

Cleanup after source-to-report checks

Finish the independent work before removing inputs or reports. Reinspect the shared root and exercise entries, rejecting links or unexpected owners:

ls -ld ~/linux-practice
ls -ld ~/linux-practice/summarize-delimited-records

Enter and verify separately:

cd ~/linux-practice/summarize-delimited-records
pwd

Require the exact recorded exercise path. Inspect the inventory and all eight explicit files:

ls -a
ls -ld -- records.txt owners.txt done-records.txt done-owners.txt variant-records.txt variant-owners.txt variant-done-records.txt variant-done-owners.txt

Require only the known regular single-link files owned by the recorded user, plus dot entries. Recheck any content you cannot identify; leave unknown entries untouched.

The shown y/n responses assume English confirmations. Establish the actual program's documented responses before removal and stop if the language or response convention is unclear. Only after all checks:

rm -i -- records.txt owners.txt done-records.txt done-owners.txt variant-records.txt variant-owners.txt variant-done-records.txt variant-done-owners.txt

Approve only known targets. To stop approval, decline the current and every remaining prompt; one refusal does not cancel later prompts. Inspect ls -a and require only . and .. before directory removal.

Return to the shared root and verify its path before the separate removal:

cd ~/linux-practice
pwd

Require the recorded shared-root path, then inspect the exercise entry:

ls -ld -- summarize-delimited-records

Only for that recognized owned empty directory:

rmdir -- summarize-delimited-records
ls -a

Retain the shared root and all other lessons. No parent locale value was changed by the per-command env invocations, so do not unset or restore a locale variable as cleanup.

Resume without replacing unexplained results

After an interruption, recheck the guest identity, command origins, option baseline, owned directories and exact location. Inspect each existing input and report to identify the last completed transformation. A wrong count or field is evidence to explain before deliberately rebuilding a report.

Do not rerun the whole starter block or a redirected pipeline over unknown existing files. For a recognized report from this attempt, inspect and remove only that named output with the taught interactive removal, confirm it absent, then recreate it from the preserved checked input. Keep the source until the report's content and counts agree.

For a complete restart, remove only recognized task files actually created using the same guarded procedure, leave unknown entries untouched, and rebuild from the supplied exact inputs in a fresh empty exercise directory. Do not silently strip headers or malformed rows and claim the original contract held.

Sources and other study views

The commands target the course's Bash 5.2, Coreutils 9.4 and grep 3.11 baseline. The upstream Coreutils reference may describe newer versions; the lesson uses only the bounded forms explained here.

Revision reconstructs the transformations; Quick Reference retrieves their syntax after study.

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy