在linux中使用head命令和tail命令查看文件中的指定行

在linux中使用head命令和tail命令查看文件中的指定行

  (2011-04-16 12:20:16)
标签: 

杂谈

分类: Linux

 

使用head命令和tail命令显示指定的行:

head

-n, --lines=[-]K

              print the first lines instead of the first 10; with  the  leading

              ‘-’, print all but the last lines of each file

tail

-n, --lines=K

              output  the  last  lines, instead of the last 10; or use -n +K to

              output lines starting with the Kth

 

[root@localhost etc]# pwd

/etc

[root@localhost etc]# cat yum.conf

[main]

cachedir=/var/cache/yum/$basearch/$releasever

keepcache=0

debuglevel=2

logfile=/var/log/yum.log

exactarch=1

obsoletes=1

gpgcheck=1

plugins=1

installonly_limit=3

//此处是一空行

 This is the default, if you make this bigger yum won't see if the metadata

is newer on the remote and so you'll "gain" the bandwidth of not having to

download the new metadata and "pay" for it by yum not having correct

information.

 It is esp. important, to have correct metadata, for distributions like

Fedora which don't keep old packages around. If you don't like this checking

interupting your command line usage, it's much better to have something

manually check the metadata once an hour (yum-updatesd will do this).

metadata_expire=90m

//此处是一空行

PUT YOUR REPOS HERE OR IN separate files named file.repo

in /etc/yum.repos.d

[root@localhost etc]# cat yum.conf |wc -l

23

yum.conf文件共有23

 

[root@localhost etc]# head yum.conf

[main]

cachedir=/var/cache/yum/$basearch/$releasever

keepcache=0

debuglevel=2

logfile=/var/log/yum.log

exactarch=1

obsoletes=1

gpgcheck=1

plugins=1

installonly_limit=3

显示前10行,这个是默认值

 

[root@localhost etc]# head -n yum.conf

[main]

cachedir=/var/cache/yum/$basearch/$releasever

显示前2

 

[root@localhost etc]# head -n -20 yum.conf

[main]

cachedir=/var/cache/yum/$basearch/$releasever

keepcache=0

显示除最后20行以外的所有行

 

[root@localhost etc]# tail yum.conf

download the new metadata and "pay" for it by yum not having correct

information.

 It is esp. important, to have correct metadata, for distributions like

Fedora which don't keep old packages around. If you don't like this checking

interupting your command line usage, it's much better to have something

manually check the metadata once an hour (yum-updatesd will do this).

metadata_expire=90m

//此处是一空行

PUT YOUR REPOS HERE OR IN separate files named file.repo

in /etc/yum.repos.d

显示最后10行,这是默认值

 

[root@localhost etc]# tail -n yum.conf

PUT YOUR REPOS HERE OR IN separate files named file.repo

in /etc/yum.repos.d

显示最后2

 

[root@localhost etc]# tail -n +21 yum.conf

//此处是一空行

PUT YOUR REPOS HERE OR IN separate files named file.repo

in /etc/yum.repos.d

从第21行开始显示后面的所有行,即第21行到第23

 

显示第11行到第20(总共23)

从第11行开始显示,但不包括最后3

[huage@localhost etc]$ head -n -3 yum.conf |tail -n +11

 

显示前20行,但从第11行开始

[huage@localhost etc]$ head -n 20 yum.conf |tail -n +11

 

显示除最后3行以外的所有行,但只显示最后10

[huage@localhost etc]$ head -n -3 yum.conf |tail -n 10

 

显示前20行中的后10

[huage@localhost etc]$ head -n 20 yum.conf |tail -n 10

 

从第11行开始显示,但只显示前10

[huage@localhost etc]$ tail -n +11 yum.conf |head -n 10

 

从第11行开始显示,但不包括最后3

[huage@localhost etc]$ tail -n +11 yum.conf |head -n -3

 

显示最后13行中的前10

[huage@localhost etc]$ tail -n 13 yum.conf |head -n 10

 

显示最后13行中除末尾的3行以外的前10

[huage@localhost etc]$ tail -n 13 yum.conf |head -n -3

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值