>_ IzzyLab

Hands-on task

This task is step 14 of the course “Text in the terminal: grep, sed, awk”

IP addresses from a server table

easy~3 min

/root/inventory.txt holds a server table: name ip role, the columns are aligned with spaces (there can be several spaces between them). Write only the IP addresses (the second field) to /root/ips.txt — one per line, in the original order.

What you'll practise:
  • awk
  • fields $1 $2
  • print
  • aligned columns

Topic breakdown

By default awk splits a line into fields on whitespace and treats a run of spaces or tabs as a single separator, so it reads tables aligned "by eye" without any setup. cut can't do that: with a space delimiter every extra space produces an empty field, and the column numbers "shift". The program {print $2} runs for every line and prints its second field.

What's checked

  1. /root/ips.txt has the addresses in the original order

What it looks like