How do I find the nth line in a file in Linux command line? How do I display line number x to line number y?
In Linux, there are several ways to achieve the same result. Printing specific lines from a file is no exception.
To display 13th line, you can use a combination of head and tail:
head -13 file_name | tail +13
Or, you can use sed command:
sed -n '13p' file.txt
To display line numbers from 20 to 25, you can combine head and tail commands like this:
head -25 file_name | tail +20
Or, you can use the sed command like this:
sed -n '20,25p' lines.txt
Detailed explanation of each command follows next. I'll also show the use of awk command for this purpose.
Display specific lines using head and tail commands
This is my favorite way of displaying lines of choice. I find it easier to remember and use.
Print a single specific line
Use a combination of head and tail command in the following function the line number x:
head -x file_name | tail +x
You can replace x with the line number you want to display. So, let's say you want to display the 13th line of the file.
[email protected]:~$ head -13 lines.txt | tail +13 This is line number 13
The “head -x” part of the command will get the first x lines of the files. It will then redirect this output to the tail command. The tail command will display all the lines starting from line number x.
Quite obviously, if you take 13 lines from the top, the lines starting from number 13 to the end will be the 13th line. That’s the logic behind this command.
Print specific range of lines
Now let's take our combination of head and tail commands to display more than one line.
Say you want to display all the lines from x to y. This includes the xth and yth lines also:
head -y lines.txt | tail +x
Let's take a practical example. Suppose you want to print all the the lines from line number 20 to 25:
[email protected]:~$ head -25 lines.txt | tail +20 This is line number 20 This is line number 21 This is line number 22 This is line number 23 This is line number 24 This is line number 25
Use SED to display specific lines
The powerful sed command provides several ways of printing specific lines.
For example, to display the 10th line, you can use sed in the following manner:
sed -n '10p' file.txt
The -n suppresses the output while the p command prints specific lines. Read this detailed SED guide to learn and understand it in detail.
To display all the lines from line number x to line number y, use this:
[email protected]:~$ sed -n '3,7p' lines.txt This is line number 3 This is line number 4 This is line number 5 This is line number 6 This is line number 7
Use AWK to print specific lines from a file
The awk command could seem complicated and there is surely a learning curve involved. But like sed, awk is also quite powerful when it comes to editing and manipulating file contents.
[email protected]:~$ awk 'NR==5' lines.txt This is line number 5
NR denotes the 'current record number'. Please read our detailed AWK command guide for more information.
To display all the lines from x to y, you can use awk command in the following manner:
[email protected]:~$ awk 'NR>=20 && NR<=25' lines.txt This is line number 20 This is line number 21 This is line number 22 This is line number 23 This is line number 24 This is line number 25
It follows a syntax that is similar to most programming language.
I hope this quick article helped you in displaying specific lines of a file in Linux command line. If you know some other trick for this purpose, do share it with the rest of us in the comment section.