Data Extraction with awk
Concept & Overview
Imagine receiving a large spreadsheet saved as plain text, where columns are separated by commas or spaces, and you only need to extract the very first column of data. In Linux, we parse structured columns of text using the powerhouse language awk.
- By default, awk splits lines by whitespace, assigning column 1 to $1, column 2 to $2, and so on.
- The -F flag lets you specify a custom separator (like -F: for colons). E.g., awk -F: '{print $1}' /etc/passwd extracts just the usernames from a colon-separated list.
Why this matters: If you run awk '{print $1}' /etc/shadow without -F:, awk will split lines by whitespace (the default) rather than colons. Since shadow file lines don't have spaces, the entire line is treated as a single field, printing every user's full hash details. The -F: flag forces awk to split columns by colons instead.
/etc/shadow using awk with a colon : separator, and save them into shadow_users.txt using sudo awk -F: '{print $1}' /etc/shadow > shadow_users.txt.Command Syntax Reference
| Command / Syntax | Description |
|---|---|
awk -F: '{print $1}' file | Print first field of colon-delimited file |
Verification Objectives
These conditions will be automatically verified when you run commands inside the interactive terminal simulator:
- ○Use "awk" with field separator flag -F
- ○Output names to "shadow_users.txt"
- ○Check shadow_users.txt has root and learner
Ready to complete Quest 109?
Test your skills live in the browser terminal with an emulated Linux filesystem, real-time command evaluation, and instant step-by-step hints.