<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]Linux中查看日志文件的正确姿势，求你别tail走天下了！]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Fri, 20 Mar 2020 02:58:49 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	摘录作为一个后端开发工程师，在Linux中查看查看文件内容是基本操作了。尤其是通常要分析日志文件排查问题，那么我们应该如何正确打开日志文件呢？对于笔者这种小菜鸡来说，第一反应就是 cat，tail，vi（或vim）了，是的，我曾经用过好多次vim编辑器来查看日志文件（可耻）。<br/><br/>千万不要使用vi命令来查看大文件内容， 尤其对于那些几十G的大文件。因为vi仅仅是一个编辑器（可以理解为windows中的记事本），使用vi命令后则会把文件所有内容加载到内存中，如果内存不够大的话，则可能会导致服务器瘫痪。<br/><br/><textarea name="code" class="php" rows="15" cols="100">
for ((i=1;i &lt;= 10; i++));
&nbsp;&nbsp;&nbsp;&nbsp;do
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;第$i行&quot; &gt;&gt; test.txt
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [[&nbsp;&nbsp;`expr $i % 2` -eq 0 ]]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo -e&nbsp;&nbsp;&gt;&gt; test.txt
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi
&nbsp;&nbsp;&nbsp;&nbsp;done
</textarea><br/>cat test.txt<br/>第1行<br/>第2行<br/><br/>第3行<br/>第4行<br/><br/>第5行<br/>第6行<br/><br/>第7行<br/>第8行<br/><br/>第9行<br/>第10行<br/><br/>不按脚本，直接每一行都有行号：<br/>cat test.txt<br/>第1行<br/>第2行<br/>第3行<br/>第4行<br/>第5行<br/>第6行<br/>第7行<br/>第8行<br/>第9行<br/>第10行<br/>第11行<br/>第12行<br/>第13行<br/>第14行<br/>第15行<br/>第16行<br/>第17行<br/>第18行<br/>第19行<br/>第20行<br/>第21行<br/><br/>less<br/><br/>less命令比more更加有弹性，可以前后翻页，不止可以向上查找，也可以向下查找。<br/>按键/命令<br/>[pagedown] ：向下翻页<br/>[pageup] ：向上翻页<br/>/字符串：在当前显示的内容（翻页进度位置），向下查找这个字符串关键字<br/>?字符串：向上查找字符串<br/>n ：重复前一个查找，与/或?有关， 比如前一个命令是？表示向上查找，此时n会向上查找<br/>N:&nbsp;&nbsp;反向的重复前一个查找<br/>g ：跳转到当前文件数据的第一行<br/>G ：跳转到当前文件数据的最后一行<br/>q ：退出当前文件的浏览<br/><br/>范例演示<br/><br/>数据截取<br/>head<br/><br/>head命令用来提取文件的前n行，一般配合使用-n选项。当指定的行数为负数-x时，则会打印出除了后面x行的其他所有数据。<br/>范例1：查看前10行数据<br/>head -n 10 test.txt<br/>head -n 10 test.txt<br/>cat test.txt<br/>第1行<br/>第2行<br/>第3行<br/>第4行<br/>第5行<br/>第6行<br/>第7行<br/>第8行<br/>第9行<br/>第10行<br/><br/>范例2（一共10000行，没有空行）：head -n -9989 test.txt<br/>tail<br/><br/>从文件尾部截取数据。tail也是工作中最常用的命令，因为可以利用-f选项，一直刷新获取文件尾部最新数据。<br/><br/>选项与参数<br/>-n&nbsp;&nbsp; ： 查看后n行数据，注意当n后面值带“+”号表示从第x行开始， 如 tail -n +1000 test.txt<br/>-f&nbsp;&nbsp;: 展示文件后面<br/>范例1：查看尾部5行数据【tail -n 5 test.txt】<br/><br/>范例2：查看文件尾部数据，并实时刷新数据<br/><br/>tail -f test.txt<br/><br/>范例3：查看文件尾部5行数据，并实时刷新数据<br/>tail -n 5 -f&nbsp;&nbsp;test.txt<br/><br/><br/>通用命令<br/>管道：Shell 还有一种功能，就是可以将两个或者多个命令（程序或者进程）连接到一起，把一个命令的输出作为下一个命令的输入，以这种方式连接的两个或者多个命令就形成了管道（pipe），管道命令用&quot;&#124;&quot;来表示。<br/><br/>范例：查看ll命令输出的前10行<br/><br/>ll &#124; head -n 3<br/><br/>ll &#124; head -n 10<br/>grep ：命令用于查找文件里符合条件的字符串，这两个命令也是linux中最常用的的，而在查看日志文件也通常会结合这两个命令一起使用。<br/><br/>范例：查看文件文件中那些行包含‘999’<br/><br/>cat -n test.txt &#124; grep &#039;999&#039;<br/><br/>&gt;&gt; : 文件追加重定向命令，可以往文件末尾追加数据，正如上文 echo &quot;第$i行&quot; &gt;&gt; test.txt。<br/><br/>范例：将一个文件的最后10行复制到helloworld.txt中<br/><br/>tail -n 10 &gt;&gt; helloworld.txt<br/><br/>wc：文件字节数，字数，行数查看wc [-clw] [文件...],<br/>-c或--bytes或--chars 只显示Bytes数。<br/>-l或--lines 只显示行数。<br/>-w或--words 只显示字数。<br/>范例：查看文件行数<br/>wc -l<br/><br/>案例实战<br/>案例1：打印日志文件中第11到20行。<br/>思路：首先获取前20行，然后在获取20行的后10行即可，需要使用管道命令。<br/>head -n 20 text.txt &#124; tail -n 10&nbsp;&nbsp; #head和tail都是-n ,head -n 是头n行，tail -n是从第n+1行到行尾<br/>cat -n test.txt &#124; head -n 20 &#124; tail -n 10（如果需要显示行号）<br/><br/>分解：<br/>head -n 20 text.txt<br/>cat test.txt<br/>第1行<br/>第2行<br/>第3行<br/>第4行<br/>第5行<br/>第6行<br/>第7行<br/>第8行<br/>第9行<br/>第10行<br/>第11行<br/>第12行<br/>第13行<br/>第14行<br/>第15行<br/>第16行<br/>第17行<br/>第18行<br/>第19行<br/>第20行<br/><br/>第二步，将上在输出通过管道从11行到最后：<br/>head -n 20 test.txt &#124;tail -n 10<br/>第11行<br/>第12行<br/>第13行<br/>第14行<br/>第15行<br/>第16行<br/>第17行<br/>第18行<br/>第19行<br/>第20行<br/><br/><br/><br/>来自：<a href="https://mp.weixin.qq.com/s/Q-NfY2sr4n2XiJwy8SsXDA" target="_blank">https://mp.weixin.qq.com/s/Q-NfY2sr4n2XiJwy8SsXDA</a>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]Linux中查看日志文件的正确姿势，求你别tail走天下了！]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>