首页 > 系统服务 > 详细

Display certain line(s) from a text file in Linux.

时间:2018-03-03 14:30:14      阅读:215      评论:0      收藏:0      [点我收藏+]

Purpose:

Display certain line or lines from a text file, such as :

Display the 1000th line from file message.log

or

Display the lines between 1000 and 1020 from file message.log

Solution:

Using sed:

sed -n ‘1000,1020p‘ message.log
sed -n ‘1000,1020p; 1021q‘ message.log #break after read out 1020th line.

Using awk:

awk ‘{if ((NR >= 1000) && (NR <= 1020)) print $0}‘ message.log

Using cat && grep:

cat -n message.log | grep -A 20 ‘^ *1000‘

Using nl && grep:

nl message.log | grep -A 20 ‘^ *1000‘

Using tail && head:

tail -n +1000 message.log | head 20
head -n 1000 message.log | tail +20

Ref:

  1. http://serverfault.com/questions/133692/how-to-display-certain-lines-from-a-text-file-in-linux

Display certain line(s) from a text file in Linux.

原文:https://www.cnblogs.com/zhchoutai/p/8496481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!