Shaping text with awk and sed

These two tools sit in the middle of a pipeline and change what passes through. sed edits the line as a whole, and awk takes it apart into fields. Knowing which of the two you want is most of the skill.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 What does the g flag do in sed 's/cat/CAT/g' words.txt?
2 What does awk '{print $NF}' scores.txt print?
3 What does awk -F: '{print $2}' servers.txt do?
4 In awk '{sum+=$1} END {print sum}' numbers.txt, when does the END block run?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Pull one field out of a colon-separated file

This writes a small inventory file, one machine per line, with a name, an address and a port separated by colons. It then prints a single field from every line.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

printf "web1:10.0.0.11:8080\nweb2:10.0.0.12:8080\ndb1:10.0.0.21:5432\n" > ~/servers.txt; awk -F: '{print $2}' ~/servers.txt
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson