Linux Commands SERVER MANAGEMENT

How to print a specific line from a file in Linux

print a specific line
print a specific line

Print a specific line from a file in Linux

Are you looking for ways to print or display a specific line from a file on your Linux system? As a Unix/Linux administrator or someone who works on shell scripts, you may come across this situation often. In this article, we’ll discuss some of the most commonly used commands to display lines from files, including sed, head, tail, and awk commands.

To display the 15th line from a file using sed, you can use the following command:

sed -n 15p <filename>

If you need to display lines 15 to 25 from a file using sed, you can use this command instead:

sed –n 15,20p <filename>

Alternatively, you can also use head and tail commands to achieve the same result. To display the 15th line from a file using head and tail, use this command:

head -15 <filename> | tail -1

For lines 15 to 25, you can use this command:

head -25 <filename> | tail -11

If you’re using Solaris, you can use the following command instead to display lines 15 to 25:

tail +15 <filename> | head -11

Lastly, you can use awk command to print a specific line from a file. To display the 15th line from a file using awk, use this command:

awk "NR==15{print;exit}" <filename>

For lines 15 to 25, use this command:

awk "NR>=15{print} NR==25{exit}" <filename>

Hopefully, this guide will help you in your future endeavors of printing specific lines from files on your Linux system.

Read More:

If you like the post and wish to receive more articles from us, please like our FB page: If you like this post and wish to receive more articles from us, please like our FB page: Button
Your suggestions and feedbacks will encourage us and help to improve further, please feel free to write your comments.

For more details on our services, please drop us an E-mail at info@grepitout.com

1 Comment

Click here to post a comment

  • Have you ever thought about creating an ebook or guest authoring on other sites?
    I have a blog based on the same topics you discuss and would
    love to have you share some stories/information.
    I know my audience would appreciate your work. If you are even remotely interested, feel free to send me an email.

Topics