

To display all the lines from x to y, you can use awk command in the following manner: :~$ awk 'NR>=20 & NR<=25' lines.txt Please read our detailed AWK command guide for more information. But like sed, awk is also quite powerful when it comes to editing and manipulating file contents. The awk command could seem complicated and there is surely a learning curve involved. This is line number 7 Use AWK to print specific lines from a file To display all the lines from line number x to line number y, use this: :~$ sed -n '3,7p' lines.txt Read this detailed SED guide to learn and understand it in detail.

The -n suppresses the output while the p command prints specific lines. The powerful sed command provides several ways of printing specific lines.įor example, to display the 10th line, you can use sed in the following manner: sed -n '10p' file.txt This is line number 25 Use SED to display specific lines Suppose you want to print all the the lines from line number 20 to 25: :~$ head -25 lines.txt | tail +20 This includes the xth and yth lines also: head -y lines.txt | tail +x Say you want to display all the lines from x to y. Now let's take our combination of head and tail commands to display more than one line. Quite obviously, if you take 13 lines from the top, the lines starting from number 13 to the end will be the 13th line. The tail command will display all the lines starting from line number x. It will then redirect this output to the tail command.

The “head -x” part of the command will get the first x lines of the files. :~$ head -13 lines.txt | tail +13Įxplanation: You probably already know that the head command gets the lines of a file from the start while the tail command gets the lines from the end. So, let's say you want to display the 13th line of the file. You can replace x with the line number you want to display. Use a combination of head and tail command in the following function the line number x: head -x file_name | tail +x This is my favorite way of displaying lines of choice. Display specific lines using head and tail commands I'll also show the use of awk command for this purpose. Or, you can use the sed command like this: sed -n '20,25p' lines.txtĭetailed explanation of each command follows next. 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 sed command: sed -n '13p' file.txt To display 13th line, you can use a combination of head and tail: head -13 file_name | tail +13 Printing specific lines from a file is no exception. In Linux, there are several ways to achieve the same result. 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?
