This short tutoral shows how to show line numbers in your grep output. If you’re here you will already know grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Grep stands for Global regular expression print.
By default, grep doesn’t display the line number in it’s output when it has matched a pattern. For example:
grep location variables.tf
variable "location" {
Adding line numbers to the grep output is straight forward – you just need to include the -n
option in the grep command. For example:
grep -n location variables.tf
11:variable "location" {
This time the output has included the line number, in this case line 11.
Note the -n
is a short way of using --line-number
grep --line-number location variables.tf
11:variable "location" {
Being able to show line numbers in grep output is particularly useful if you are searching through code.