Linux Commands SERVER MANAGEMENT

How to print a specific line from a file in Linux/Unix

Unix/Linux administrator or those who work on shell scripts often face a situation to print or display a specific line from a file.  There are many ways to achieve it.  Will discuss the most commonly used commands to display lines using sed, head, tail and awk commands.

Display 15th line from a file using sed :

 
sed -n 15p <filename>

Display 15th  to 25th line from a file using sed :

 
sed –n 15,20p <filename>

Display 15th line from a file using head and tail :

 
head -15 <filename> | tail -1

Display 15th  to 25th line from a file using head/tail :

 
head -25 <filename> | tail -11

Display 15th  to 25th line from a file using head/tail in Solaris :

 
tail +15 <filename> | head -11

Display 15th line from a file using awk :

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

Display 15th  to 25th line from a file using awk :

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

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