awktutorial/03-field-separators.awk

27 lines
975 B
Awk
Executable File

#!/usr/bin/awk -f
# https://www.grymoire.com/Unix/Awk.html
# when awk reads a file line by line, it also provides you with
# variables representing the "fields" in each line.
# so, awk processes input line by line, but it makes it simple for you
# to process each line field by field, or to pick out any subset of
# fields that you want to crunch.
# by default, fields are words separated by whitespace, and they are
# represented by the variables $1, $2, $3 and so on.
# since we're practicing with /etc/password as the input file, whitepace
# separators won't work for us. fields in /etc/passwd are separated
# by colons. awk allows us to specify an alternate field separator
# by setting the FS variable.
# in this example, we'll change FS to a colon in the BEGIN block, and
# we will then print fields 1, 6 and 7 from each line of the input file.
# these are the username, their home directory path, and their shell.
BEGIN { FS=":" }
{ print $1, $6, $7 }