查看大量日志文件以进行故障排除是系统管理员和程序员的日常工作。
在本文中,让我们回顾如何使用10个很棒的示例来有效地查看和操作大型日志文件。
示例1:使用sed命令显示文件的特定行(基于行号)
仅查看行号提及的特定行。
Syntax: $ sed -n -e Xp -e Yp FILENAME
- sed : sed command, which 将print all the lines 通过 default.
- -n:抑制输出。
- -e CMD:要执行的命令
- Xp:打印行号X
- Yp:打印行号Y
- FILENAME:要处理的文件的名称。
的example mentioned below 将print the lines 120, 145, 1050 from the syslog.
$ sed -n -e 120p -e 145p -e 1050p /var/log/syslog
在以下示例中,您可以从行号101到110查看var / log / cron的内容。
- M –起始行号
- N – Ending line number
Syntax: sed -n M,Np FILENAME $ sed -n 101,110p / var / log / cron
示例2:使用head命令显示文件的前N行
本示例仅显示/ var / log / maillog文件的前15行。将15更改为10以显示日志文件的前10行。
Syntax: head -n N FILENAME $ head -n 15 /var/log/maillog
示例3:使用head命令忽略文件的最后N行
本示例说明如何忽略最后N行,并仅显示文件顶部的其余行。
的following example 将display all the lines of the /var/log/secure except the last 250 lines.
Syntax: head -n -N FILENAME $ head -n -250 /var/log/secure
示例4:使用tail命令显示文件的最后N行
本示例仅显示/ var / log / messages文件的最后50行。将50更改为100,以显示日志文件的最后100行。
Syntax: tail -n N FILENAME $ tail -n 50 /var/log/messages
示例5:使用tail命令忽略文件的前N-1行
本示例说明如何忽略前N-1行,仅显示其余行。
的following example ignores the 1st four lines of the /etc/xinetd.conf, which contains 上ly the 评论.
Syntax: tail -n +N FILENAME $ tail -n +5 /etc/xinetd.conf defaults { instances = 60 log_type = SYSLOG authpriv log_on_success = HOST PID log_on_failure = HOST cps = 25 30 } includedir /etc/xinetd.d
示例6:使用tail命令实时查看增长的日志文件
这可能是sysadmins最常用的命令之一。要查看不断增长的日志文件并仅查看更新的内容,请使用tail -f,如下所示。
的following example shows the content of the /var/log/syslog command in real-time.
Syntax: 尾-f FILENAME $ 尾-f /var/log/syslog
示例7:使用head和tail命令显示文件的特定行(基于行号)
的example below 将display line numbers 101 –/var/log/anaconda.log文件中的110
- M –起始行号
- N – Ending line number
Syntax: cat 科幻le | tail -n +N | head -n (M-N+1) $ cat /var/log/anaconda.log | tail -n +101 | head -n 10
- cat:将整个文件打印到标准输出。
- tail -n +101:忽略直到给定行号的行,然后在给定行号之后开始打印行。
- head -n 10:打印前10行,即101到110,并忽略其余行。
示例8:显示与模式匹配的行,以及匹配后的几行。
的following example displays the line that matches “Initializing CPU”从/ var / log / dmesg和此匹配后的5行开始。
# grep"初始化CPU#1" /var/log/dmesg Initializing CPU#1 [Note: 的above shows 上ly the line matching the pattern] # grep-A 5 "初始化CPU#1" dmesg Initializing CPU#1 Calibrating delay using timer specific routine.. 3989.96 BogoMIPS (lpj=1994982) CPU: After generic identify, caps: bfebfbff 20100000 00000000 00000000 CPU: After vendor identify, caps: bfebfbff 20100000 00000000 00000000 monitor/mwait feature present. CPU: L1 I cache: 32K, L1 D cache: 32K [Note: 的above shows the line and 5 lines after the pattern matching]
请参阅我们之前的文章 掌握Grep! – 15个实用的Grep命令示例 that explains how to use grepcommand.
如我们先前的grep命令文章所述,以下操作是可行的。
- 查看由模式标识的特定行,即grep’的默认功能。
- 仅查看匹配的字符。
- 使用-A选项匹配后查看N行。
- 使用-B选项在匹配之前查看N行。
- 使用-C选项查看比赛周围的N条线。
示例9:显示文件中的特定字节。
的following example explains how to display either the top 40 or the last 30 通过tes of a 科幻le.
显示syslog中的前40个字节。
$ head -c40 /var/log/syslog
显示syslog中的最后30个字节。
$ tail -c30 /var/log/syslog
示例10:查看压缩的日志文件
在特定时间后,所有系统日志文件都将旋转并压缩。您可以动态解压缩它,然后将输出通过管道传递到另一个unix命令以查看文件,如下所述。
请参阅我们之前的文章 的Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples
- 显示压缩文件的前N行。
$ zcat 科幻le.gz | head -250
- 显示压缩文件的最后N行。
$ zcat 科幻le.gz | tail -250
- 忽略压缩文件的最后N行。
$ zcat 科幻le.gz | head -n -250
- 忽略压缩文件的前N行。
$ zcat 科幻le.gz | tail -n +250
- 查看与图案匹配的线
$ zcat 科幻le.gz | grep-A2 'error'
- 查看由行号标识的特定行范围。
$ zcat 科幻le.gz | sed -n -e 45p -e 52p
如果您需要返回, 将此页面收藏在del.icio.us 方便参考。
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
我多次使用示例8寻找对特定错误的响应。我也曾经使用过tail和head命令,从来没有真正参与过sed,但是那个例子是一个很好的例子。感谢您发布此信息。