We can use awk to print different columns. By default it uses “space” as a column delimiter, but we can specify a different one with the -F option:
awk -F ":" '{print $1}' /etc/passwdthis is print the first column of the passwd file. Note the use of single quotes '' in the second argument.
We can also change what the separator looks like if we want to simply replace it:
awk 'BEGIN{FS=":":OFS="->"}' '{print $1,$7}' /etc/passwdWe can also print the last field in a file and specify the separator. Consider for example that we might have the /etc/shells file:
/bin/sh
/bin/bash
/bin/rbash
/usr/bin/git-shell
/usr/bin/fish
/bin/fish
/usr/bin/rbash
/usr/bin/bash
/usr/bin/sh
/usr/bin/systemd-home-fallback-shellIf we want to print out only the shells, without their full path, we might do the following:
awk -F "/" '/^\// {print $NF}' /etc/shells | uniqWe can also do fairly complete scripting in awk. For example, we may use length() < x as a function, or use if statements:
ps -ef | awk '{ if($NF == "/bin/fish") print $0}'A for loop can be constructed using syntax similar to JS:
awk 'BEGIN { for(i=1; i<=10; i++) print "The square root of", i, "is", i*i}'We can define the Field separators using
awk 'BEING{OFS=","} {print $1,$3,$4}' /etc/passwd | head -n 3This will use commas to separate fields, effectively printing out a CSV.