linux中怎么用shell显示文件某一行或几行内容

您所在的位置:网站首页 shell如何读取文件中某一行的部分内容 linux中怎么用shell显示文件某一行或几行内容

linux中怎么用shell显示文件某一行或几行内容

2024-07-13 21:09| 来源: 网络整理| 查看: 265

https://blog.csdn.net/wuzhiwuweisun/article/details/79136308

声明:从网上看了很多关于这方面的资料,做了一下整合,参考的链接在文章末尾贴出。

 

命令:相关显示命令有sed,tail,cat.awk,head等,可以常看Linux命令大全,man命令或者help命令查看相关用法。我们只介绍其中的一部分。

 

一、sed命令

 

#sed --help;查看具体使用规则:

#sed -n 'xp' filename;显示文件X行命令:   

#sed -n 'x,yp' filename;显示文件X行到Y行的内容:    

 

举例:

sed -n 4,8p file #打印file中的4-8行 sed -n 4p file #打印file中的第4行

 

二、head命令

 

1.命令格式:

 

head [参数]... [文件]...  

 

2.命令功能:

 

head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。 

 

3.命令参数:

 

-q 隐藏文件名

-v 显示文件名

-c 显示字节数 -n 显示的行数

 

4.使用实例:

 

实例1:显示文件的前n行

 

命令:

 

head -n 5 log2014.log

 

输出:

[root@localhost test]# cat log2014.log 2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07 2014-08 2014-09 2014-10 2014-11 2014-12 ============================== [root@localhost test]# head -n 5 log2014.log 2014-01 2014-02 2014-03 2014-04 2014-05[root@localhost test]#

 

实例2:显示文件前n个字节

 

命令:

 

head -c 20 log2014.log

 

输出:

[root@localhost test]# head -c 20 log2014.log 2014-01 2014-02 2014 [root@localhost test]#

 

实例3:文件的除了最后n个字节以外的内容 

 

命令:

 

head -c -32 log2014.log

 

输出:

[root@localhost test]# head -c -32 log2014.log 2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07 2014-08 2014-09 2014-10 2014-11 2014-12[root@localhost test]#

 

实例4:输出文件除了最后n行的全部内容

 

命令:

 

head -n -6 log2014.log

 

输出:

[root@localhost test]# head -n -6 log2014.log 2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07[root@localhost test]#

 

 

三、tail命令

 

tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容. 

 

1.命令格式;

 

tail[必要参数][选择参数][文件]   

 

2.命令功能:

 

用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

 

3.命令参数:

 

-f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c 显示的字节数 -n 显示行数 --pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.  -q, --quiet, --silent 从不输出给出文件名的首部  -s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒   

4.使用实例:  

实例1:显示文件末尾内容  

命令:  

tail -n 5 log2014.log  

说明:  

显示文件最后5行内容

 

输出:

[root@localhost test]# tail -n 5 log2014.log 2014-09 2014-10 2014-11 2014-12 ==============================[root@localhost test]#

 

实例2:循环查看文件内容  

命令:  

tail -f test.log  

说明:  

ping 192.168.120.204 > test.log & //在后台ping远程主机。并输出文件到test.log;这种做法也适用于一个以上的档案监视。用Ctrl+c来终止。

 

输出:

[root@localhost ~]# ping 192.168.120.204 > test.log & [1] 11891[root@localhost ~]# tail -f test.log PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data. 64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms 64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms 64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms 64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms 64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms 64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms 64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms 64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms 64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms 64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms 64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms [root@localhost ~]#

 

实例3:从第5行开始显示文件  

命令:  

tail -n +5 log2014.log  

输出:

[root@localhost test]# cat log2014.log 2014-01 2014-02 2014-03 2014-04 2014-05 2014-06 2014-07 2014-08 2014-09 2014-10 2014-11 2014-12 ============================== [root@localhost test]# tail -n +5 log2014.log 2014-05 2014-06 2014-07 2014-08 2014-09 2014-10 2014-11 2014-12 ==============================

 

四、head命令和tail命令结合

 

举例:取倒数第二行的内容(注意这里的文件是一直在写入数据的,也就是行数是一直在变化的)

 

方法:tail文件的最后两行再管道到head取第一行(“|"是管道符,把前边结果的输出作为后边命令的输入)

 

代码:

Administrator@YLMF-1705132054 /cygdrive/e $ cat command.txt 0 1 2 3 4 Administrator@YLMF-1705132054 /cygdrive/e $ tail -n 2 command.txt | head -n 1 3

 

由于awk命令比较复杂,这里不做介绍了,用到的话可以参考后面的链接博客:戳我!打开链接

 

最后贴出参考的资料和博客地址:

 

1.http://bbs.chinaunix.net/thread-2143504-1-1.html

 

2.http://www.cnblogs.com/peida/archive/2012/11/07/2758084.html

 

3.http://blog.csdn.net/pzasdq/article/details/52895209

 

4.http://www.cnblogs.com/peida/archive/2012/11/06/2756278.html

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3