How to get a specific line or range of lines’ output from a file in linux ?

In this post, we will look how to get a specific line or a range of lines’ output from a file in linux.

We will look at these options step by step.

Create a sample file say testfile1 in our case.

saket@ngelinux$ cat testfile1 
first
second
third
fourth
fifth
saket@ngelinux$ 

1. To get a specific line from a file

saket@ngelinux$ sed -n 3p testfile1 
third

2. Get range of lines output

saket@ngelinux$ sed -n 1,3p testfile1 
first
second
third

3. Get multiple specific lines.

saket@ngelinux$ sed -n '1p;3p' testfile1 
first
third

In the similar fashion, you can get any number of required lines from a file/output in linux.

Leave a Reply

Your email address will not be published.