>_ IzzyLab

Hands-on task

This task is step 12 of the course “awk and datamash: reports from tables”

awk: lines between markers

medium~5 min

In /root/log.txt there is a block between the marker lines --START-- and --END--. Write an awk program in the file /root/between.awk (handy to do in the Editor tab above the terminal): it prints the lines between the markers, not including the markers themselves. Run it with awk -f and write the result to /root/body.txt. The program will also be tested on another file — one with several blocks.

What you'll practise:
  • awk
  • state flag
  • line ranges
  • regular expressions
  • next

Topic breakdown

Extracting a block between markers is a two-state finite automaton, where the flag variable remembers whether we are "inside" the block. The marker lines are recognized by regular expressions and toggle the flag, while not ending up in the output themselves. In awk, conditional printing is expressed with a "bare" pattern and no action: a true expression means "print the current line in full". When there's more than one rule, it's handier to keep the program in a separate file and run it with awk -f: you can read it with indentation and comments, and the shell's quotes don't get in the way.

What's checked

  1. /root/body.txt holds only the lines between the markers
  2. between.awk works on another file

What it looks like