count number of lines bash code example

Example 1: shell count number of lines

$ wc -l < /dir/file.txt
3272485

Example 2: bash count the number of lines that meet condition

# Basic syntax:
awk 'condition { count_variable++ } END { print count_variable }' input_file
# Where:
#	- ++ increases the count by 1 when the condition is true
#	- END specifies what to do after the input_file has been processed

# Example usage:
awk '$3=="NA" { count++ } END { print count }' input_file
# This returns the count of the lines in column/field 3 that are NA

# Example usage:
awk '$1=="word" && $2==42 { count++ } END {print count}' input_file
# This returns the count of the lines that are "word" in column 1 AND
# 42 in column 2