Linux sed and awk Cheat Sheet/Print line numbers with lines

Prefix each line with NR.

Section: awk Fields and Records

Print line numbers with lines

bash
bash
awk '{print NR ": " $0}' file.txt
Explanation

`NR` is the current record number across all input.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing awk Fields and Records
Print selected columns
Show the first and third whitespace-delimited fields.
OpenIn sheetbashsame section
Use a custom field separator
Split CSV-like data on commas.
OpenIn sheetbashsame section
Print the last field
Use NF to reference the final field on each line.
OpenIn sheetbashsame section
Set output field separator
Join output fields with commas.
OpenIn sheetbashsame section
Print rows matching a condition
Show lines where column 3 is greater than 100.
OpenIn sheetbash1 tag match
Use BEGIN and END blocks
Print headers and footers around processed output.
OpenIn sheetbash1 tag match