How to display line numbers in Terminal while displaying content of a file


In order to display the contents of a file in macOS/Linux/Unix Terminal you can make use of cat command - read more

Sample File:
Lorem ipsum dolor sit amet
ut mea nostrum detracto quaestio. 
Eu nobis denique fabellas est,
clita liberavisse duo an.
Nam vidit falli disputationi eu
usu nonumy nostro eirmod no.
Liber dictas incorrupte eum ex.
Qui illum singulis ei, 
duo te democritum disputationi, 
facer iisque aliquid ad eos.

In order to display the line numbers along with the text on the console we can make use of option -n along with the cat command and file name,

Example:
$ cat -n myfile.txt
     1	Lorem ipsum dolor sit amet
     2	ut mea nostrum detracto quaestio. 
     3	Eu nobis denique fabellas est,
     4	clita liberavisse duo an.
     5	Nam vidit falli disputationi eu
     6	usu nonumy nostro eirmod no.
     7	Liber dictas incorrupte eum ex.
     8	Qui illum singulis ei, 
     9	duo te democritum disputationi, 
    10	facer iisque aliquid ad eos.
Display line number in terminal for text file
Display line number in terminal for text file
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.