Linux系統下,不像windows圖形化文件來操做文件,在命令行下如何查看指定文件的某行到指定行之間的命令如何來寫呢?今天咱們將利用head命令和tail命令來實現指定行的查看。
使用head命令和tail命令顯示指定的行:windows
headthis
-n, --lines=[-]K.net
print the first K lines instead of the first 10; with the leading命令行
‘-’, print all but the last K lines of each filedebug
tailorm
-n, --lines=Krem
output the last K lines, instead of the last 10; or use -n +K toget
output lines starting with the Kthit
[root@localhost etc]# pwdio
/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 2 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 2 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