AWK Showing Output Flashcards
How do you display AWK output?
Using print
or
printf
Whats the diff between print and printf?
both will print the output, but printf will also do some formatting.
For example, it can show a textual string and format it into a column of a specific size.
It can even strip decimals from floating numbers
How do you search for a pattern using AWK?
awk /[pattern]/
where [pattern] is what you’re searching for.
the more complicated way to write it out is:
awk ‘($1 ~ /^(pattern1|pattern2)/) {print $2}’ filename
Where $1 first field is checked against pattern1 or pattern2 and it will print out the next field ($2) from the selected filename
likely a csv would fit this usecase well
How would you print the Enviornment variables from a user?
Specifically, show the username stored in USER using AWK
awk ‘BEGIN { print ENVIRON[“USER”] }’
How would you determine other environment variables?
See the export command for other environment variables that may be available
What would this output?
~~~
awk -F: ‘BEGIN {
printf “%-20s %s\n”, “Username”, “Home directory”
printf “%-20s %s\n”, “——–”,”————–”}
{ printf “%-20s %s\n”, $1, $(NF-1) }
‘ /etc/passwd
~~~
output:
~~~
Username Home directory
——– ————–
root /root
daemon /usr/sbin
bin /bin
~~~