Q1What does awk '{print $1}' f output?
awk — Field Extraction
Practice awk: '{print $1}' pulls out a column, $NF grabs the last column, -F',' switches the separator to a comma, NR and NF, and /error/{print $2} processes only matching lines — illustrated and hands-on in a browser terminal.
Pulling Out Columns — $1 / $NF / -F
awk is a command that splits each line into columns (fields) by whitespace and lets you process them column by column. Writing awk '{print $1}' pulls out and prints just the first column of each line. Inside {} you write the action to run on each line (here, print). $1 is the first column, $2 is the second, and $0 refers to the whole line.
$NF is a special way to refer to the last column. NF is a variable holding the number of fields on a line, so $NF gives you that line's final column. Even when the column count differs from line to line, you always get the last one. When the separator is not whitespace, specify it with -F, as in -F','. It's safest to wrap the separator in quotes (-F',').
printf 'alice 30 tokyo\nbob 25 osaka\n' > users.txt # create the material
awk '{print $1}' users.txt # column 1: alice and bob
awk '{print $2}' users.txt # column 2: 30 and 25
awk '{print $NF}' users.txt # last column: tokyo and osaka
printf 'alice,30\nbob,25\n' > csv.txt # comma-separated material
awk -F',' '{print $1}' csv.txt # comma separator, column 1
$1 is the first column, and $NF points to that line's last column.| Form | Meaning | Example |
|---|---|---|
$1 | Pull out column 1 | awk '{print $1}' users.txt |
$2 | Pull out column 2 | awk '{print $2}' users.txt |
$NF | Pull out the last column | awk '{print $NF}' users.txt |
$0 | Pull out the whole line | awk '{print $0}' users.txt |
-F',' | Change the separator to a comma | awk -F',' '{print $1}' csv.txt |
NR | Current line number | awk '{print NR, $0}' nf.txt |
NF | Number of fields on the line | awk '{print NF}' nf.txt |
/pat/{print $1} | Print column 1 of lines matching pat | awk '/error/{print $1}' log.txt |
Line Number and Field Count — NR / NF
NR is a built-in variable holding the number of the line currently being processed, and NF holds the field count of that line. Writing awk '{print NR, $0}' prints each whole line with its line number in front. With awk '{print NF}' you can see how many columns each line was split into.
printf 'red\ngreen blue\n' > nf.txt # create the material
awk '{print NR, $0}' nf.txt # 1 red / 2 green blue
awk '{print NF}' nf.txt # line 1 has 1 column, line 2 has 2
Processing Only Matching Lines — /pat/{print ...}
When you write a pattern before the program, awk applies that program only to the matching lines. awk '/error/{print $1}' prints column 1 only for lines containing error. Where grep shows the whole line, awk can pull just the columns you need from the matching lines.
printf 'error disk\ninfo start\nerror cpu\n' > log.txt # create the material
awk '/error/{print $2}' log.txt # column 2 of error lines: disk and cpu
Knowledge Check
Answer each question one by one.
Q2Which option do you add to make awk use a comma as the separator?
Q3Which lines does awk '/error/{print $1}' f process?