Skip to main content
Techlogia — AI and Web Development Berlin
All courses
Free to read – no sign-up

Working with CSV data

Create a CSV table and filter rows with Linux tools (grep, awk, sort). The validator reads the result file.

Duration: 25 minLevel: BeginnerExercises: 5

Create and filter a table

Analysing CSV data

CSV ("Comma-Separated Values") is the simplest table format: plain text, columns separated by commas, one line per record. Excel, databases and APIs exchange data this way. You analyse CSV with the Linux tools grep, wc, cut and sort.

Key terms & tools

  • Header: the first line with the column names.
  • grep: filters lines containing a pattern.
  • wc -l: counts lines.
  • cut -d, -f1: cuts out the 1st column (delimiter ,).
  • sort: sorts lines alphabetically.

Work in the folder /home/student/csv/ (mkdir -p /home/student/csv).

Your goal

You create a CSV table and analyse it: filter, count, extract columns, sort.

Exercises

  1. 1. Create a CSV

    Concept: CSV structure. First line = column names, then one line per record. Create a table schueler.csv with the header name,klasse,alter and at least four data lines.

    mkdir -p /home/student/csv
    printf "name,klasse,alter\nMax,7a,13\nLisa,7b,12\nTom,7a,13\nAnna,8a,14\n" > /home/student/csv/schueler.csv

    Check: schueler.csv starts with name,klasse,alter and has at least 4 data lines with 3 columns each.

  2. 2. Filter for class 7a

    Concept: filtering lines with grep. Extract all students of class 7a and write them to klasse-7a.csv.

    grep ",7a," /home/student/csv/schueler.csv > /home/student/csv/klasse-7a.csv

    The pattern ,7a, matches the class column exactly (between two commas).

    Check: klasse-7a.csv contains lines with ,7a,.

  3. 3. Total number of students

    Concept: counting lines. Count the data lines (excluding the header) and write only the number to anzahl.txt. tail -n +2 skips the first line.

    tail -n +2 /home/student/csv/schueler.csv | wc -l > /home/student/csv/anzahl.txt

    Check: anzahl.txt contains exactly one number.

  4. 4. Extract names only

    Concept: extracting a column with cut. cut -d, -f1 cuts out the 1st column (comma delimiter). Get only the names (without header) into namen.txt.

    tail -n +2 /home/student/csv/schueler.csv | cut -d, -f1 > /home/student/csv/namen.txt

    Check: namen.txt contains at least four lines, each with one name.

  5. 5. Sort students alphabetically

    Concept: sorting while keeping the header. Sort the students alphabetically by name, but keep the header on top. Trick: output the header separately, sort the rest.

    ( head -1 /home/student/csv/schueler.csv; tail -n +2 /home/student/csv/schueler.csv | sort ) > /home/student/csv/sortiert.csv

    Check: sortiert.csv starts with the header name,klasse,alter, followed by the sorted lines.

Related courses

Now practice it yourself

Reading is good – doing is better. Start this course on a real Linux VM, right in your browser. A free account is all it takes.

Start for free

Lab content under CC BY 4.0 – free to use with attribution (© TechLogia).

How do you like this page?

Working with CSV data | Techlogia Lab