Search

Disaplying specific number of lines at a time from a file.

Disaplying specific number of lines from a file.

The command cat displays the complete contents of the file at a time, but in case you want to view a specific number of lines at a time

Here are a few methods to do it.

1. more :

This is probably the simplest way, the command more by default displays one page of contents at a time, to restrict the number of lines we need to pass the number as an option.

If we have a file by the name "test" with the contents



If we want to view one line at a time then, we can run more as follows



Each time you press enter the next line gets displayed, the blank lines are also treated as a line. You can change the "-1" to any number of your choice.

2. Here is a bash script that tries to achive the same as shown above.

The only way this script is better than more is that it is a little more interactive, in all other respects more is a better option, this is just for fun.



Let us save script lines.sh .

Give execute permission using



The script takes one filename as input and displays the count of number of lines present in it, before asking the user to enter the number of lines he/she wants to view at a time.

After displaying the number of lines user requested for it will prompt if the user wants to continue on reciving a confirmation it will display the next set of lines and so on till the end of the file.

If the user wants to stop in between he/she can say no when the scripts prompts for confirmation, on which the script will give the number of lines displayed and exit.

Here is a trial run on the same file used above.

Follow by Email