Forrest logo
back to the awk tool

awk:tldr:89541

awk: Print every third line starting from the first line.
$ awk 'NR%3==1' ${filename}
try on your machine

The command you provided uses the awk utility to filter and manipulate text. Here is how it works:

awk is a versatile text processing tool that operates on each line of a given input file(s) or input from a pipeline. It uses specifically defined patterns and actions to perform various operations.

In your command:

  • 'NR%3==1' is the pattern that specifies a condition that a line must meet for further processing.
    • NR is a built-in variable in awk that represents the current line number being processed.
    • % is the modulo operator which gives the remainder of division.
    • 3 is the number we are taking the modulo with.
    • ==1 checks if the remainder is equal to 1. In other words, it checks if the current line number is divisible by 3 with a remainder of 1. So, this pattern selects every line whose line number (NR) modulo 3 is equal to 1.

${filename} is the variable that represents the filename or a wildcard expression representing multiple files to be processed by awk. You can replace ${filename} with the actual name of a file or a wildcard expression to match multiple files.

Therefore, the command filters out lines from the specified file(s) and prints only those lines whose line number is divisible by 3 with a remainder of 1.

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