If you need to read a file and print from a range of line numbers (including the last one), you can use the command as shown below.
cat <filename> | awk 'NR >=linenumber1 && NR <=linenumber2'
To read and display a file along with the line numbers, you can use the following format:.
cat -n <filename>
cat -n sample.txt
To print lines starting from line 2 to line 7, then you can use the following command.
cat sample.txt | awk 'NR >=2 && NR <=7'
If for any reason you need to display the line numbers along with the output, then you can pass the -n argument to cat before piping it to awk as shown below.
cat -n sample.txt | awk 'NR >=2 && NR <=7'
[Note: There are multiple methods to do the same procedure. This method is intended for beginners who are just getting started in Linux.]
Hope this helps!
If this article helped you, do leave a comment below and like us on Twitter and Facebook. Cheers!