nl Command in Linux: Number Lines in Text Files in Terminal
Linuxcommands
The nl command in Linux (number lines) is used to number the lines of a file. The command considers special conditions for the header and footer of the file.
The frequently used options are:
- -h sub-option: Used to format the text header. The default is not to number the header;
- -b sub-option: Used to format the body of the text. The default is to number only non-empty lines;
- -f sub-option: Used to format the text footer. The default is not to number the footer.
The sub-options are:
- A: Number all lines;
- t: Number only the filled ones;
- n: Do not number the lines.
Example:
Suppose that arquivo.txt has the following content:
\:\:\:
Relatório de Notas e Frequência dos alunos de Engenharia de Software
—————————————————————————————————
Nome Nota Freq. Resultado
—————————————————————————————————
\:\:
Carlos Torres 8,5 80% Aprovado
José Antônio 10,0 100% Aprovado
Maria de Lourdes 10,0 100% Aprovado
Mário Cabral 9,5 100% Aprovado
\:
—————————————————————————————————
And using the nl command:
$ nl arquivo.txt
The result will be:
Relatório de Notas e Frequência dos alunos de Engenharia de Software—————————————————————————————————Nome Nota Freq. Resultado—————————————————————————————————1 Carlos Torres 8,5 80% Aprovado2 José Antônio 10,0 100% Aprovado3 Maria de Lourdes 10,0 100% Aprovado4 Mário Cabral 9,5 100% Aprovado—————————————————————————————————
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.