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

本文介绍了在Linux环境下使用多种工具和技术来从文本文件中显示特定行的有效方法。包括使用sed、awk、cat结合grep、nl结合grep以及tail和head等命令组合实现的功能。这些方法适用于快速定位和查看大型日志文件中的指定行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值