Forrest logo
back to the awk tool

awk:tldr:e6726

awk: Print the second column of the lines containing "foo" in a space-separated file.
$ awk '/${foo}/ {print $2}' ${filename}
try on your machine

The command awk '/${foo}/ {print $2}' ${filename} is using the awk command-line tool to search for lines in ${filename} (a file) that match the pattern set by the value of ${foo} (a variable).

Let's break down the command:

  • ${foo} is a placeholder for a variable's value. In this case, it represents a pattern that will be matched.

  • /.../ is used to delimit the pattern in awk. So /${foo}/ means that the pattern is the value of ${foo}.

  • {print $2} is the action to be executed when a line matches the pattern. $2 refers to the second field (a column) of the matching line, and print is used to output that field.

  • ${filename} represents the name of the file being processed by awk.

So, in summary, the command searches ${filename} for lines that contain the pattern ${foo} and prints the second field of each matching line.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the awk tool