﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-JAVA学习笔记-随笔分类-LINUX</title><link>http://www.blogjava.net/lingy/category/39871.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 31 Jan 2012 14:06:07 GMT</lastBuildDate><pubDate>Tue, 31 Jan 2012 14:06:07 GMT</pubDate><ttl>60</ttl><item><title>[端口被占用,环境起不来]常见测试环境问题解决</title><link>http://www.blogjava.net/lingy/archive/2012/01/31/369065.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Tue, 31 Jan 2012 03:29:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2012/01/31/369065.html</guid><description><![CDATA[<span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">大家是否遇到部署环境的时候,端口被占用,环境起不来的问题呢？遇到这个情况,我们该如何解决呢？</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">下面我把我遇到这个问题解决的方法总结下：</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">现象是这样的:</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">1. 页面访问出现: Http 500</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">2. 后台启动日志提示:<span class="Apple-converted-space">&nbsp;</span></span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">vodka init successfully!!! ...</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">start apache ......</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">(98)Address already in use: make_sock: could not bind to address [::]:5071</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">no listening sockets available, shutting down</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Unable to open logs</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">解决方法: 杀掉占用的进程</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">首先,需要用root账号登陆,登陆后,输入: netstat -plan |grep 5070</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">出现如下:</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">[Intranet root@qa-kernal-143-41 /root]</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">#netstat -plan |grep 5070</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">tcp 0 0 :::5070 :::* LISTEN 28002/httpd<span class="Apple-converted-space">&nbsp;</span></span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">tcp 1 0 ::ffff:10.20.143.41:5070 ::ffff:10.16.46.75:4654 CLOSE_WAIT 28406/httpd<span class="Apple-converted-space">&nbsp;</span></span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">最后杀掉占用的进程,输入:&nbsp;&nbsp; lsof -i:5070 即可 (注5070是端口号)</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">[Intranet root@qa-kernal-143-41 /root]</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">#lsof -i:5070</span><img src ="http://www.blogjava.net/lingy/aggbug/369065.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2012-01-31 11:29 <a href="http://www.blogjava.net/lingy/archive/2012/01/31/369065.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux用户组相关知识</title><link>http://www.blogjava.net/lingy/archive/2012/01/31/369064.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Tue, 31 Jan 2012 03:28:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2012/01/31/369064.html</guid><description><![CDATA[<span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">用root用户登陆输入命令cd /home，再用ll查看发现drwxr-xr-x 12 622&nbsp;&nbsp; 622&nbsp;&nbsp;&nbsp;&nbsp;4096 Apr 18 19:21 aurora&nbsp;&nbsp;aurora用户属主不正确</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">用命令chown aurora.aurora aurora修改用户属主</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">知识点：</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">1.查看用户组信息#cat /etc/passwd</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">/etc/passwd中一行记录对应着一个用户，每行用冒号隔开分为7个字段，具体字段说明如下：</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(255,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">name:password:uid:gid:comment:home:shell<br style="outline-style: none" />用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">home: 指定用户的主目录的绝对路径</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">shell: 用户登陆后启动的一个进程，这个进程是用户登录到系统后运行的命令解释器或某个特定的程序，即Shell。</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">例子：</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">aurora:x:707:707::/home/aurora:/bin/bash</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp; 系统首先会查阅 /etc/passwd 文件，看是否有aurora 这个账号，然后确定aurora的UID，通过UID 来确认用户和身份，</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp; 如果存在则读取/etc/shadow 影子文件中所对应的aurora的密码；&nbsp;&nbsp;<span class="Apple-converted-space">&nbsp;</span></span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp; 用户名：x：uid：gid：用户名全称：用户的家：用户所用的shell类型</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;x指向映射</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">uid为0 则为root&nbsp;&nbsp;</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">2.aurora:$1$nC4FzXJz$OP5LS0Evj4uMsLvMHP1eZ/:15082:0:99999:7:::</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">etc/shadow文件是/etc/passwd 的影子文件，这个文件并不由/etc/passwd 而产生的，这两个文件是应该是对应互补的；</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">shadow内容包括用户及被加密的密码及其他/etc/passwd 不能包括的信息。</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">用户：密码：上次修改密码的时间 ：两次修改口令间隔最少的天数：两次修改口令间隔最多的天数</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">3.aurora:x:707:</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">用户名：用户组密码：GID :用户列表</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">第四字段：用户列表，每个用户之间用,号分割；本字段能为空；</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">如果字段为空表示用户组为GID的用户名</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">4.chown 修改文件和文件夹的用户和用户组属性</span><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; outline-style: none; font: 14px/24px Arial; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; display: inline !important; font: 14px/24px Arial; white-space: normal; orphans: 2; float: none; letter-spacing: normal; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">5.passwd aurora 修改密码</span><img src ="http://www.blogjava.net/lingy/aggbug/369064.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2012-01-31 11:28 <a href="http://www.blogjava.net/lingy/archive/2012/01/31/369064.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux 命令</title><link>http://www.blogjava.net/lingy/archive/2010/08/17/329113.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Tue, 17 Aug 2010 08:28:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2010/08/17/329113.html</guid><description><![CDATA[<p>&nbsp;</p>
<p>insert into ali_zeus_access_area<br />
select * from ali_zeus_access_area@zeus_dev;</p>
<p>script -q tty.log</p>
<p>sqlplus /nolog <br />
conn rnd/mCQe88il@crmintsb<br />
conn rnd/mCQe88il@crmint</p>
<p><br />
scp tty.log guangyan.lingy@10.20.130.14:/home/guangyan.lingy/tty0601.log</p>
<p>export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK<br />
sqlplus rnd/mCQe88il@crmg</p>
<p>for remote in \<br />
172.16.131.47 172.16.131.48 172.16.131.49 172.16.131.50 172.16.131.51 172.16.131.52 172.16.131.53 172.16.131.54 172.16.131.55 172.16.131.2 172.16.131.3 172.16.131.4 172.16.131.5 172.16.131.6 172.16.131.7 172.16.131.41 172.16.131.42 172.16.131.56 172.16.131.57 172.16.131.58 \<br />
;do echo $remote; ssh $remote "grep 'sms webService visitor IP is' output/logs/user/martini_sales_tool.log"; done</p>
<p>&nbsp;</p>
<p>daisy.louy<br />
&nbsp;louying<br />
&nbsp;<br />
&nbsp;<br />
carvin hello123<br />
http://svn.alibaba-inc.com/repos/crm/martini/branches/20091209_dcs_guangyan.lingy_dev</p>
<p>weiming.shi<br />
jinbo.yu<br />
1234567890 alibaba-guest<br />
我刚又试了</p>
<p><br />
中国的十大宜居小城,中午的时候看的<br />
http://focus.news.163.com/10/0531/09/680LGN5L00011SM9.html<br />
有泉州:)</p>
<p>数据模型资源手册</p>
<p>需要低烟无卤阻燃电缆料的电缆企业</p>
<img src ="http://www.blogjava.net/lingy/aggbug/329113.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2010-08-17 16:28 <a href="http://www.blogjava.net/lingy/archive/2010/08/17/329113.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vim 到指定行 :3305</title><link>http://www.blogjava.net/lingy/archive/2009/09/02/293577.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 02 Sep 2009 04:28:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/09/02/293577.html</guid><description><![CDATA[用<br />
:number <br />
eg:<br />
:3305
<img src ="http://www.blogjava.net/lingy/aggbug/293577.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-09-02 12:28 <a href="http://www.blogjava.net/lingy/archive/2009/09/02/293577.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Vim查找操作</title><link>http://www.blogjava.net/lingy/archive/2009/09/02/293576.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 02 Sep 2009 04:25:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/09/02/293576.html</guid><description><![CDATA[<p>&nbsp;</p>
<div class="cnt" id="blog_text"><font size="4">" 搜索<br />
/joe/e : 设置光标到匹配"joe"的末尾<br />
/joe/e+1 : 设置光标到匹配"joe"的末尾再后移一位<br />
/joe/s-2 : 设置光标到匹配"joe&#8220;的开头再前移两位<br />
/^joe.*fred.*bill/ : 匹配以'j'开头且"joe"到"fred"到"bill"之间至少有一个字符<br />
/^[A-J]\+/ : 搜索'A'到&#8217;J&#8216;重复两次以上的开头行<br />
/begin\_.*end : 多行匹配<br />
/fred\_s*joe/i : 可以是任何空白字符包括\n,\t等等<br />
/fred\|joe : 搜索fred或者joe<br />
/.*fred\&amp;.*joe : 搜索同时包括fred跟joe的行<br />
/\&lt;fred\&gt;/i : 搜索独立的单词fred<br />
/\&lt;\d\d\d\d\&gt; : 搜索独立的4位数字<br />
/\D\d\d\d\d\D : 搜索6位字符串中间4位数字前后两位不能为数字<br />
/\&lt;\d\{4}\&gt; : 同/\&lt;\d\d\d\d\&gt;<br />
" 查找空行<br />
/^\n\{3} : 匹配三连续的空行<br />
" 使用正则表达式组查找<br />
/\(fred\).*\(joe\).*\2.*\1<br />
" 正则表达式重复<br />
/^\([^,]*,\)\{8}<br />
" visual searching<br />
:vmap // y/&lt;C-R&gt;"&lt;CR&gt; : visually模式下的键盘映射，把//映射成匹配当前选中的文本<br />
:vmap &lt;silent&gt; // y/&lt;C-R&gt;=escape(@", '\\/.*$^~[]')&lt;CR&gt;&lt;CR&gt; : 包括空白字符<br />
" \zs 和 \ze 匹配原 :h /\zs<br />
/&lt;\zs[^&gt;]*\ze&gt; : 匹配尖括号中的内容<br />
" 零宽度匹配 :h /\@=<br />
/&lt;\@&lt;=[^&gt;]*&gt;\@= : search for tag contents, ignoring chevrons<br />
/&lt;\@&lt;=\_[^&gt;]*&gt;\@= : search for tags across possible multiple lines<br />
" 多行查找 \_ 的意思是包括换行符<br />
/&lt;!--\_p\{-}--&gt; : 匹配&lt;!--开始到--&gt;结尾的所有内容<br />
/fred\_s*joe/i : 匹配fred开始到joe，之间一定得是空白字符<br />
/bugs\(\_.\)*bunny : 匹配所有bugs到bunny的字符串<br />
:h \_ : help<br />
" 查找函数声明，nmap为normal模式下的键盘映射<br />
:nmap gx yiw/^\(sub\&lt;bar&gt;function\)\s\+&lt;C-R&gt;"&lt;CR&gt;<br />
" 查找多个文件<br />
:bufdo /searchstr/ : 在多个文件缓冲区里执行查找<br />
" 更好的多文件查找定位方法<br />
:bufdo %s/searchstr/&amp;/gic : 在多个文件缓冲区里查找，按下n停止<br />
" 怎样不使用 / 来查找网址<br />
?</font><a href="http://www.vim.org/" target="_blank"><font size="4">http://www.vim.org/</font></a><font size="4"> : 向后查找<br />
" 查找指定字符以外的字符串<br />
/\c\v([^aeiou]&amp;\a){4} : 查找4个辅音字母<br />
----------------------------------------<br />
#替换<br />
:%s/fred/joe/igc : 普通替换命令<br />
:%s/\r//g : 删除 DOS 的换行符 ^M<br />
" 你的文本文件是否乱七八糟的排成一行？使用如下命令<br />
:%s/\r/\r/g : 转换 DOS 回车符 ^M 为真正的回车符<br />
:%s= *$== : 删除行尾空白<br />
:%s= \+$== : 同上<br />
:%s#\s*\r\?$## : 删除尾部空白和dos换行符<br />
:%s#\s*\r*$## : 同上<br />
" 删除空行<br />
:%s/^\n\{3}// : 删除连续3个空行<br />
:%s/^\n\+/\r/ : 压缩空行，多个替换为一个<br />
%s#&lt;[^&gt;]\+&gt;##g : 删除html的tag部分<br />
" IF YOU ONLY WANT TO KNOW ONE THING<br />
:'a,'bg/fred/s/dick/joe/igc : 非常有用<br />
# 译释：''a,''b指定一个范围：mark a ~ mark b<br />
# g//用一个正则表达式指出了进行操作的行必须可以被fred匹配<br />
# 看后面，g//是一个全局显示命令<br />
# s/dick/joe/igc则对于这些满足条件的行进行替换<br />
" 复制列<br />
:%s= [^ ]\+$=&amp;&amp;= : 复制最后一列<br />
:%s= \f\+$=&amp;&amp;= : 一样<br />
:%s= \S\+$=&amp;&amp; : 晕，还一样！<br />
" 记忆（反向引用）<br />
:s/\(.*\):\(.*\)/\2 : \1/ : 将两个字段颠倒<br />
:%s/^\(.*\)\n\1$/\1/ : 删除重复行<br />
" 非贪婪匹配 \{-}<br />
:%s/^.\{-}pdf/new.pdf/ : 删除第一个pdf<br />
" use of optional atom \?<br />
:%s#\&lt;[zy]\?tbl_[a-z_]\+\&gt;#\L&#gc : lowercase with optional leading characters<br />
" 跨越尽量多的行<br />
:%s/&lt;!--\_.\{-}--&gt;// : 删除多行注释<br />
:help /\{-} : 查看非贪婪匹配的更多帮助<br />
" 使用寄存器替换<br />
:s/fred/&lt;c-r&gt;a/g : 将fred替换为寄存器a里的内容<br />
:s/fred/&lt;c-r&gt;asome_text&lt;c-r&gt;s/g<br />
:s/fred/\=@a/g : better alternative as register not displayed<br />
" 在一行里写多种命令<br />
:%s/\f\+\.gif\&gt;/\r&amp;\r/g | v/\.gif$/d | %s/gif/jpg/<br />
:%s/a/but/gie|:update|:next : 当使用 @: 来重复<br />
" 或运算<br />
:%s/suck\|buck/loopy/gc : 替换suck或者buck（这里|不是管道）<br />
" 调用vim函数<br />
:s/__date__/\=strftime("%c")/ : 将__date__替换成当前日期，使用strftime函数<br />
" 处理列，替换所有在第三列中的str1<br />
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2:<br />
" 交换第一列跟第四列<br />
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1:<br />
" 过滤form中的内容放在寄存器里<br />
:redir @*|sil exec 'g#&lt;\(input\|select\|textarea\|/\=form\)\&gt;#p'|redir END<br />
:nmap ,z :redir @*&lt;Bar&gt;sil exec 'g@&lt;\(input\&lt;Bar&gt;select\&lt;Bar&gt;textarea\&lt;Bar&gt;<br />
/\=form\)\&gt;@p'&lt;Bar&gt;redir END&lt;CR&gt;<br />
" 两位以上的数字减三（带进位。这个命令挺有趣）<br />
:%s/\d\+/\=(submatch(0)-3)/<br />
" 包含loc或者functions的行中的数字加6<br />
:g/loc\|function/s/\d/\=submatch(0)+6/<br />
" 比上面更好的方法<br />
:%s#txtdev\zs\d#\=submatch(0)+1#g<br />
:h /\zs 查看帮助<br />
" 前缀为gg的数字加6<br />
:%s/\(gg\)\@&lt;=\d\+/\=submatch(0)+6/<br />
:h zero-width 查看帮助<br />
" 替换一个特定字符串为数字<br />
:let i=10 | 'a,'bg/Abc/s/yy/\=i/ |let i=i+1 # 将yy转换成10，11，12等等<br />
" 比上面的更精确<br />
:let i=10 | 'a,'bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 # 将xxyy 转换成 xx11,xx12,<br />
xx13<br />
" find replacement text, put in memory, then use \zs to simplify substitute<br />
:%s/"\([^.]\+\).*\zsxx/\1/<br />
" Pull word under cursor into LHS of a substitute<br />
:nmap &lt;leader&gt;z :%s#\&lt;&lt;c-r&gt;=expand("&lt;cword&gt;")&lt;cr&gt;\&gt;#<br />
" Pull Visually Highlighted text into LHS of a substitute<br />
:vmap &lt;leader&gt;z :&lt;C-U&gt;%s/\&lt;&lt;c-r&gt;*\&gt;/<br />
----------------------------------------<br />
" all following performing similar task, substitute within substitution<br />
" Multiple single character substitution in a portion of line only<br />
:%s,\(all/.*\)\@&lt;=/,_,g : replace all / with _ AFTER "all/"<br />
" Same thing<br />
:s#all/\zs.*#\=substitute(submatch(0), '/', '_', 'g')#<br />
" Substitute by splitting line, then re-joining<br />
:s#all/#&amp;^M#|s#/#_#g|-j!<br />
" Substitute inside substitute<br />
:%s/.*/\='cp '.submatch(0).' all/'.substitute(submatch(0),'/','_','g')/<br />
----------------------------------------<br />
" 全局显示命令<br />
:g/gladiolli/# : 查找并显示匹配的行号<br />
:g/fred.*joe.*dick/ : 显示所有含有 fred,joe &amp; dick的行<br />
:g/\&lt;fred\&gt;/ : 显示单一单词fred<br />
:g/^\s*$/d : 删除所有空行<br />
:g!/^dd/d : 删除不含字符串''dd''的行<br />
:v/^dd/d : 同上<br />
:g/fred/,/joe/d : 删除所有的从fred到joe<br />
:g/-------/.-10,.d : 以-------为标记删除之前的10行<br />
:g/{/ ,/}/- s/\n\+/\r/g : 删除 {...}之间的空行<br />
:v/\S/d : Delete empty lines (both types)<br />
:v/./,/./-j : 压缩空行<br />
:g/^$/,/./-j : 同上<br />
:g/&lt;input\|&lt;form/p : 或运算<br />
:g/^/put_ : 双倍行宽 (pu = put)<br />
:g/^/m0 : 颠倒文件 (m = move)<br />
:'a,'bg/^/m'b : 颠倒选中的 a 到 b<br />
:g/^/t. : 重复行<br />
:g/fred/t$ : 拷贝行从fred到结尾<br />
:g/stage/t'a : 拷贝行从stage 到 marker a（a为标记的位置）<br />
:g/\(^I[^^I]*\)\{80}/d : 删除最少包含80个tab的行<br />
" perform a substitute on every other line<br />
:g/^/ if line('.')%2|s/^/zz /<br />
" match all lines containing "somestr" between markers a &amp; b<br />
" copy after line containing "otherstr"<br />
:'a,'bg/somestr/co/otherstr/ : co(py) or mo(ve)<br />
" as above but also do a substitution<br />
:'a,'bg/str1/s/str1/&amp;&amp;&amp;/|mo/str2/<br />
:%norm jdd : 隔行删除<br />
" 增加数字 (键入 &lt;c-a&gt;)<br />
:.,$g/^\d/exe "norm! \&lt;c-a&gt;": 增加从当前行首到结尾的数字<br />
:'a,'bg/\d\+/norm! ^A : 增加数字<br />
" 保存全局命令的结果 (注意必须使用添加模式) 你需要使用 qaq 清空寄存器a.<br />
"save results to a register/paste buffer 存储结果到 寄存器/粘贴 到 a<br />
:g/fred/y A : 添加配备行到寄存器到 a<br />
:g/fred/y A | :let @*=@a : 放入复制缓冲区<br />
:let @a=''|g/Barratt/y A |:let @*=@a<br />
:'a,'b g/^Error/ . w &gt;&gt; errors.txt<br />
" 复制每一行，然后在复制出来的每一行两侧加上一个 print '复制出来的内容'<br />
:g/./yank|put|-1s/'/"/g|s/.*/Print '&amp;'/<br />
" 用文件中的内容替换字符串，-d 表示删除&#8220;标记&#8221;<br />
:g/^MARK$/r tmp.ex | -d<br />
" display prettily<br />
:g/&lt;pattern&gt;/z#.5 : display with context<br />
:g/&lt;pattern&gt;/z#.5|echo "==========" : display beautifully<br />
" Combining g// with normal mode commands<br />
:g/|/norm 2f|r* : replace 2nd | with a star<br />
"send output of previous global command to a new window<br />
:nmap &lt;F3&gt; :redir @a&lt;CR&gt;:g//&lt;CR&gt;:redir END&lt;CR&gt;:new&lt;CR&gt;:put! a&lt;CR&gt;&lt;CR&gt;<br />
----------------------------------------<br />
" 全局命令和替换命令联姻 (强大的编辑能力)<br />
:'a,'bg/fred/s/joe/susan/gic : 可以使用反向引用来匹配<br />
:g/fred/,/joe/s/fred/joe/gic : non-line based (ultra)<br />
----------------------------------------<br />
" 先找fred，然后找joe<br />
:/fred/;/joe/-2,/sid/+3s/sally/alley/gIC<br />
----------------------------------------<br />
" create a new file for each line of file eg 1.txt,2.txt,3,txt etc<br />
:g/^/exe ".w ".line(".").".txt"<br />
----------------------------------------<br />
" Absolutely essential<br />
----------------------------------------<br />
* # g* g# : 查找当前光标下的单词（单个单词） (&lt;cword&gt;) (向前/向后)<br />
% : 匹配括号 {}[]()<br />
. : 重复上次操作<br />
@: : 重复上次的命令<br />
matchit.vim : 适%能匹配 &lt;script&gt; &lt;?php等标记<br />
&lt;C-N&gt;&lt;C-P&gt; : 插入模式下自动完成填词<br />
&lt;C-X&gt;&lt;C-L&gt; : 行自动完成（超级有用）<br />
/&lt;C-R&gt;&lt;C-W&gt; : 把单个&lt;cword&gt;单词放入搜索或者命令行<br />
/&lt;C-R&gt;&lt;C-A&gt; : 把字符串中有的单词&lt;CWORD&gt;放入搜索或者命令行<br />
:set ignorecase : 忽略大小写<br />
:syntax on : 打开语法高亮 Perl,HTML,PHP 等等<br />
:h regexp&lt;C-D&gt; : 按ctrl+d得到包含regexp的列表<br />
(按tab自动不齐)<br />
----------------------------------------<br />
" 简单编辑更新 _vimrc文件<br />
:nmap ,s :source $VIM/_vimrc :普通模式下的键盘映射 ,s映射成加载用户目录下的<br />
_vimrc文件<br />
:nmap ,v :e $VIM/_vimrc :,v映射成打开_vimrc文件<br />
----------------------------------------<br />
#VISUAL 模式 (方便增加 HTML 标签)<br />
:vmap sb "zdi&lt;C-R&gt;z&lt;ESC&gt; : 在 VISUALLY模式下将选中的文本前后分别加上<br />
:vmap st "zdi&lt;?= &lt;C-R&gt;z ?&gt;&lt;ESC&gt; : 加上 &lt;?= ?&gt;<br />
----------------------------------------<br />
" 浏览<br />
:Exp(lore) : 浏览文件<br />
:Sex(plore) : 分割窗口浏览文件<br />
:ls : 显示缓冲区<br />
:cd .. : 设置当前目录位置<br />
:args : 查看当前打开的所有文件<br />
:lcd %:p:h : 改变路径到当前编辑的文件<br />
:autocmd BufEnter * lcd %:p:h : 放入.vimrc自动完成上面的命令<br />
----------------------------------------<br />
" 缓冲区浏览(一直排名前10的vim脚本)<br />
" 需要 bufexplorer.vim </font><a href="http://www.vim.org/script.php?script_id=42" target="_blank"><font size="4">http://www.vim.org/script.php?script_id=42</font></a><br />
<font size="4">\be : 缓冲浏览器中查看缓冲列表<br />
\bs : 同上，但是分割窗口<br />
----------------------------------------<br />
" 转换大小写<br />
guu : 将正行的字母转换成小写<br />
gUU : 将正行的字母转换成大写<br />
Vu : 转换选中的行（小写）<br />
VU : 转换选中的行（大写）<br />
g~~ : 反向转换<br />
vEU : 转换词大写<br />
vE~ : 反向转换词<br />
ggguG : 将当前编辑文件内容全部转换成小写<br />
" Visually 模式下选择所有的字母及数字 (放入 .vimrc文件中)<br />
vmap ,c :s/\&lt;\(.\)\(\k*\)\&gt;/\u\1\L\2/g&lt;CR&gt;<br />
" 大写所有句子的第一个字母<br />
:%s/[.!?]\_s\+\a/\U&amp;\E/g<br />
----------------------------------------<br />
gf : 打开当前光标下或后的文件<br />
:nnoremap gF :view &lt;cfile&gt;&lt;cr&gt; : 打开当前光标下或后的文件, 如果不存在则创建<br />
ga : 显示当前光标下单个字的ascii,十进制，十六进制&#8230;&#8230;<br />
ggVGg? : 将整个文件用rot13编码&#8230;&#8230;（谁看得懂啊~~hoho)<br />
ggg?G : 同上 (针对大文件)<br />
:8 | normal VGg? : 将第八行用rot13编码<br />
:normal 10GVGg? : 同上<br />
&lt;C-A&gt;,&lt;C-X&gt; : 增加，减少当前光标下的数字<br />
win32 用户需要重定义 CNTRL-A<br />
&lt;C-R&gt;=5*5 : 插入25 (小型计算器)<br />
----------------------------------------<br />
" 几个彩蛋&#8230;&#8230;<br />
:h 42 : also </font><a href="http://www.google.com/search?q=42" target="_blank"><font size="4">http://www.google.com/search?q=42</font></a><br />
<font size="4">:h holy-grail<br />
:h!<br />
----------------------------------------<br />
" 标记 &amp; 移动<br />
'. : 跳回最后编辑的行 (超有用)<br />
`. : 同上，但是定位编辑点<br />
g; : 跳转到比较旧的编辑位置（如果有的话） (vim6.3后的新功能)<br />
g, : 这个是较心的位置 (同上)<br />
:changes :打出改变表<br />
:h changelist : 查看&#8220;改变表跳转&#8221;的帮助<br />
&lt;C-O&gt; : 依次沿着你的跳转记录向回跳 (从最近的一次开始)<br />
&lt;C-I&gt; : 依次沿着你的跳转记录向前跳<br />
:ju(mps) : 列出跳转轨迹<br />
:help jump-motions<br />
:history : 列出历史记录<br />
:his c : 命令行历史<br />
:his s : 搜索历史<br />
q/ : 搜索命令历史的窗口<br />
q: : 命令行命令历史的窗口<br />
:&lt;C-F&gt; : 历史窗口<br />
----------------------------------------<br />
" 缩写 &amp; 映射<br />
:map &lt;f7&gt; :'a,'bw! c:/aaa/x<br />
:map &lt;f8&gt; :r c:/aaa/x<br />
:map &lt;f11&gt; :.w! c:/aaa/xr&lt;CR&gt;<br />
:map &lt;f12&gt; :r c:/aaa/xr&lt;CR&gt;<br />
:ab php : 查看以php开头的缩写<br />
:map , : 列出所有的映射（以逗号开始的）<br />
" 允许映射 F10 (win32)<br />
set wak=no : :h winaltkeys<br />
" 映射中常使用的表示<br />
&lt;CR&gt; : 回车<br />
&lt;ESC&gt; : Esc<br />
&lt;LEADER&gt; : 右斜杠<br />
&lt;BAR&gt; : 管道符号<br />
&lt;BACKSPACE&gt; : 退格键<br />
&lt;SILENT&gt; : 不回显<br />
#显示自定义的 RGB 颜色显示当前光标下的字符串 例如 #445588<br />
:nmap &lt;leader&gt;c :hi Normal guibg=#&lt;c-r&gt;=expand("&lt;cword&gt;")&lt;cr&gt;&lt;cr&gt;<br />
map &lt;f2&gt; /price only\\|versus/ :in a map need to backslash the \<br />
----------------------------------------<br />
" Simple PHP debugging display all variables yanked into register a<br />
" 简单的 PHP 调试将所有显示的变量放入寄存器a<br />
iab phpdb exit("&lt;hr&gt;Debug &lt;C-R&gt;a ");<br />
----------------------------------------<br />
" 使用寄存器来映射 (放入 .vimrc文件自动加载)<br />
:let @m=":'a,'bs/"<br />
:let @s=":%!sort -u"<br />
----------------------------------------<br />
" 列出寄存器<br />
:reg : 显示当前所有的寄存器<br />
:reg a : 显示寄存器a中的内容<br />
"1p.... : 引用一个叫1的寄存器<br />
:let @y='yy@"' : pre-loading registers (put in .vimrc)<br />
qqq : 清空寄存器 "q"<br />
----------------------------------------<br />
" 一些有用的决窍<br />
"ayy@a : 把当前行作为命令执行<br />
yy@" : 上面的匿名寄存器<br />
u@. : 只执行键入的命令<br />
----------------------------------------<br />
" 从其它命令处获得输入（需要外部命令）<br />
:r!ls.exe : 从ls 获得输入插入到当前位置<br />
!!date : 从date获得输入（删除当前行）<br />
" 使用外部sort排序<br />
:%!sort -u : 用sort排序整个文件（结果覆盖整个文件）<br />
:'a,'b!sort -u : 从mark a到mark b之间的内容进行排序<br />
!1} sort -u : 排序一个段落<br />
:g/^$/;,/^$/-1!sort : Sort each block (note the crucial ;)<br />
----------------------------------------<br />
" 多文件管理 (基本的)<br />
:bn : 跳转到下一个buffer<br />
:bp : 跳转到前一个buffer<br />
:wn : 保存当前buffer并跳转到下一个buffer (超有用)<br />
:wp : 保存当前buffer并跳转到前一个buffer<br />
:bd : 把当前文件从buffer移出 (超有用)<br />
:bun : 卸载当前buffer (关闭这个窗口但是不移出)<br />
:badd file.c : 添加file.c到buffer列表<br />
:b 3 : 前往第三个 buffer<br />
:b main : 前往含有main的buffer中 比如说 main.c<br />
:sav php.html : 把当前文件存为php.html并打开<br />
:sav! %&lt;.bak : 换一个后缀名保存 (旧方法)<br />
:sav! %:r.cfm : 同上<br />
:sav %:s/fred/joe/ : 替换文件名<br />
:sav %:s/fred/joe/:r.bak2 : 替换文件和后缀<br />
:!mv % %:r.bak : 重命名当前文件<br />
:e! : 打开未修改之前的文件<br />
:w c:/aaa/% : 存储文件到指定位置<br />
:e # : 编辑标记为#的文件在buffer中<br />
:rew : 返回到第一个可编辑的文件<br />
:brew : 回到第一个buffer<br />
:sp fred.txt : 分割窗口打开fred.txt<br />
:sball,:sb : 把所有的 buffers分割显示在一个窗口中 (超有用)<br />
:scrollbind : in each split window<br />
:map &lt;F5&gt; :ls&lt;CR&gt;:e # : 按F5显示所有buffer, 并显示行号<br />
:set hidden : 允许不保存当前buffer而进行切换<br />
----------------------------------------<br />
" 在分割窗口中快速切换<br />
map &lt;C-J&gt; &lt;C-W&gt;j&lt;C-W&gt;_<br />
map &lt;C-K&gt; &lt;C-W&gt;k&lt;C-W&gt;_<br />
----------------------------------------<br />
" 录制命令 (最好的技巧)<br />
qq # 录制命令放入 q寄存器<br />
输入一些命令<br />
q # 录制结束<br />
@q :执行放入寄存器q中的内容<br />
@@ ：重复<br />
5@@ ：重复5次<br />
" 编辑一个 寄存器/录制<br />
"qp :显示寄存器q中的内容(普通模式下)<br />
&lt;ctrl-R&gt;q :显示寄存器q中的内容 (插入模式下)<br />
" 你现在可以看到记录内容，随便编辑<br />
"qdd :删除，重新存入q<br />
@q :执行 录制/寄存器 q<br />
" 在可视块中运行记录<br />
1) 定义记录/寄存器<br />
qq:s/ to/ from/g^Mq<br />
2) 定义可视块<br />
V}<br />
3) 键入 : 将显示下面信息<br />
:'&lt;,'&gt;<br />
4)完成如下操作<br />
:'&lt;,'&gt;norm @q<br />
----------------------------------------<br />
"combining a recording with a map (to end up in command mode)<br />
nnoremap ] @q:w!&lt;bar&gt;bd<br />
----------------------------------------<br />
" 可视化模式提供一种灵活易用的方法选择一块文本供操作符使用<br />
" 记出<br />
v : 进入可视化模式<br />
V : 进入可视化行选择模式<br />
&lt;C-V&gt; : 进入可视化块选择模式<br />
gv : 重新选择<br />
o : 选择的区域头尾移动<br />
"*y : 复制选择区域到paste buffer<br />
V% : 选择一个匹配段<br />
V}J : 合并一个段落<br />
V}gJ : 合并一个段落，并保留空格<br />
----------------------------------------<br />
" 删除选中的10行的前两个字符（不过这里应该假设是紧凑的排版格式，不能包含空格、<br />
tab等字符的，可是经实验应该是钱3个字符才对啊？？）<br />
0&lt;c-v&gt;10j2ld<br />
----------------------------------------<br />
" 如何用可视块拷贝几列<br />
" 可视块(并非通常的 v 命令)<br />
&lt;C-V&gt;，然后通过移动命令选择列 (win32 &lt;C-Q&gt;)<br />
然后执行 c,d,y,r 等命令<br />
----------------------------------------<br />
" how to overwrite a visual-block of text with another such block<br />
Pick the first block: ctrl-v move "ay<br />
Pick the second block: ctrl-v move c ctrl-o "aP &lt;esc&gt;<br />
----------------------------------------<br />
" _vimrc 基本设置<br />
:set incsearch : 输入搜索命令时，立即显示目前输入的模式对应的匹配。匹配的字符串<br />
被高亮。<br />
:set wildignore=*.o,*.obj,*.bak,*.exe : tab补全时忽略这些忽略这些<br />
:set shiftwidth=3 : 设置自动缩进为3个字符<br />
:set vb t_vb=". : 安静模式，关闭响铃跟闪烁<br />
:set browsedir=buffer : 设置文件浏览使用的目录<br />
&#8220;注：<br />
&#8221;last 使用文件浏览器最近访问相同的目录。<br />
&#8220;buffer 使用相关缓冲区的目录。<br />
&#8221;current 使用当前目录。<br />
&#8220;{path} 使用指定目录。<br />
----------------------------------------<br />
" 启动windows中的IE<br />
:nmap ,f :update&lt;CR&gt;:silent !start c:\progra~1\intern~1\iexplore.exe<br />
file://%:p&lt;CR&gt;<br />
:nmap ,i :update&lt;CR&gt;: !start c:\progra~1\intern~1\iexplore.exe &lt;cWORD&gt;&lt;CR&gt;<br />
----------------------------------------<br />
" 在vim里打开ftp<br />
cmap ,r :Nread </font><a href="ftp://209.51.134.122/public_html/index.html" target="_blank"><font size="4">ftp://209.51.134.122/public_html/index.html</font></a><br />
<font size="4">cmap ,w :Nwrite </font><a href="ftp://209.51.134.122/public_html/index.html" target="_blank"><font size="4">ftp://209.51.134.122/public_html/index.html</font></a><br />
<font size="4">gvim </font><a href="ftp://www.somedomain.com/index.html" target="_blank"><font size="4">ftp://www.somedomain.com/index.html</font></a><font size="4"> # 使用 netrw.vim<br />
----------------------------------------<br />
" 向寄存器中添加内容 (使用相应寄存器名称的大写)<br />
" 复制5行放入a寄存器，然后向下跳转10行再复制5行<br />
"a5yy<br />
10j<br />
"A5yy<br />
----------------------------------------<br />
[I : 显示当前行中字符的所有匹配(超级有用)<br />
----------------------------------------<br />
" 常规缩进<br />
:'a,'b&gt;&gt; ：将mark a到mark b之间的内容进行两次缩进<br />
" 虚拟模式下缩进 (可重复)<br />
:vnoremap &lt; &lt;gv<br />
&#8221;这是一个虚拟模式下的键盘映射 &lt; 映射为&lt;gv<br />
"&lt; 意为向内缩进，gv 上面已有解释，为重复上次选区<br />
&#8220;&lt;gv 也就是先向内缩进然后再选择刚才的选区<br />
&#8220;这样就可以只按 &lt; 实现重复缩进了<br />
:vnoremap &gt; &gt;gv ：向内缩进，原理同上<br />
" 块缩进<br />
&gt;i{<br />
&gt;a{<br />
" also<br />
&gt;% and &lt;%<br />
&#8221;自己试试看吧，涉及到用 { 的语言很有用，比如c,c++等<br />
----------------------------------------<br />
" 重定向 &amp; 粘贴到寄存器 * （*为寄存器名称）<br />
:redir @* : 重定向命令到paste缓冲区<br />
:redir END : 结束<br />
:redir &gt;&gt; out.txt : 重定向到文件<br />
" 操作粘贴缓冲区<br />
"*yy : 复制到寄存器<br />
"*p : 从寄存器中粘贴一行<br />
" 复制到粘贴缓冲区 (扩展模式)<br />
:'a,'by* : 复制一个范围到粘贴寄存器<br />
:%y* : 复制一个括号匹配到粘贴缓冲区<br />
:.y* : 复制当前行到粘贴缓冲区<br />
" 从剪贴板上过滤非可打印字符<br />
" 当从一些 GUI 程序粘贴时会有用处<br />
:nmap &lt;leader&gt;p :let @* = substitute(@*,'[^[:print:]]','','g')&lt;cr&gt;"*p<br />
----------------------------------------<br />
" 重新格式化文本<br />
gq} : 合并一个段落<br />
gqap : 当前段落<br />
ggVGgq : 全部段落<br />
Vgq : 当前行<br />
" 在70列的时候换行<br />
:s/.\{,69\};\s*\|.\{,69\}\s\+/&amp;\r/g<br />
----------------------------------------<br />
" 命令使用于多个文件<br />
:argdo %s/foo/bar/e : 在所有文件上操做 :args<br />
:bufdo %s/foo/bar/e<br />
:windo %s/foo/bar/e<br />
:argdo exe '%!sort'|w! : 包含外部命令<br />
----------------------------------------<br />
" 命令行技巧<br />
gvim -h : 显示帮助<br />
ls | gvim - : 管道操作<br />
cat xx | gvim - -c "v/^\d\d\|^[3-9]/d " : 从管道出过滤内容<br />
gvim -o file1 file2 : 分割窗口显示两个文件<br />
" 打开文件后执行一条命令<br />
gvim.exe -c "/main" joe.c : 打开 joe.c &amp; 跳转到 "main"<br />
" 在打开一个文件时执行多条命令<br />
vim -c "%s/ABC/DEF/ge | update" file1.c<br />
" 在一组文件上执行多条命令<br />
vim -c "argdo %s/ABC/DEF/ge | update" *.c<br />
" 从一系列文件中删除一块区域<br />
vim -c "argdo /begin/+1,/end/-1g/^/d | update" *.c<br />
" 自动编辑文件 (编辑命令序列Ex commands已经包含在convert.vim中了)<br />
vim -s "convert.vim" file.c<br />
#不加载.vimrc跟任何plugin(干净清新的 VIM^_^)<br />
gvim -u NONE -U NONE -N<br />
" Access paste buffer contents (put in a script/batch file)<br />
gvim -c 'normal ggdG"*p' c:/aaa/xp<br />
" 将paste中的内容送往默认的打印机<br />
gvim -c 's/^/\=@*/|hardcopy!|q!'<br />
" gvim 里的 grep (win32 or *nix)<br />
:grep somestring *.php : 创建匹配的文件列表<br />
" 使用 :cn(向后后) :cp(向前) 操纵列表<br />
:h grep ：查看帮助<br />
----------------------------------------<br />
" GVIM 的差异比较<br />
gvim -d file1 file2 : vimdiff (比较不差异)<br />
dp : 把光标处的不同放到另一个文件<br />
do : 在光标处从另一个文件取得不同<br />
----------------------------------------<br />
" Vim traps<br />
在正则表达式中 + | ( { 都要加上转义符(反斜杠)<br />
/fred\+/ : 匹配 fred/freddy 但不匹配 free<br />
/\(fred\)\{2,3}/ : note what you have to break<br />
----------------------------------------<br />
" \v ，或叫做very magic (通常都是这么叫)可以取消转义符<br />
/codes\(\n\|\s\)*where : 普通的正则表达式<br />
/\vcodes(\n|\s)*where : very magic<br />
----------------------------------------<br />
" 把对象送到命令行或者搜索行<br />
&lt;C-R&gt;&lt;C-W&gt; : 执行当前光标下的单个单词<br />
&lt;C-R&gt;&lt;C-A&gt; : 执行当前光标下尽可能多的单词<br />
&lt;C-R&gt;- : 送至一个小型寄存器（同样使用于插入模式）<br />
&lt;C-R&gt;[0-9a-z] : 送至一个命名寄存器 (括弧同上)<br />
&lt;C-R&gt;% : 送至文件名(#也行) (同上)<br />
&lt;C-R&gt;=somevar : 送至一个变量 (例如 :let sray="ray[0-9]")<br />
----------------------------------------<br />
" 控制寄存器<br />
:let @a=@_ : 清除寄存器 a<br />
:let @a="" : 同上 a<br />
:let @*=@a : 拷贝寄存器 a 到 paste buffer<br />
:let @*=@: : 拷贝最后执行的命令到 paste buffer<br />
:let @*=@/ : 拷贝最后执行的查找命令到paste buffer<br />
:let @*=@% : 拷贝当前文件到 paste buffer<br />
<br />
map &lt;f11&gt; "qyy:let @q=@q."zzz"<br />
----------------------------------------<br />
" 帮助的帮助？ (使用 TAB)<br />
:h quickref : VIM 快速参考页<br />
:h tips : Vim'自己的技巧帮助<br />
:h visual&lt;C-D&gt;&lt;tab&gt; : 虚拟模式的帮助列表<br />
: 然后使用tab选择它们<br />
:h ctrl&lt;C-D&gt; : 所有关于ctrl键的帮助列表<br />
:helpg uganda : 过滤帮助文件 使用 :cn, :cp 查找下一个及后一个<br />
:h :r : 关于 :ex 的命令帮助<br />
:h CTRL-R : 普通模式相关<br />
:h /\r : \r是什么的意思<br />
:h \\zs : 使用双反斜线查找关于 \zs 的帮助<br />
:h i_CTRL-R : 在插入模式中 &lt;C-R&gt;的解释<br />
:h c_CTRL-R : 在命令模式中 &lt;C-R&gt; 的解释<br />
:h v_CTRL-V : 虚拟模式<br />
:h tutor : VIM 快速指南<br />
&lt;C-[&gt;, &lt;C-T&gt; : Move back &amp; Forth in HELP History<br />
gvim -h : VIM 命令行帮助<br />
----------------------------------------<br />
" 选项设置在那里<br />
:scriptnames : 列出所有已经加载的 plugins, _vimrcs文件<br />
:verbose set history? :显示 history的值并显示在那里定义的<br />
:function : 列出所有函数<br />
:func SearchCompl : 显示指定函数的细节<br />
----------------------------------------<br />
" 制作你自己的VIM 帮助<br />
:helptags /vim/vim64/doc : 重新编译所有 *.txt 的帮助文件在这个目录里<br />
:help add-local-help ：如何添加本地帮助<br />
----------------------------------------<br />
" 用外部程序运行文件 (例如 php)<br />
map &lt;f9&gt; :w&lt;CR&gt;:!c:/php/php.exe %&lt;CR&gt;<br />
map &lt;f2&gt; :w&lt;CR&gt;:!perl -c %&lt;CR&gt;<br />
----------------------------------------<br />
" 在另一个buffer中，捕捉当前脚本的输出<br />
:new | r!perl # : 新建一个buffer，从另一个buffer中读入结果<br />
:new! x.out | r!perl # : 同上，并指定一个新文件名<br />
:new+read!ls<br />
----------------------------------------<br />
" create a new buffer, paste a register "q" into it, then sort new buffer<br />
:new +put q|%!sort<br />
----------------------------------------<br />
" 插入DOS换行符<br />
:%s/$/\&lt;C-V&gt;&lt;C-M&gt;&amp;/g : (that's what you type<br />
:%s/$/\&lt;C-Q&gt;&lt;C-M&gt;&amp;/g : for Win32) 对于Win32应该这样<br />
:%s/$/\^M&amp;/g : 你看到的^M是一个字符<br />
----------------------------------------<br />
" 自动删除行尾 Dos回车符和空格<br />
autocmd BufRead * silent! %s/[\r \t]\+$//<br />
autocmd BufEnter *.php :%s/[ \t\r]\+$//e<br />
----------------------------------------<br />
" 对指定文件或文件类型执行某个动作<br />
autocmd VimEnter c:/intranet/note011.txt normal! ggVGg?<br />
autocmd FileType *.pl exec('set fileformats=unix')<br />
----------------------------------------<br />
" 把最后一个命令贴到当前位置<br />
i&lt;c-r&gt;:<br />
" 把最后一个搜索指令贴到当前位置<br />
i&lt;c-r&gt;/<br />
----------------------------------------<br />
" 更多的完成功能<br />
&lt;C-X&gt;&lt;C-F&gt; :插入当前目录下的一个文件名到当前位置<br />
# 在insert模式下使用<br />
# 然后用 Ctrl-P/Ctrl-N 翻页<br />
----------------------------------------<br />
" 替换一个visual区域<br />
" 选择一个区域，然后输入 :s/Emacs/Vim/ 等等，vim会自动进入:模式<br />
:'&lt;,'&gt;s/Emacs/Vim/g : 前面的' '&lt;.'&gt; 是vim自动添加的<br />
gv : 重新选择前一个可视区域 (ULTRA)<br />
----------------------------------------<br />
" 在文件中插入行号<br />
:g/^/exec "s/^/".strpart(line(".")." ", 0, 4)<br />
:%s/^/\=strpart(line(".")." ", 0, 5)<br />
:%s/^/\=line('.'). ' '<br />
----------------------------------------<br />
#用VIM的方式来编号行<br />
:set number : 显示行号<br />
:map &lt;F12&gt; :set number!&lt;CR&gt; : Show linenumbers flip-flop<br />
:%s/^/\=strpart(line('.')." ",0,&amp;ts)<br />
#从任意行开始编号(需要perl)<br />
:'a,'b!perl -pne 'BEGIN{$a=223} substr($_,2,0)=$a++'<br />
#产生数字列表<br />
#Type in number on line say 223 in an empty file<br />
qqmnYP`n^Aq : in recording q repeat with @q<br />
" 递增已存在数字到文件末<br />
:.,$g/^\d/exe "normal! \&lt;c-a&gt;"<br />
" 高级递增，参见：<br />
</font><a href="http://vim.sourceforge.net/tip_view.php?tip_id=150" target="_blank"><font size="4">http://vim.sourceforge.net/tip_view.php?tip_id=150</font></a><br />
<font size="4">----------------------------------------<br />
" 高级递增 (真的很有用)<br />
" 把下面几句放到 _vimrc<br />
let g:I=0<br />
function! INC(increment)<br />
let g:I =g:I + a:increment<br />
return g:I<br />
endfunction<br />
" 例如从mark a 到mark b 递增，从223开始，步长为5<br />
:let I=223<br />
:'a,'bs/^/\=INC(5)/<br />
" create a map for INC<br />
cab viminc :let I=223 \| 'a,'bs/$/\=INC(5)/<br />
----------------------------------------<br />
" 生成从 23-64 的数字列表<br />
o23&lt;ESC&gt;qqYp&lt;C-A&gt;q40@q<br />
----------------------------------------<br />
" 在当前插入模式下编辑/移动 (真得很有用)<br />
&lt;C-U&gt; : 删除全部<br />
&lt;C-W&gt; : 删除最后一个单词<br />
&lt;HOME&gt;&lt;END&gt; : 移动到行首/行尾<br />
&lt;C-LEFTARROW&gt;&lt;C-RIGHTARROW&gt; : 向前/后移动一个单词<br />
&lt;C-X&gt;&lt;C-E&gt;,&lt;C-X&gt;&lt;C-Y&gt; : scroll while staying put in insert<br />
----------------------------------------<br />
#加密(小心使用，不要忘了密码)<br />
:X : vim会提示你输入密码<br />
:h :X<br />
----------------------------------------<br />
" 模式行 (使文件只读等)，必须在前/后 5行内<br />
// vim:noai:ts=2:sw=4:readonly:<br />
" vim:ft=html: : 使用 HTML 语法高亮<br />
:h modeline<br />
----------------------------------------<br />
" 建立你自己的菜单项<br />
amenu Modeline.Insert\ a\ VIM\ modeline &lt;Esc&gt;&lt;Esc&gt;ggOvim:ff=unix ts=4 ss=4&lt;CR&gt;<br />
vim60:fdm=marker&lt;esc&gt;gg<br />
----------------------------------------<br />
" 一个保存当前光标下的狭义字到一个文件的函数<br />
function! SaveWord()<br />
normal yiw<br />
exe ':!echo '.@0.' &gt;&gt; word.txt'<br />
endfunction<br />
map ,p :call SaveWord()<br />
----------------------------------------<br />
" 删除重复行的函数<br />
function! Del()<br />
if getline(".") == getline(line(".") - 1)<br />
norm dd<br />
endif<br />
endfunction<br />
<br />
:g/^/ call Del() #使用该函数的一个例子<br />
----------------------------------------<br />
" 双字节编码 (non alpha-numerics)<br />
:digraphs : 显示编码表<br />
:h dig : 帮助<br />
i&lt;C-K&gt;e' : 输入 &#233;<br />
i&lt;C-V&gt;233 : 输入 &#233; (Unix)<br />
i&lt;C-Q&gt;233 : 输入 &#233; (Win32)<br />
ga : 查看字符的hex值<br />
#删除非 ascii 字符<br />
:%s/[&lt;C-V&gt;128-&lt;C-V&gt;255]//gi : where you have to type the Control-V<br />
:%s/[?-?]//gi : Should see a black square &amp; a dotted y<br />
:%s/[&lt;C-V&gt;128-&lt;C-V&gt;255&lt;C-V&gt;01-&lt;C-V&gt;31]//gi : All pesky non-asciis<br />
:exec "norm /[\x00-\x1f\x80-\xff]/" : same thing<br />
#Pull a non-ascii character onto search bar<br />
yl/&lt;C-R&gt;" :<br />
/[^a-zA-Z0-9_[:space:][:punct:]] : search for all non-ascii<br />
----------------------------------------<br />
" 文件名自动完成 (例如 main_c.c)<br />
:e main_&lt;tab&gt; : tab 键完成<br />
gf : 打开光标处广义字命名的文件 (normal模式)<br />
main_&lt;C-X&gt;&lt;C-F&gt; : 文件名自动完成(insert模式)<br />
----------------------------------------<br />
" Vim复杂使用<br />
" 交换两个单词<br />
:%s/\&lt;\(on\|off\)\&gt;/\=strpart("offon", 3 * ("off" == submatch(0)), 3)/g<br />
" 交换两个单词<br />
:vnoremap &lt;C-X&gt; &lt;Esc&gt;`.``gvP``P<br />
----------------------------------------<br />
" 把text文件转换成html文件(oh,ft)<br />
:runtime! syntax/2html.vim : 转换 txt 成 html<br />
:h 2html<br />
----------------------------------------<br />
" VIM 有一个内部自带的 grep 命令<br />
:grep some_keyword *.c : 得到一个包含some_keyword的c文件名列表<br />
:cn : 去下一个出现的位置<br />
----------------------------------------<br />
" 强制无扩展名的文件的语法着色方式<br />
:set syntax=perl<br />
" 取消语法着色 (很有用)<br />
:set syntax off<br />
" 改变色彩主题 (在~vim/vim??/colors中的任何文件)<br />
:colorscheme blue<br />
" 通过使用模式行强迫使用 HTML 语法高亮<br />
# vim:ft=html:<br />
" 强制自动语法加亮(非标准的文件扩展)<br />
au BufRead,BufNewFile */Content.IE?/* setfiletype html<br />
----------------------------------------<br />
:set noma (non modifiable) : 防止修改<br />
:set ro (Read Only) : 只读保护<br />
----------------------------------------<br />
" 对话 (打开一堆文件)<br />
gvim file1.c file2.c lib/lib.h lib/lib2.h : 在"对话"中加载这些文件<br />
:mksession : 生成一个Session文件 (默认是Session.vim)<br />
:q<br />
gvim -S Session.vim : 重新加载所有文件<br />
----------------------------------------<br />
#标记(tags) (跳转到子程序/函数)<br />
taglist.vim : 很流行的插件<br />
:Tlist : 显示标记 (函数列表)<br />
&lt;C-]&gt; : 跳转到光标处的函数<br />
----------------------------------------<br />
" columnise a csv file for display only as may crop wide columns<br />
:let width = 20<br />
:let fill=' ' | while strlen(fill) &lt; width | let fill=fill.fill | endwhile<br />
:%s/\([^;]*\);\=/\=strpart(submatch(1).fill, 0, width)/ge<br />
:%s/\s\+$//ge<br />
" Highlight a particular csv column (put in .vimrc)<br />
function! CSVH(x)<br />
execute 'match Keyword /^\([^,]*,\)\{'.a:x.'}\zs[^,]*/'<br />
execute 'normal ^'.a:x.'f,'<br />
endfunction<br />
command! -nargs=1 Csv :call CSVH(&lt;args&gt;)<br />
" call with<br />
:Csv 5 : highlight fifth column<br />
----------------------------------------<br />
" 折叠：隐藏某些片断，使查看更容易<br />
zf} : 使用动作命令折叠一个段落<br />
v}zf : 使用可视模式折叠一个段落<br />
zf'a : 折叠到一个标记上<br />
zo : 打开折叠<br />
zc : 重新关闭折叠<br />
----------------------------------------<br />
" 显示"不可见字符"<br />
:set list<br />
:h listchars<br />
----------------------------------------<br />
" 如何在不进入插入模式的情况下粘贴"普通模式的命令"<br />
:norm qqy$jq<br />
----------------------------------------<br />
" 处理文件名<br />
:h filename-modifiers : 帮助<br />
:w % : 写入当前文件<br />
:w %:r.cfm : 改变文件扩展名为 .cfm<br />
:!echo %:p : 显示完整路径和文件名<br />
:!echo %:p:h : 只显示完整路径<br />
:!echo %:t : 只显示文件名<br />
:reg % : 显示文件名<br />
&lt;C-R&gt;% : 插入文件名 (插入模式)<br />
"%p : 插入文件名 (普通模式)<br />
/&lt;C-R&gt;% : 在文本中查找文件名<br />
----------------------------------------<br />
" 删除，但不破坏 buffer 内容<br />
"_d : 你一直想要的东西<br />
"_dw : 例如：删除一个单词 (使用黑洞???)<br />
----------------------------------------<br />
" 送完整的路径名到剪贴板，用于邮件附件等<br />
nnoremap &lt;F2&gt; :let @*=expand("%:p")&lt;cr&gt; :unix<br />
nnoremap &lt;F2&gt; :let @*=substitute(expand("%:p"), "/", "\\", "g")&lt;cr&gt; :win32<br />
----------------------------------------<br />
" 不用离开 Vim 就能修改文件名的简单 shell 脚本<br />
$ vim<br />
:r! ls *.c<br />
:%s/\(.*\).c/mv &amp; \1.bla<br />
:w !sh<br />
:q!<br />
----------------------------------------<br />
" 在一个文本里计算单词数<br />
g&lt;C-G&gt;<br />
----------------------------------------<br />
" 你自己设置高亮显示的例子<br />
:syn match DoubleSpace " "<br />
:hi def DoubleSpace guibg=#e0e0e0<br />
----------------------------------------<br />
" reproduce previous line word by word<br />
imap ] @@@&lt;ESC&gt;hhkyWjl?@@@&lt;CR&gt;P/@@@&lt;CR&gt;3s<br />
nmap ] i@@@&lt;ESC&gt;hhkyWjl?@@@&lt;CR&gt;P/@@@&lt;CR&gt;3s<br />
" 根据文件类型映射快捷键<br />
:autocmd bufenter *.tex map &lt;F1&gt; :!latex %&lt;CR&gt;<br />
:autocmd bufenter *.tex map &lt;F2&gt; :!xdvi -hush %&lt;.dvi&amp;&lt;CR&gt;<br />
----------------------------------------<br />
" 读取 MS-Word 文档，需要 antiword<br />
:autocmd BufReadPre *.doc set ro<br />
:autocmd BufReadPre *.doc set hlsearch!<br />
:autocmd BufReadPost *.doc %!antiword "%"<br />
----------------------------------------<br />
" a folding method<br />
vim: filetype=help foldmethod=marker foldmarker=&lt;&lt;&lt;,&gt;&gt;&gt;<br />
A really big section closed with a tag &lt;&lt;&lt;<br />
--- remember folds can be nested ---<br />
Closing tag &gt;&gt;&gt;<br />
----------------------------------------<br />
" Just Another Vim Hacker JAVH<br />
vim -c ":%s%s*%Cyrnfr)fcbafbe[Oenz(Zbbyranne%|:%s)[[()])-)Ig|norm Vg?"<br />
# 译释：呵呵，谁来解释一下吧！<br />
# 其实不过是在启动vim的时候执行了一个命令<br />
# 先写入了 Just Another Vim Hacker 的rot13编码<br />
# 然后再解码</font></div>
<br />
<img src ="http://www.blogjava.net/lingy/aggbug/293576.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-09-02 12:25 <a href="http://www.blogjava.net/lingy/archive/2009/09/02/293576.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vim 显示行号、语法高亮、自动缩进的设置</title><link>http://www.blogjava.net/lingy/archive/2009/09/02/293574.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 02 Sep 2009 04:24:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/09/02/293574.html</guid><description><![CDATA[<p>在Ubuntu中vim的配置文件存放在/etc/vim目录中，配置文件名为vimrc</p>
<p>在Fedora中vim的配置文件存放在/etc目录中，配置文件名为vimrc</p>
<p>在终端 输入以下命令来编辑vimrc配置文件：<br />
sudo vim /etc/vim/vimrc<br />
或者 sudo gedit /etc/vim/vimrc</p>
<p>1、显示行号<br />
&nbsp;&nbsp; <br />
在文件末端添加一新行，输入 set nu</p>
<p>2、语法高亮<br />
在文件中找到 "syntax on&nbsp;&nbsp; 这一行，去掉前面的双引号"，双引号是注释的意思</p>
<p>3、自动缩进</p>
<p>在文件末尾添加一行，输入&nbsp; set autoindent<br />
在添加一行，输入&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set cindent<br />
其中 autoindent 是自动缩进； cindent是特别针对 C语言语法自动缩进</p>
<p>注意：如果设置好以上设置后，VIM没有作出相应的动作，那么请你把你的VIM升级到最新版，一般只要在终端输入以下命令即可：sudo apt-get install vim</p>
<p>祝大家使用VIM愉快！</p>
<p>&nbsp;</p>
<p><br />
下面告诉大家怎么在linux下学习C，C在linux下的编译器是gcc，你在终端用vim输入C的源码</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如下保存到hello.c然后在命令行运行：</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ＃ gcc hello.c -o hello</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样会生成一个hello的可执行文件，在命令行里输入：</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ＃ ./hello</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 就能运行刚刚你编译的C源码了</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; linux下同样也能编译C++，linux下C++编译器是g＋＋，特别提示：</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C++的头文件是iostream不是iostream.h另外还需加入命名空间std，例如：</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //hello.cpp</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #include &lt;iostream&gt;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using namespace std;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int main ()</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"helloworld!"&lt;&lt;endl;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样才能正常编译，在该目录下，终端输入：</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # g++ hello.cpp -o hello</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样就生成一个hello的可执行文件，同样./hello命令运行它</p>
<p>&nbsp;</p>
<p>本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/Dreamcode/archive/2009/05/11/4166995.aspx</p>
<img src ="http://www.blogjava.net/lingy/aggbug/293574.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-09-02 12:24 <a href="http://www.blogjava.net/lingy/archive/2009/09/02/293574.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个简单的逻辑备份策略学习 </title><link>http://www.blogjava.net/lingy/archive/2009/07/10/286287.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Fri, 10 Jul 2009 08:58:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/07/10/286287.html</guid><description><![CDATA[<p>一个简单的逻辑备份策略学习<br />
需求：<br />
每天19:00备份一次业务用户<br />
每个备份最多保留7天<br />
每个备份用日期作为备份文件的名称</p>
<p>[root@localhost opt]# cat /home/autoBackup/ora_env<br />
export ORACLE_BASE=/opt/oracle<br />
export ORACLE_HOME=/opt/oracle/product/10.2.0<br />
export ORACLE_SID=STAPLES<br />
export ORACLE_TERM=xterm<br />
#export NLS_LANG="AMERICAN_AMERICA.ZHS16GBK"<br />
export ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data<br />
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib<br />
export PATH=$PATH:$ORACLE_HOME/bin</p>
<p>export TNS_ADMIN=$ORACLE_HOME/network/admin<br />
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/jlib<br />
export JAVA_HOME=$ORACLE_HOME/jdk<br />
export ORA_NLS10=$ORACLE_HOME/nls/data<br />
export LC_CTYPE=en_US.UTF-8</p>
<p>[root@localhost opt]# cat /opt/backup_exp.sh<br />
#!/bin/sh<br />
#CREATOR：daimin<br />
#function：backup database with expdp<br />
#usage：crontab on linux<br />
#last modify：tuolei 2007-08-29 create<br />
#set environment variable<br />
. /home/autoBackup/ora_env&nbsp; #设置exp的环境变量<br />
#开始备份，假定目录backupPath已经创建，并且backupPath=/opt/oracle/backup<br />
backupPath='/opt/oracle/backup/'<br />
FILE=`date +%Y%m%d`'.dmp'<br />
LOGFILE=`date +%Y%m%d`'.log'<br />
exp daimin/daimin@STAPLES file=$backupPath$FILE.dmp log=$backupPath$LOGFILE</p>
<p>#删除以前过期的备份<br />
find /opt/oracle/backup/* -name "*.dmp" -mtime +4 -exec rm {} \;</p>
<p>&nbsp;</p>
<p>[root@localhost etc]# crontab -e<br />
0 19 * * * /opt/backup_exp.sh &gt; /opt/oracle/backup/logs/backup_exp.log 2&gt;&amp;1<br />
~<br />
"crontab.XXXXmfkudb" 1L, 76C written<br />
crontab: installing new crontab<br />
[root@localhost etc]# crontab -l<br />
0 19 * * * /opt/backup_exp.sh &gt; /opt/oracle/backup/logs/backup_exp.log 2&gt;&amp;1</p>
<p>注意：<br />
1、/opt/backup_exp.sh需要具有可执行权限，才可以被root用户执行<br />
[root@localhost backup]# chmod +x /opt/backup_exp.sh<br />
否则会出现/bin/sh: /opt/backup_exp.sh: Permission denied错误<br />
2、需要在脚本的第一行加#!/bin/sh<br />
否则会出现/bin/sh: /opt/backup_exp.sh: cannot execute binary file错误<br />
3、注意在执行exp命令之前，需要设置环境变量，所以在逻辑备份脚本中执行了. /home/autoBackup/ora_env 命令;</p>
<p><br />
参考网页：<br />
http://blog.csdn.net/wzy0623/archive/2008/10/31/3193150.aspx<br />
http://www.linuxsir.org/main/?q=node/209</p>
<p><br />
本文来自CSDN博客，转载请标明出处：http://blog.csdn.net/daimin1983/archive/2008/12/13/3511380.aspx</p>
<img src ="http://www.blogjava.net/lingy/aggbug/286287.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-07-10 16:58 <a href="http://www.blogjava.net/lingy/archive/2009/07/10/286287.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux系统利用Crontab命令实现定时重启</title><link>http://www.blogjava.net/lingy/archive/2009/07/10/286193.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Fri, 10 Jul 2009 03:01:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/07/10/286193.html</guid><description><![CDATA[　作者：sixth
<p>　　Crontab是一个很方便的在unix/linux系统上定时(循环)执行某个任务的程序 </p>
<p>　　使用cron服务，用 service crond status 查看 cron服务状态，如果没有启动则 service crond start启动它， </p>
<p>　　cron服务是一个定时执行的服务，可以通过crontab 命令添加或者编辑需要定时执行的任务： </p>
<p>　　crontab -u //设定某个用户的cron服务，一般root用户在执行这个命令的时候需要此参数 </p>
<p>　　crontab -l //列出某个用户cron服务的详细内容 </p>
<p>　　crontab -r //删除没个用户的cron服务 </p>
<p>　　crontab -e //编辑某个用户的cron服务 </p>
<p>　　比如说root查看自己的cron设置：crontab -u root -l </p>
<p>　　再例如，root想删除fred的cron设置：crontab -u fred -r </p>
<p>　　在编辑cron服务时，编辑的内容有一些格式和约定，输入：crontab -u root -e </p>
<p>　　进入vi编辑模式，编辑的内容一定要符合下面的格式：*/1 * * * * ls &gt;&gt; /tmp/ls.txt </p>
<p>　　编辑/etc/crontab文件，在末尾加上一行： 30 5 * * * root init 6 这样就将系统配置为了每天早上5点30自动重新启动。 </p>
<p>　　需要将crond设置为系统启动后自动启动的服务，可以在/etc/rc.d/rc.local 中，在末尾加上 </p>
<p>　　service crond start </p>
<p>　　如果还需要在系统启动十加载其他服务，可以继续加上其他服务的启动命令。 </p>
<p>　　比如： service mysqld start </p>
<p>　　基本用法: </p>
<p>　　1. crontab -l </p>
<p>　　列出当前的crontab任务 </p>
<p>　　2. crontab -d </p>
<p>　　删除当前的crontab任务 </p>
<p>　　3. crontab -e (solaris5.8上面是 crontab -r) </p>
<p>　　编辑一个crontab任务,ctrl_D结束 </p>
<p>　　4. crontab filename </p>
<p>　　以filename做为crontab的任务列表文件并载入 </p>
<p>　　crontab file的格式: </p>
<p>　　crontab 文件中的行由 6 个字段组成，不同字段间用空格或 tab 键分隔。前 5 个字段指定命令要运行的时间 </p>
<p>　　分钟 (0-59) </p>
<p>　　小时 (0-23) </p>
<p>　　日期 (1-31) </p>
<p>　　月份 (1-12) </p>
<p>　　星期几(0-6，其中 0 代表星期日) </p>
<p>　　第 6 个字段是一个要在适当时间执行的字符串 </p>
<p>　　例子: </p>
<p>　　#MIN HOUR DAY MONTH DAYOFWEEK COMMAND </p>
<p>　　#每天早上6点10分 </p>
<p>　　10 6 * * * date </p>
<p>　　#每两个小时 </p>
<p>　　0 */2 * * * date (solaris 5.8似乎不支持此种写法) </p>
<p>　　#晚上11点到早上8点之间每两个小时，早上8点 </p>
<p>　　0 23-7/2，8 * * * date </p>
<p>　　#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 </p>
<p>　　0 11 4 * mon-wed date </p>
<p>　　#1月份日早上4点 </p>
<p>　　0 4 1 jan * date </p>
<p>　　补充：在使用crontab的时候，要特别注意的是运行脚本中能够访问到的环境变量和当前测试环境中的环境变量未必一致，一个比较保险的做法是在运行的脚本程序中自行设置环境变量(export) </p>
<p>　　(1)先建一个文件crond.txt如下， 每天早上5点36分重新启动 </p>
<p>　　36 5 * * * reboot </p>
<p>　　(2)上传到/opt目录 </p>
<p>　　(3)运行命令 </p>
<p>　　crontab /opt/crond.txt </p>
<p>　　crontab -l </p>
<p>　　让配置文件生效：如果让配置文件生效，还得重新启动cron，切记，既然每个用户下的cron配置文件修改后。也要重新启动cron服务器。 </p>
<p>　　在Fedora 和Redhat中，我们应该用； </p>
<p>　　[root@localhost ~]# /etc/init.d/crond restart </p>
<p>　　如果让crond 在开机时运行，应该改变其运行级别； </p>
<p>　　[root@localhost ~]# chkconfig --levels 35 crond on </p>
<p>　　service crond status 查看 cron服务状态，如果没有启动则 service crond start启动它， cron服务是一个定时执行的服务，可以通过crontab 命令添加或者编辑需要定时执行的任务 <br />
<br />
</p>
<span id="zoom" class="a14c">
<p style="text-indent: 2em">
<p style="text-indent: 2em">
<p style="text-indent: 2em">Crontab文件的每一行由六个域(minutes、hours、day of month、month、day of week、 command)组 成，域之间用空格或Tab分开，其中：
<p style="text-indent: 2em">minutes： 分钟域，值的范围是0到59
<p style="text-indent: 2em">hours： 小时域，值的范围是0到23
<p style="text-indent: 2em">day of month： 日期，值的范围是1到31
<p style="text-indent: 2em">month： 月份，值的范围是1到12
<p style="text-indent: 2em">day of week： 星期，值的范围是0到6，星期日值为0
<p style="text-indent: 2em">command： 所要运行的命令
<p style="text-indent: 2em">如果一个域是*，表明命令可以在该域所有可能的取值范围内执行。
<p style="text-indent: 2em">如果一个域是由连字符隔开的两个数字，表明命令可以在两个数字之间的范围内执行（包括两个数字 本身）。
<p style="text-indent: 2em">如果一个域是由逗号隔开的一系列值组成的，表明命令可以在这些值组成的范围内执行。
<p style="text-indent: 2em">如果日期域和星期域都有值，则这两个域都有效。
<p style="text-indent: 2em">编写一个文件，用以启动自动备份进程。
<p style="text-indent: 2em">
<p style="text-indent: 2em">cd /opt
<p style="text-indent: 2em">touch reboot.txt
<p style="text-indent: 2em">在reboot.txt中添加一下内容:
<p style="text-indent: 2em">0 4 * * * reboot
<p style="text-indent: 2em">
<p style="text-indent: 2em">crontab /opt/reboot.txt
<p style="text-indent: 2em">用crontab -e编辑定时操作，例如加入下行命令：
<p style="text-indent: 2em">用crontab －l命令来查看
<p style="text-indent: 2em">
<p style="text-indent: 2em">注意:需要启动服务(添加在rc.local中)
<p style="text-indent: 2em">
<p style="text-indent: 2em">重启crond任务
<p style="text-indent: 2em">
<p style="text-indent: 2em">/etc/init.d/cron restart (ubuntu下)
<p style="text-indent: 2em">
<p style="text-indent: 2em">第一种 在Fedora或Redhat 等以RPM包管理的系统中；
<p style="text-indent: 2em">
<p style="text-indent: 2em">[root@localhost ~]# /etc/init.d/crond start
<p style="text-indent: 2em">[root@localhost ~]# /etc/init.d/crond stop
<p style="text-indent: 2em">[root@localhost ~]# /etc/init.d/crond restart
<p style="text-indent: 2em">
<p style="text-indent: 2em">/etc/rc.d/init.d/crond restart
<p style="text-indent: 2em">
<p style="text-indent: 2em">命令简介
<p style="text-indent: 2em">
<p style="text-indent: 2em">crontab-操作每个用户的守护程序和该执行的时间表。
<p style="text-indent: 2em">
<p style="text-indent: 2em">部分参数说明
<p style="text-indent: 2em">crontab file [-u user]-用指定的文件替代目前的crontab。
<p style="text-indent: 2em">crontab-[-u user]-用标准输入替代目前的crontab.
<p style="text-indent: 2em">crontab-1[user]-列出用户目前的crontab.
<p style="text-indent: 2em">crontab-e[user]-编辑用户目前的crontab.
<p style="text-indent: 2em">crontab-d[user]-删除用户目前的crontab.
<p style="text-indent: 2em">crontab-c dir- 指定crontab的目录。
<p style="text-indent: 2em">crontab文件的格式：M H D m d cmd.
<p style="text-indent: 2em">M: 分钟（0-59）。
<p style="text-indent: 2em">H：小时（0-23）。
<p style="text-indent: 2em">D：天（1-31）。
<p style="text-indent: 2em">m: 月（1-12）。
<p style="text-indent: 2em">d: 一星期内的天（0~6，0为星期天）。
<p style="text-indent: 2em">cmd要运行的程序，程序被送入sh执行，这个shell只有USER,HOME,SHELL这三个环境变量。
<p style="text-indent: 2em">下面是一个例子文件：
<p style="text-indent: 2em">
<p style="text-indent: 2em">
<center><ccid_nobr>
<table border="1" cellspacing="0" bordercolorlight="black" bordercolordark="#ffffff" cellpadding="2" width="400" align="center">
    <tbody>
        <tr>
            <td style="font-size: 9pt" class="code" bgcolor="#e6e6e6">
            <pre><ccid_code>#MIN HOUR DAY MONTH DAYOFWEEK COMMAND
            #每天早上6点
            106* * * date
            #每两个小时
            0*/2* * * date
            #晚上11点到早上8点之间每两个小时，早上部点
            0 23-7/2，8* * * date
            #每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
            0 11 4* mon-wed date
            #1月份日早上4点
            0 4 1 jan* date
            范例
            lark:~&gt;crontab-1 列出用户目前的crontab.
            #MIN HOUR DAY MONTH DAYOFWEEK COMMAND
            10 6* * * date
            0*/2* * * date
            0 23-7/2,8 * * * date
            lark:~&gt;</ccid_code></pre>
            </td>
        </tr>
    </tbody>
</table>
</ccid_nobr></center>
<p style="text-indent: 2em">
<p style="text-indent: 2em">（责任编辑：云子） </p>
</span>
<img src ="http://www.blogjava.net/lingy/aggbug/286193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-07-10 11:01 <a href="http://www.blogjava.net/lingy/archive/2009/07/10/286193.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux定时任务系统Cron入门</title><link>http://www.blogjava.net/lingy/archive/2009/07/09/286135.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Thu, 09 Jul 2009 10:38:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/07/09/286135.html</guid><description><![CDATA[<p>cron是一个linux下的定时执行工具，可以在无需人工干预的情况下运行作业。由于Cron 是Linux的内置服务，但它不自动起来，可以用以下的方法启动、关闭这个服务：</p>
<p>&nbsp;</p>
<p>/sbin/service crond start //启动服务</p>
<p>/sbin/service crond stop //关闭服务</p>
<p>/sbin/service crond restart //重启服务</p>
<p>/sbin/service crond reload //重新载入配置</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>你也可以将这个服务在系统启动的时候自动启动：</p>
<p>&nbsp;</p>
<p>在/etc/rc.d/rc.local这个脚本的末尾加上：</p>
<p>/sbin/service crond start</p>
<p>&nbsp;</p>
<p>现在Cron这个服务已经在进程里面了，我们就可以用这个服务了，Cron服务提供以下几种接口供大家使用：</p>
<p>&nbsp;</p>
<p><strong>1.直接用crontab命令编辑</strong></p>
<p>&nbsp;</p>
<p>cron服务提供crontab命令来设定cron服务的，以下是这个命令的一些参数与说明：</p>
<p>&nbsp;</p>
<p>crontab -u //设定某个用户的cron服务，一般root用户在执行这个命令的时候需要此参数</p>
<p>&nbsp;</p>
<p>crontab -l //列出某个用户cron服务的详细内容</p>
<p>&nbsp;</p>
<p>crontab -r //删除没个用户的cron服务</p>
<p>&nbsp;</p>
<p>crontab -e //编辑某个用户的cron服务</p>
<p>&nbsp;</p>
<p>比如说root查看自己的cron设置：crontab -u root -l</p>
<p>&nbsp;</p>
<p>再例如，root想删除fred的cron设置：crontab -u fred -r</p>
<p>&nbsp;</p>
<p>在编辑cron服务时，编辑的内容有一些格式和约定，输入：crontab -u root -e</p>
<p>&nbsp;</p>
<p>进入vi编辑模式，编辑的内容一定要符合下面的格式：*/1 * * * * ls &gt;&gt; /tmp/ls.txt</p>
<p>&nbsp;</p>
<p>这个格式的前一部分是对时间的设定，后面一部分是要执行的命令，如果要执行的命令太多，可以把这些命令写到一个脚本里面，然后在这里直接调用这个脚本就可以了，调用的时候记得写出命令的完整路径。时间的设定我们有一定的约定，前面五个*号代表五个数字，数字的取值范围和含义如下：</p>
<p>&nbsp;</p>
<p>分钟　（0-59）</p>
<p>&nbsp;</p>
<p>小時　（0-23）</p>
<p>&nbsp;</p>
<p>日期　（1-31）</p>
<p>&nbsp;</p>
<p>月份　（1-12）</p>
<p>&nbsp;</p>
<p>星期　（0-6）//0代表星期天</p>
<p>&nbsp;</p>
<p>除了数字还有几个个特殊的符号就是"*"、"/"和"-"、","，*代表所有的取值范围内的数字，"/"代表每的意思,"*/5"表示每5个单位，"-"代表从某个数字到某个数字,","分开几个离散的数字。以下举几个例子说明问题：</p>
<p>&nbsp;</p>
<p>每天早上6点</p>
<p>&nbsp;</p>
<p>0 6 * * * echo "Good morning." &gt;&gt; /tmp/test.txt //注意单纯echo，从屏幕上看不到任何输出，因为cron把任何输出都email到root的信箱了。</p>
<p>&nbsp;</p>
<p>每两个小时</p>
<p>&nbsp;</p>
<p>0 */2 * * * echo "Have a break now." &gt;&gt; /tmp/test.txt</p>
<p>&nbsp;</p>
<p>晚上11点到早上8点之间每两个小时，早上八点</p>
<p>&nbsp;</p>
<p>0 23-7/2，8 * * * echo "Have a good dream：）" &gt;&gt; /tmp/test.txt</p>
<p>&nbsp;</p>
<p>每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点</p>
<p>&nbsp;</p>
<p>0 11 4 * 1-3 command line</p>
<p>&nbsp;</p>
<p>1月1日早上4点</p>
<p>&nbsp;</p>
<p>0 4 1 1 * command line</p>
<p>&nbsp;</p>
<p>每次编辑完某个用户的cron设置后，cron自动在/var/spool/cron下生成一个与此用户同名的文件，此用户的cron信息都记录在这个文件中，这个文件是不可以直接编辑的，只可以用crontab -e 来编辑。cron启动后每过一份钟读一次这个文件，检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务。</p>
<p>&nbsp;</p>
<p><strong>2.编辑/etc/crontab 文件配置cron</strong></p>
<p>&nbsp;</p>
<p>cron服务每分钟不仅要读一次/var/spool/cron内的所有文件，还需要读一次/etc/crontab,因此我们配置这个文件也能运用 cron服务做一些事情。用crontab配置是针对某个用户的，而编辑/etc/crontab是针对系统的任务。此文件的文件格式是：</p>
<p>&nbsp;</p>
<p>SHELL=/bin/bash</p>
<p>PATH=/sbin:/bin:/usr/sbin:/usr/bin</p>
<p>MAILTO=root //如果出现错误，或者有数据输出，数据作为邮件发给这个帐号</p>
<p>HOME=/ //使用者运行的路径,这里是根目录</p>
<p># run-parts</p>
<p>01 * * * * root run-parts /etc/cron.hourly //每小时执行/etc/cron.hourly内的脚本</p>
<p>02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本</p>
<p>22 4 * * 0 root run-parts /etc/cron.weekly //每星期执行/etc/cron.weekly内的脚本</p>
<p>42 4 1 * * root run-parts /etc/cron.monthly //每月去执行/etc/cron.monthly内的脚本</p>
<p>&nbsp;</p>
<p>大家注意"run-parts"这个参数了，如果去掉这个参数的话，后面就可以写要运行的某个脚本名，而不是文件夹名了</p>
<img src ="http://www.blogjava.net/lingy/aggbug/286135.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-07-09 18:38 <a href="http://www.blogjava.net/lingy/archive/2009/07/09/286135.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ubuntu Linux系统下Apt-get命令参数详解</title><link>http://www.blogjava.net/lingy/archive/2009/06/13/282008.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Sat, 13 Jun 2009 04:28:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/06/13/282008.html</guid><description><![CDATA[ 常用的APT命令参数：
<p>&nbsp;&nbsp;&nbsp; apt-cache search package 搜索包</p>
<p>&nbsp;&nbsp;&nbsp; apt-cache show package 获取包的相关信息，如说明、大小、版本等</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get install package 安装包</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get install package - - reinstall 重新安装包</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get -f install 修复安装"-f = ——fix-missing"</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get remove package 删除包</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get remove package - - purge 删除包，包括删除配置文件等</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get update 更新源</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get upgrade 更新已安装的包</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get dist-upgrade 升级系统</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get dselect-upgrade 使用 dselect 升级</p>
<p>&nbsp;&nbsp;&nbsp; apt-cache depends package 了解使用依赖</p>
<p>&nbsp;&nbsp;&nbsp; apt-cache rdepends package 是查看该包被哪些包依赖</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get build-dep package 安装相关的编译环境</p>
<p>&nbsp;&nbsp;&nbsp; apt-get source package 下载该包的源代码</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get clean &amp;&amp; sudo apt-get autoclean 清理无用的包</p>
<p>&nbsp;&nbsp;&nbsp; sudo apt-get check 检查是否有损坏的依赖</p>
<img src ="http://www.blogjava.net/lingy/aggbug/282008.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-06-13 12:28 <a href="http://www.blogjava.net/lingy/archive/2009/06/13/282008.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Ubuntu 8共享文件</title><link>http://www.blogjava.net/lingy/archive/2009/06/13/281957.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Sat, 13 Jun 2009 00:58:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/06/13/281957.html</guid><description><![CDATA[<p>环境：主机操作系统是Windows XP，虚拟机是Ubuntu 8.10，虚拟机是VirtualBox 2.1.0。</p>
<p>&nbsp;&nbsp;&nbsp; 1. 安装增强功能包(Guest Additions)</p>
<p>&nbsp;&nbsp;&nbsp; 安装好Ubuntu 8.10后，运行Ubuntu并登录。然后在VirtualBox的菜单里选择"设备(Devices)" -&gt; "安装增强功能包(Install Guest Additions)"。</p>
<p>&nbsp;&nbsp;&nbsp; 你会发现在Ubuntu桌面上多出一个光盘图标，这张光盘默认被自动加载到了文件夹/media/cdom0。进入命令行终端，输入：</p>
<p>&nbsp;&nbsp;&nbsp; cd /media/cdom0</p>
<p>&nbsp;&nbsp;&nbsp; sudo ./VboxLinuxAdditions.run</p>
<p>&nbsp;&nbsp;&nbsp; 开始安装工具包。安装完毕后会提示要重启Ubuntu。</p>
<p>&nbsp;&nbsp;&nbsp; 2. 设置共享文件夹</p>
<p>&nbsp;&nbsp;&nbsp; 重启完成后点击"设备(Devices)" -&gt; 共享文件夹(Shared Folders)菜单，添加一个共享文件夹，选项固定和临时是指该文件夹是否是持久的。共享名可以任取一个自己喜欢的，比如"gongxiang"，尽量使用英文名称。</p>
<p>&nbsp;&nbsp;&nbsp; 3. 挂载共享文件夹</p>
<p>&nbsp;&nbsp;&nbsp; 重新进入虚拟Ubuntu，在命令行终端下输入：</p>
<p>&nbsp;&nbsp;&nbsp; sudo mkdir /mnt/shared</p>
<p>&nbsp;&nbsp;&nbsp; sudo mount -t vboxsf gongxiang /mnt/shared</p>
<p>&nbsp;&nbsp;&nbsp; 其中"gongxiang"是之前创建的共享文件夹的名字。OK，现在Ubuntu和主机可以互传文件了。</p>
<p>&nbsp;&nbsp;&nbsp; 假如您不想每一次都手动挂载，可以在/etc/fstab中添加一项</p>
<p>&nbsp;&nbsp;&nbsp; gongxiang /mnt/shared vboxsf rw,gid=100,uid=1000,auto 0 0</p>
<p>&nbsp;&nbsp;&nbsp; 这样就能够自动挂载了。</p>
<p>&nbsp;&nbsp;&nbsp; 4. 卸载的话使用下面的命令：</p>
<p>&nbsp;&nbsp;&nbsp; sudo umount -f /mnt/shared</p>
<p>&nbsp;&nbsp;&nbsp; 注意：</p>
<p>&nbsp;&nbsp;&nbsp; 共享文件夹的名称千万不要和挂载点的名称相同。比如，上面的挂载点是/mnt/shared，如果共享文件夹的名字也是shared的话，在挂载的时候就会出现如下的错误信息(看http://www.virtualbox.org/ticket/2265)：</p>
<p>&nbsp;&nbsp;&nbsp; /sbin/mount.vboxsf: mounting failed with the error: Protocol error</p>
<p>&nbsp;&nbsp;&nbsp; 原因分析可以看Tips on running Sun Virtualbox的Shared Folder on a Linux Guest节。</p>
<p><br />
&nbsp;</p>
<img src ="http://www.blogjava.net/lingy/aggbug/281957.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-06-13 08:58 <a href="http://www.blogjava.net/lingy/archive/2009/06/13/281957.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VirtualBox网络配置（NAT和Host Interface）</title><link>http://www.blogjava.net/lingy/archive/2009/06/13/281951.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Fri, 12 Jun 2009 23:50:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/06/13/281951.html</guid><description><![CDATA[<p>VirtualBox提供了三种联网方式，在这里介绍前两种方式（NAT和HostInterface）的配置方法，第三种联网方式属于利用主机上的所有的虚拟机构建一个虚拟网络的方法，较简单，有兴趣的可以查看自带的用户手册。</p>
<p>第一种方式NAT：NAT（网络地址转换），你可以认为你的虚拟机是在你的主机后方，所有的联网请求都先通过主机（其实是通过主机的NAT服务器，这是VirtualBox内置的）。</p>
<ul>
    <li>优点：配置简单，可以说只要你的主机可以上网那么配置就没有什么难的。
    <li>缺点：虚拟OS的网络地址外网不可见，无法与你的主机进行通信，所以如果你要利用主机和你的虚拟OS调试网络应用程序，恐怕不能使用这种联网方式。 </li>
</ul>
<p>配置步骤：</p>
<p>第一步：打开VirtualBox的设置窗口</p>
<p><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b73e7600100baw5&amp;url=http://static11.photo.sina.com.cn/orignal/4b73e76006f99d03acbfa" target="_blank"><img src="http://static11.photo.sina.com.cn/bmiddle/4b73e76006f99d03acbfa"  alt="" /></a></p>
<p>第二步：进入网络设置界面，并将Attached To 选项选为NAT，点击确定</p>
<a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b73e7600100baw5&amp;url=http://static8.photo.sina.com.cn/orignal/4b73e76045c022ad1a467" target="_blank"></a>
<p><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b73e7600100baw5&amp;url=http://static11.photo.sina.com.cn/orignal/4b73e76006f99d19b0f5a" target="_blank"><img src="http://static11.photo.sina.com.cn/bmiddle/4b73e76006f99d19b0f5a"  alt="" /></a></p>
<p>第三步：启动虚拟操作系统，并在该系统中设置联网方式为DHCP（***重要），自动获得网络地址，VirtualBox会使用10.0.0.0这样的地址进行联网。这样你的虚拟OS上网，收发邮件都没有问题。但是无法和主机网络中的其他PC通信哦。</p>
<p>&nbsp;<wbr></p>
<p>下面介绍Host Interface的方式进行连接</p>
<p>先关闭你的虚拟操作系统^_^</p>
<p>第一步：打开设置界面，到网络选项卡下，并选择Host Interface连接方式，其他设置不需要改变</p>
<p><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b73e7600100baw5&amp;url=http://static16.photo.sina.com.cn/orignal/4b73e76045c024478a4bf" target="_blank"><img src="http://static16.photo.sina.com.cn/bmiddle/4b73e76045c024478a4bf"  alt="" /></a></p>
<p>&nbsp;<wbr></p>
<p>第二步：在网络选项卡下方的Host Interfaces中，新建一个网络接口，点击右方的绿色button。这样会在你的主机的网络连接中增加一个VirtualBox Host Interface 1这样的一个本地连接。</p>
<p><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b73e7600100baw5&amp;url=http://static8.photo.sina.com.cn/orignal/4b73e76045c024e154eb7" target="_blank"><img src="http://static8.photo.sina.com.cn/bmiddle/4b73e76045c024e154eb7"  alt="" /></a></p>
<p>在主机的网络连接中，新建的连接是默认断开的（当然咯，因为你的虚拟主机还没有启动嘛）</p>
<p><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b73e7600100baw5&amp;url=http://static7.photo.sina.com.cn/orignal/4b73e76045c02511b8cb6" target="_blank"><img src="http://static7.photo.sina.com.cn/bmiddle/4b73e76045c02511b8cb6"  alt="" /></a></p>
<p>第三步：很重要的一步，我就是卡在这里好久时间才搞明白。VirtualBox需要设置一个网桥，而使得所虚拟出来的网络接口可以通过你本机的网卡进行联网。怎么设置呢？</p>
<p>选中上图中的两个本地连接，右键，选择桥接就可以咯。</p>
<p>第四步：进入虚拟操作系统，修改联网方式为静态IP的方式，这样就OK了</p>
<img src ="http://www.blogjava.net/lingy/aggbug/281951.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-06-13 07:50 <a href="http://www.blogjava.net/lingy/archive/2009/06/13/281951.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>巧用find解决在目录查找特定字符串</title><link>http://www.blogjava.net/lingy/archive/2009/06/04/279944.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Thu, 04 Jun 2009 01:21:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/06/04/279944.html</guid><description><![CDATA[&nbsp;巧用find解决在目录查找特定字符串
<div class="t_msgfontfix">
<table cellspacing="0" cellpadding="0">
    <tbody>
        <tr>
            <td id="postmessage_790762" class="t_msgfont">有时候需要在一个<span class="t_tag" onclick="tagshow(event)" href="tag.php?name=%C4%BF%C2%BC">目录</span>下的所有文件查找含有特定字符串的文件，及特定字符串在文件中的定位。<br />
            自然反应肯定是使用grep命令，加上-r参数。<br />
            grep -r "pattern" .<br />
            可是如果待查找的目录是SVN目录，然后，主目录下面又有N多子目录，再用grep可就惨了，会查出好多.svn目录下含有该字符串的文件，而这些文件是 创建SVN目录时由 tortoiseSVN自动创建的辅助目录。这些文件我们是不修改的。但是大量这样的文件会严重影响我们的修改速度。<br />
            grep有--exclude参数，<br />
            grep -r --exclude=.svn "pattern" .<br />
            但是这只能排除掉主目录下边的.svn目录，对于子目录里的.svn就无能为力了。<br />
            <br />
            这时候就该find命令上场了。先说一下find中对我们最有用的参数-prune<br />
            如果在查找文件时希望忽略某个目录，那么可以使用-prune选项来指出需要忽略的目录。<br />
            <br />
            比如要在当前目录下查找不在.svn子目录之内的所有文件<br />
            find . -path "*\.svn" -prune -o -print<br />
            <br />
            -path "*\.svn" -prune -o -print 是 -path "*\.svn" -a -prune -o -print 的简写表达式。<br />
            按顺序求值, -a 和 -o 都是短路求值，与 shell 的 &amp;&amp; 和 || 类似。<br />
            如果 -path "*\.svn" 为真，则求值 -prune , -prune 返回真，与逻辑表达式为真；否则不求值 -prune，与逻辑表达式为假。<br />
            如果 -path "*\.svn" -a -prune 为假，则求值 -print ，-print返回真，或逻辑表达式为真；否则不求值 -print，或逻辑表达式为真。<br />
            这个表达式组合特例可以用伪码写为<br />
            if -path "*\.svn" then<br />
            -prune<br />
            else<br />
            -print<br />
            如果需要避开多个文件夹,可以使用下面的命令:<br />
            find . \( -path dir1 -o -path file1 \) -prune -o -print<br />
            圆括号表示表达式的结合。<br />
            \ 表示引用，即指示 shell 不对后面的字符作特殊解释，而留给 find 命令去解释其意义。<br />
            查找某一确定文件，-name等选项加在-o 之后<br />
            #find . \(-path dir1 -o -path file1 \) -prune -o -name "temp" -print<br />
            <br />
            排除目录已经做到了, 下一步就是用grep去<span class="t_tag" onclick="tagshow(event)" href="tag.php?name=%CB%D1%CB%F7">搜索</span>特定串了.结合xargs使用即可.<br />
            find . -path "*\.svn" -prune -o -print|xargs grep "CodeType" -n</td>
        </tr>
    </tbody>
</table>
</div>
<img src ="http://www.blogjava.net/lingy/aggbug/279944.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-06-04 09:21 <a href="http://www.blogjava.net/lingy/archive/2009/06/04/279944.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux字符集的修改方法 </title><link>http://www.blogjava.net/lingy/archive/2009/06/02/279603.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Tue, 02 Jun 2009 07:00:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/06/02/279603.html</guid><description><![CDATA[<p><br />
<br />
用export LANG=zh_CN.UTF-8这样只下次重起又要重设置</p>
<p>修改 /etc/sysconfig/i18n 文件，如 <br />
<br />
LANG="en_US"，xwindow会显示英文界面， <br />
<br />
LANG="zh_CN.GB18030"，xwindow会显示中文界面。</p>
<p>编辑/etc/sysconfig/i18n这个文件，<br />
LANG="zh_CN.GB18030"<br />
SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"<br />
SYSFONT="latarcyrheb-sun16"<br />
<br />
保存,重起.OK了<br />
注:<br />
I18N 是 internationalization 的缩写形式，意即在 i 和 n 之间有 18 个字母，本意是指软件的&#8220;国际化&#8221;.<br />
I18N支持多种语言，但是同一时间只能是英文和一种选定的语言，例如英文+中文、英文+德文、英文+韩文等等；<br />
原来的:<br />
LANG="zh_CN.UTF-8"<br />
SUPPORTED="zh_CN.UTF-8:zh_CN:zh"<br />
SYSFONT="latarcyrheb-sun16"</p>
<img src ="http://www.blogjava.net/lingy/aggbug/279603.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-06-02 15:00 <a href="http://www.blogjava.net/lingy/archive/2009/06/02/279603.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux中批量把文件名大写改成小写</title><link>http://www.blogjava.net/lingy/archive/2009/06/01/279434.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Mon, 01 Jun 2009 09:26:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/06/01/279434.html</guid><description><![CDATA[for fname in *; do mv $fname `echo $fname|tr [A-Z] [a-z]`; done<br />
变换后如果有相同的文件名会被覆盖掉的，可以加个参数<br />
<br />
for each in *; do mv <font color="red">-i</font> $each `echo $each|tr A-Z a-z`;done&nbsp;<br />
<br />
&nbsp;cat zeus-schema.sql|tr [A-Z] [a-z] &gt;zeus-schema1.sql
<img src ="http://www.blogjava.net/lingy/aggbug/279434.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-06-01 17:26 <a href="http://www.blogjava.net/lingy/archive/2009/06/01/279434.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux中locate 命令的使用</title><link>http://www.blogjava.net/lingy/archive/2009/05/28/278323.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Thu, 28 May 2009 06:22:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/28/278323.html</guid><description><![CDATA[<div id="content">
<p>名称：locate</p>
<p>使用权限：所有使用者</p>
<p>使用方式：</p>
<p>locate [-q] [-d ] [--database=]</p>
<p>　　locate [-r ] [--regexp=]</p>
<p>　　locate [-qv] [-o ] [--output=]</p>
<p>　　locate [-e ] [-f ] &lt;[-l ] [-c]<!--iwms_ad_begin-->
<table width="97%" align="center" border="0">
    <tbody>
        <tr>
            <td colspan="3">
            <div align="center"><script language="javaScript" src="../../system/system60.js" type="text/javascript"></script><script type="text/javascript"><!-- google_ad_client="pub-5195587195407606" ;
/* Linuxidc 468x60, ������ 08-8-31 */ google_ad_slot="9963827011" ; google_ad_width="468;
google_ad_height" = 60;
//-->
</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script><script>window.google_render_ad();</script><ins style="padding-right: 0px; padding-left: 0px; visibility: visible; padding-bottom: 0px; margin: 0px; width: 468px; border-top-style: none; padding-top: 0px; border-right-style: none; border-left-style: none; position: relative; height: 60px; border-bottom-style: none"><ins style="padding-right: 0px; display: block; padding-left: 0px; visibility: visible; padding-bottom: 0px; margin: 0px; width: 468px; border-top-style: none; padding-top: 0px; border-right-style: none; border-left-style: none; position: relative; height: 60px; border-bottom-style: none"><iframe id="google_ads_frame3" style="left: 0px; position: absolute; top: 0px" name="google_ads_frame" marginwidth="0" marginheight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-5195587195407606&amp;dt=1243491604640&amp;lmt=1240449717&amp;prev_slotnames=4448053148%2C0155470610&amp;output=html&amp;slotname=9963827011&amp;correlator=1243491604453&amp;url=http%3A%2F%2Fwww.linuxidc.com%2FLinux%2F2007-11%2F9113.htm&amp;ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fhl%3Dzh-CN%26q%3Dlinux%2Blocate%25E5%2591%25BD%25E4%25BB%25A4%26meta%3D%26aq%3D0%26oq%3Dlinux%2Blocate&amp;frm=0&amp;ga_vid=976550028.1243491604&amp;ga_sid=1243491604&amp;ga_hid=1133516476&amp;flash=9.0.115.0&amp;w=468&amp;h=60&amp;u_h=800&amp;u_w=1280&amp;u_ah=774&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_java=true&amp;dtd=16&amp;xpc=VbySMiPH9e&amp;p=http%3A//www.linuxidc.com" frameborder="0" width="468" scrolling="no" height="60" allowTransparency></iframe></ins></ins></div>
            </td>
        </tr>
    </tbody>
</table>
<!--iwms_ad_end--></p>
<p>　　&lt;[-U ] [-u]&gt;</p>
<p>　　locate [-Vh] [--version] [--help]</p>
<p>　　说明：</p>
<p><clk style="font-size: 14px; line-height: 15pt">locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案。其<nobr oncontextmenu="return false" onmousemove="$cE.MoW()" id="clickeyekey1" onmouseover="$cE.s(event,1)" style="font-size: 14px; color: #6600ff; line-height: 15pt; border-bottom: #6600ff 1px dotted; background-color: transparent; text-decoration: underline" onclick='$cE.c(event,1,"",1)' onmouseout="$cE.OuK()">方法</nobr>是先建立一个包括系统内所有档案名称及路径的数据库，之后当寻找时就只需查询这个数据库，而不必实际深入档案系统之中了。</clk></p>
<p>　　在一般的 distribution 之中，数据库的建立都被放在 contab 中自动执行。一般使用者在使用时只要用</p>
<p>　# locate your_file_name的型式就可以了。 参数：</p>
<p>-u</p>
<p>-U</p>
<p>建立数据库，-u 会由根目录开始，-U 则可以指定开始的位置。</p>
<p>　　-e</p>
<p>　　将</p>
<p>　　排除在寻找的范围之外。</p>
<p>　　-l</p>
<p><clk style="font-size: 14px; line-height: 15pt">　　如果 是 1．则启动安全模式。在安全模式下，使用者不会看到<nobr oncontextmenu="return false" onmousemove="$cE.MoW()" id="clickeyekey0" onmouseover="$cE.s(event,0)" style="font-size: 14px; color: #6600ff; line-height: 15pt; border-bottom: #6600ff 1px dotted; background-color: transparent; text-decoration: underline" onclick='$cE.c(event,0,"",1)' onmouseout="$cE.OuK()">权限</nobr>无法看到的档案。这会始速度减慢，因为 locate 必须至实际的档案系统中取得档案的权限资料。</clk></p>
<p>　　-f</p>
<p>　　将特定的档案系统排除在外，例如我们没有到理要把 proc 档案系统中的档案放在数据库中。</p>
<p>　　-q</p>
<p>　　安静模式，不会显示任何错误讯息。</p>
<p>　　-n</p>
<p>　　至多显示 个输出。</p>
<p>　　-r</p>
<p>　　使用正规运算式 做寻找的条件。</p>
<p>　　-o</p>
<p>　　指定数据库存的名称。</p>
<p>　　-d</p>
<p>　　指定数据库的路径</p>
<p>　　-h</p>
<p>　　显示辅助讯息</p>
<p>　　-v</p>
<p>　　显示更多的讯息</p>
<p>　　-V</p>
<p><clk style="font-size: 14px; line-height: 15pt">　　显示<nobr oncontextmenu="return false" onmousemove="$cE.MoW()" id="clickeyekey3" onmouseover="$cE.s(event,3)" style="font-size: 14px; color: #6600ff; line-height: 15pt; border-bottom: #6600ff 1px dotted; background-color: transparent; text-decoration: underline" onclick='$cE.c(event,3,"",1)' onmouseout="$cE.OuK()">程序</nobr>的版本讯息 范例：</clk></p>
<p>　　locate chdrv : 寻找所有叫 chdrv 的档案</p>
<p>　　locate -n 100 a.out : 寻找所有叫 a.out 的档案，但最多只显示 100 个</p>
<p>　　locate -u : 建立数据库</p>
</div>
　locate命令可以在搜寻数据库时快速找到档案，数据库由updatedb程序来更新，updatedb是由cron daemon周期性建立的，locate命令在搜寻数据库时比由整个由硬盘资料来搜寻资料来得快，但较差劲的是locate所找到的档案若是最近才建立或刚更名的，可能会找不到，在内定值中，updatedb每天会跑一次，可以由修改crontab来更新设定值。(etc/crontab)<!--iwms_ad_begin-->
<table width="97%" align="center" border="0">
    <tbody>
        <tr>
            <td colspan="3">
            <div align="center"><script language="javaScript" src="../../system/system60.js" type="text/javascript"></script><script type="text/javascript"><!-- google_ad_client="pub-5195587195407606" ;
/* Linuxidc 468x60, ������ 08-8-31 */ google_ad_slot="9963827011" ; google_ad_width="468;
google_ad_height" = 60;
//-->
</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script><script>window.google_render_ad();</script><ins style="padding-right: 0px; padding-left: 0px; visibility: visible; padding-bottom: 0px; margin: 0px; width: 468px; border-top-style: none; padding-top: 0px; border-right-style: none; border-left-style: none; position: relative; height: 60px; border-bottom-style: none"><ins style="padding-right: 0px; display: block; padding-left: 0px; visibility: visible; padding-bottom: 0px; margin: 0px; width: 468px; border-top-style: none; padding-top: 0px; border-right-style: none; border-left-style: none; position: relative; height: 60px; border-bottom-style: none"><iframe id="google_ads_frame3" style="left: 0px; position: absolute; top: 0px" name="google_ads_frame" marginwidth="0" marginheight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-5195587195407606&amp;dt=1243491713265&amp;lmt=1240449717&amp;prev_slotnames=4448053148%2C0155470610&amp;output=html&amp;slotname=9963827011&amp;correlator=1243491713109&amp;url=http%3A%2F%2Fwww.linuxidc.com%2FLinux%2F2007-11%2F9113p2.htm&amp;ref=http%3A%2F%2Fwww.linuxidc.com%2FLinux%2F2007-11%2F9113.htm&amp;frm=0&amp;ga_vid=186602105.1243491713&amp;ga_sid=1243491713&amp;ga_hid=1708668547&amp;flash=9.0.115.0&amp;w=468&amp;h=60&amp;u_h=800&amp;u_w=1280&amp;u_ah=774&amp;u_aw=1280&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;dtd=0&amp;xpc=WtokoQXoK6&amp;p=http%3A//www.linuxidc.com" frameborder="0" width="468" scrolling="no" height="60" allowTransparency></iframe></ins></ins></div>
            </td>
        </tr>
    </tbody>
</table>
<!--iwms_ad_end-->
<p>　　locate指定用在搜寻符合条件的档案，它会去储存档案与目录名称的数据库内，寻找合乎范本样式条件的档案或目录录，可以使用特殊字元（如&#8221;*&#8221;或&#8221;?&#8221;等）来指定范本样式，如指定范本为kcpa*ner, locate会找出所有起始字串为kcpa且结尾为ner的档案或目录，如名称为kcpartner若目录录名称为kcpa_ner则会列出该目录下包括子目录在内的所有档案。</p>
<p>　　locate指令和find找寻档案的功能类似，但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库，在执行loacte时直接找该索引，查询速度会较快，索引数据库一般是由操作系统管理，但也可以直接下达update强迫系统立即修改索引数据库。</p>
<p>　　不过第一次在执行update後再使用locate寻找档案常会失败，此时就要执行slocate ˉu该命令（也可执行updatedb指令，其效果相同）来更新slocate数据库，该命令会在/usr/sbin下产生slocate执行档，再由locate到此数据库寻找所要找的资料。</p>
<p>locate：该命令在运行时需要后台索引的数据库作为支撑，在 <a title="Ubuntu" href="http://www.linuxidc.com/topicnews.aspx?tid=2">Ubuntu</a> 中这个数据库文件位于 /var/cache/locate/locatedb。一般来说，这个数据库文件每天是通过 cron 自动更新的。如果不幸没有得到更新，那么可以执行 sudo updatedb 来手动更新。</p>
<p>假如我想要在系统中查找一个名为 linux.html 的文件，那么可以这样执行命令：locate linux.html。locate 搜索文件的速度很快，一会儿就会把结果列出来。locate 有一个十分有用的选项 -r，它可以让你在搜索文件时使用正则表达式。</p>
<img src ="http://www.blogjava.net/lingy/aggbug/278323.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-28 14:22 <a href="http://www.blogjava.net/lingy/archive/2009/05/28/278323.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VirtualBox 在XP下虚拟Ubuntu时不能全屏的问题解决方法</title><link>http://www.blogjava.net/lingy/archive/2009/05/28/278310.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Thu, 28 May 2009 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/28/278310.html</guid><description><![CDATA[<p>VirtualBox 在XP下虚拟Ubuntu时不能全屏的问题解决方法http://www.diybl.com/　2008-6-14 　网络　点击: 　[ 评论 ]<br />
文章搜索：&nbsp;&nbsp;&nbsp;&nbsp; 【点击打包该文章】 被过滤广告 <br />
【本站开通在线QQ讨论群】 </p>
<p>VirtualBox 在XP下虚拟Ubuntu时不能完全全屏的问题解决方法</p>
<p>本人LCD 1280&#215;800 Virtualbox 在深度XP下 虚拟 Ubuntu 8.04<br />
声音、网络一切正常，就是有不能完全全屏的问题，ubuntu作为客机的分辨率<br />
只能调到800&#215;600,经过一番检索+摸索，搞定，现整理如下：</p>
<p>1.安装VboxLinuxadditions<br />
a）安装make gcc lib等</p>
<p>-------------------------------<br />
apt-get install build-essential<br />
-------------------------------</p>
<p><br />
b）安装module-assistant</p>
<p>---------------------------------<br />
apt-get install module-assistant<br />
---------------------------------</p>
<p><br />
c）运行</p>
<p>-------------<br />
m-a prepare<br />
--------------</p>
<p>d）安装VboxLinuxadditions(选&#8220;设备&#8221;-&#8220;安装虚拟电脑工具包&#8221;)<br />
进入目录后运行</p>
<p>---------------------------------<br />
sudo ./VBoxLinuxAdditions.run<br />
---------------------------------</p>
<p>2.修改VBOX在Ubuntu里的设置</p>
<p>运行<br />
--------------------------------<br />
sudo gedit /etc/X11/xorg.conf<br />
--------------------------------<br />
后如下把部分修改</p>
<p>------------------------</p>
<p>Section "Device"<br />
Identifier "Configured Video Device"<br />
Driver "vboxvideo"<br />
EndSection</p>
<p>Section "Monitor"<br />
Identifier "Configured Monitor"<br />
EndSection</p>
<p>Section "Screen"<br />
Identifier "Default Screen"<br />
Monitor "Configured Monitor"<br />
Device "Configured Video Device"<br />
DefaultDepth 24 #我这里颜色质量只能开到24<br />
SubSection "Display"<br />
Modes "1280x800" "800x600" #按屏幕需要改就可以了<br />
EndSubSection<br />
EndSection</p>
<p>-------------------------------<br />
3.重启Ubuntu，然后分辨率就可以设置成1280&#215;800了</p>
<p>按"右ctrl+F" 进入全屏：无缝模式+共享剪贴板，linux和win的和平共存，<br />
VBox的网络设置成NAT，guest共享host的网络，这样应该就可以在恶心的校园网玩上linux了~ </p>
<p><br />
文章出处：http://www.diybl.com/course/6_system/linux/Linuxjs/2008614/125481.html</p>
<img src ="http://www.blogjava.net/lingy/aggbug/278310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-28 10:56 <a href="http://www.blogjava.net/lingy/archive/2009/05/28/278310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux sed 批量替换字符串</title><link>http://www.blogjava.net/lingy/archive/2009/05/28/278296.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 27 May 2009 16:40:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/28/278296.html</guid><description><![CDATA[比如，要将目录/modules下面所有<span class="t_tag" onclick="tagshow(event)" href="tag.php?name=%CE%C4%BC%FE">文件</span>中的zhangsan都修改成lisi，这样做：<br />
<br />
<span class="t_tag" onclick="tagshow(event)" href="tag.php?name=sed">sed</span> -i "s/zhangsan/lisi/g" `grep zhangsan -rl /modules`<br />
<br />
解释一下：<br />
<br />
<font color="#ff0000">-i 表示inplace edit，就地修改文件<br />
-r 表示搜索子目录<br />
-l 表示输出匹配的文件名</font><br />
<br />
这个<span class="t_tag" onclick="tagshow(event)" href="tag.php?name=%C3%FC%C1%EE">命令</span>组合很强大，要注意备份文件。<br />
<font face="Verdana "><font size="2"><br />
</font></font><br />
<font face="Verdana "><font size="2">（1）sed 'y/1234567890/ABCDEFGHIJ/' test_sed</font></font><br />
<font face="Verdana "><font size="2">sed 'y/1234567890/ABCDEFGHIJ/' filename<br />
ABCDEFGHIJ<br />
BCDEFGHIJA<br />
CDEFGHIJAB<br />
DEFGHIJABC<br />
注意变换关系是按两个list的位置对应变换</font></font><br />
<font face="Verdana "><font size="2"><font color="#ff0000">其中：test_sed的内容是：</font></font></font><br />
<font color="#ff0000">1234567890<br />
2345678901<br />
3456789012<br />
4567890123</font><br />
<font color="#ff0000"><br />
(2)</font><font face="Verdana "><font size="2"><font color="#ff0000">替换每行所有匹配</font></font></font><br />
<font color="#ff0000">sed 's/01/Ab/g' test_sed<br />
1234567890<br />
23456789Ab<br />
3456789Ab2<br />
456789Ab23</font><br />
<font color="#ff0000">注意：第一行的0，1没有分别替换为A,b</font><br />
<img src ="http://www.blogjava.net/lingy/aggbug/278296.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-28 00:40 <a href="http://www.blogjava.net/lingy/archive/2009/05/28/278296.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>任务管理</title><link>http://www.blogjava.net/lingy/archive/2009/05/28/278293.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 27 May 2009 16:24:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/28/278293.html</guid><description><![CDATA[jobs<br />
bg<br />
fg<br />
&amp;<br />
nohup
<img src ="http://www.blogjava.net/lingy/aggbug/278293.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-28 00:24 <a href="http://www.blogjava.net/lingy/archive/2009/05/28/278293.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux不常用命令之一 —— awk</title><link>http://www.blogjava.net/lingy/archive/2009/05/27/278272.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 27 May 2009 14:47:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/27/278272.html</guid><description><![CDATA[
		<font color="#ff0000" size="2">#awk  -F:  '模式    {动作}'  file<br /></font>
		<b>
				<font size="2">1)$ awk  '/yaoshuyin/ {print $3,$2;  print $4,$5}'  emp_names.txt<br />2)$ awk '/yaoshuyin | cnscn/ {print $3,$2} {print $4,$5}'  emp_names.txt<br />3)$ awk '$5  ˜  /yaoshuyin | tom/'  emp_names.txt</font>
		</b>
		<br />
		<font size="2">
				<b>4)#</b>
		</font>
		<font size="2">
				<b>awk '$0  ~  /DBNAME/  { print $0}'   800hr_db.inc.php   <br />$DB_DBNAME  ="800hr_db";</b>
		</font>
		<br />
		<font size="2">
				<b>5)#awk '$0  !~  /DBNAME/  { print $0}' 800hr_db.inc.php<br />&lt;?<br />   $DB_HOSTNAME="10.10.10.206";<br />   $DB_USERNAME="800hr";<br />   $DB_PASSWORD="800hr_buildhr_best";<br />?&gt;</b>
		</font>
		<br />
		<br />
		<font size="2">
				<b>6)#awk  '{FS=":"}  {print $2}'   emp_names.txt</b>
				<br />
				<br />
				<b>   #awk -F:  '{print $2}'  emp_names.txt<br /></b>
				<br />
				<br />
		</font>
		<font size="2">
				<br />
		</font>
		<b>
				<font size="2">
						<font color="#ff0000">#awk  -F:  -f  awk.prog   file</font>
				</font>
		</b>
		<br />
		<b>
				<br />
				<b>模式     </b>是一个正规表达式或由!、||、&amp;&amp;、或由圆括号分组的几个正规表达式。 正规式必须由/ ... /括起 <br />            /hello/  {print $2, $1}   或  <br />            /hello/ || /none/  {print $1, $2}  或  <br />            !/hello/ {print $1, $2}  或 <br />            /^[Hh1]/ {print $1, $2}<br />            /hello/ {<br />                         print $2<br />                         print $1<br />            }<br /><br />================================================================<br /><b><font color="#ff0000">#awk函数</font></b><br /><font color="#000000">length($2)</font><br /><br />BEGIN    {<br />                   print  "Beginning to process  the input data..."<br /><b>                   for(s=2;s&lt;6;++s)</b><br /><b>                   {</b><br /><b>                        if(s==4)  continue;</b><br /><b>                        print  "s is now",s</b><br /><b>                    }</b><br /><b>                    <font color="#ff0000">printf   "this is a number %d and a string %s\n",333,"333"</font></b><br />             }<br /><br />            {<br />                   s +=$2<br />                   print  $2, "length=" length($2), "s =" s<br />            }<br /><br />END      {<br />                   print "End of the process..."<br />            }<br /><br /><br />%c  (character)<br />%d  (decimal)<br />%o  (octal)<br />%x  (hexadecimal)<br />%f  (float)<br /><b>printf  "one=&gt;%1d&lt;,two=&gt;%2.3f&lt;,three=&gt;%-3d&lt;\n",sss,sss,sss     <font color="#ff0000"># -表示左对齐</font></b><br /><br /><br /><b><font size="2">====================================================</font></b><br /><b><font size="2"><font color="#ff0000">awk语句</font></font></b><br /><font size="2"><font color="#000000">{</font></font><br /><font size="2"><b>         if(s&gt;2  &amp;&amp; s&lt;20 || s==10)</b></font><br /><font size="2"><b>         {</b></font><br /><font size="2"><b>                ++s</b></font><br /><font size="2"><b>                print s</b></font><br /><font size="2"><b>          }</b></font><br /><br /><b><font size="2">          while(s&lt;10)</font></b><br /><b><font size="2">          {</font></b><br /><b><font size="2">                s=$1/32.3</font></b><br /><b><font size="2">                ++m</font></b><br /><b><font size="2">           }</font></b><br /><br /><b><font size="2">           for(s=0;s&lt;10;++s)</font></b><br /><b><font size="2">           {</font></b><br /><b><font size="2">                  s=$1/32.3</font></b><br /><b><font size="2">                 ++m</font></b><br /><b><font size="2">            }</font></b><br /><br /><font size="2">}</font><br /><br /><font size="2"><b>====================================================</b></font><br /><br /><br /><font size="2"><b>$ df -k | awk '{print $1}'| grep -v "Filesystem"</b><br />文件系统<br />/dev/sda2<br />/dev/sda1<br />none<br /></font><br /><br /><br /><font size="2"><b>$ df -k | awk '{print $1}'| grep -v "Filesystem" | sed s'/\/dev\///g'</b><br />文件系统<br />sda2<br />sda1<br />none<br /><br /><b>#awk  -f   prog  filename_list</b><br /><br /><b>=========================================================</b><br /><b>-F字段分隔符</b><br /><b>$0              代表整行</b><br /><b>$1              第一个字段</b><br /><b><font color="#000000">{print  $2, $1}                 输出时以，为字段分隔符</font></b><br /><br /><p>在我们使用Linux时，一些常用的命令，比如ls、date、pwd等（不一一例举了）不太容易忘记，但是对于一些不太常用的命令而言，不光光是参数，就连命令都会忘记（小弟是做开发工作的，所以比较容易忘记:D）</p><p>让我们继续，开始使用 awk，以了解其工作原理。在命令行中输入以下命令：</p><p>$ awk '{ print }' /etc/passwd<br /> <br />您将会见到 /etc/passwd 文件的内容出现在眼前。现在，解释 awk 做了些什么。调用 awk 时，我们指定 /etc/passwd 作为输入文件。执行 awk 时，它依次对 /etc/passwd 中的每一行执行 print 命令。所有输出都发送到 stdout，所得到的结果与与执行catting /etc/passwd完全相同。</p><p>现在，解释 { print } <a onclick="javascript:tagshow(event, '%B4%FA%C2%EB');" href="javascript:;" target="_self"><u><strong>代码</strong></u></a>块。在 awk 中，花括号用于将几块代码组合到一起，这一点类似于 C 语言。在代码块中只有一条 print 命令。在 awk 中，如果只出现 print 命令，那么将打印当前行的全部内容。</p><p>这里是另一个 awk 示例，它的作用与上例完全相同：</p><p>$ awk '{ print $0 }' /etc/passwd<br /> </p><p><br />在 awk 中， $0 变量表示整个当前行，所以 print 和 print $0 的作用完全一样。</p><p>如果您愿意，可以创建一个 awk 程序，让它输出与输入数据完全无关的数据。以下是一个示例：</p><p>$ awk '{ print "" }' /etc/passwd<br /> </p><p><br />只要将 "" 字符串传递给 print 命令，它就会打印空白行。如果<a onclick="javascript:tagshow(event, '%B2%E2%CA%D4');" href="javascript:;" target="_self"><u><strong>测试</strong></u></a>该脚本，将会发现对于 /etc/passwd 文件中的每一行，awk 都输出一个空白行。再次说明， awk 对输入文件中的每一行都执行这个脚本。以下是另一个示例：</p><p>$ awk '{ print "hiya" }' /etc/passwd<br /> </p><p><br />运行这个脚本将在您的屏幕上写满 hiya。:)</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>多个字段</p><p>awk 非常善于处理分成多个逻辑字段的文本，而且让您可以毫不费力地引用 awk 脚本中每个独立的字段。以下脚本将打印出您的系统上所有用户帐户的列表：</p><p>$ awk -F":" '{ print $1 }' /etc/passwd<br /> </p><p><br />上例中，在调用 awk 时，使用 -F 选项来指定 ":" 作为字段分隔符。awk 处理 print $1 命令时，它会打印出在输入文件中每一行中出现的第一个字段。以下是另一个示例：</p><p><br />$ awk -F":" '{ print $1 $3 }' /etc/passwd<br /> </p><p><br />以下是该脚本输出的摘录：</p><p>halt7<br />operator11<br />root0<br />shutdown6<br />sync5<br />bin1<br />....etc.<br /> </p><p><br />如您所见，awk 打印出 /etc/passwd 文件的第一和第三个字段，它们正好分别是用户名和用户标识字段。现在，当脚本运行时，它并不理想 -- 在两个输出字段之间没有空格！如果习惯于使用 bash 或 python 进行<a onclick="javascript:tagshow(event, '%B1%E0%B3%CC');" href="javascript:;" target="_self"><u><strong>编程</strong></u></a>，那么您会指望 print $1 $3 命令在两个字段之间插入空格。然而，当两个字符串在 awk 程序中彼此相邻时，awk 会连接它们但不在它们之间添加空格。以下命令会在这两个字段中插入空格：</p><p><br />$ awk -F":" '{ print $1 " " $3 }' /etc/passwd<br /> </p><p><br />以这种方式调用 print 时，它将连接 $1 、" " 和 $3 ，创建可读的输出。当然，如果需要的话，我们还可以插入一些文本标签：</p><p><br />$ awk -F":" '{ print "username: " $1 "\t\tuid:" $3" }' /etc/passwd<br /> </p><p><br />这将产生以下输出：</p><p>username: halt          uid:7<br />username: operator      uid:11<br />username: root          uid:0<br />username: shutdown      uid:6<br />username: sync          uid:5<br />username: bin           uid:1<br />....etc.<br /> </p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>外部脚本</p><p>将脚本作为命令行自变量传递给 awk 对于小的单行程序来说是非常简单的，而对于多行程序，它就比较复杂。您肯定想要在外部文件中撰写脚本。然后可以向 awk 传递 -f 选项，以向它提供此脚本文件：</p><p>$ awk -f myscript.awk myfile.in<br /> </p><p><br />将脚本放入文本文件还可以让您使用附加 awk 功能。例如，这个多行脚本与前面的单行脚本的作用相同，它们都打印出 /etc/passwd 中每一行的第一个字段：</p><p>BEGIN {<br />    FS=":"<br />}<br />{ print $1 }<br /> </p><p><br />这两个方法的差别在于如何设置字段分隔符。在这个脚本中，字段分隔符在代码自身中指定（通过设置 FS 变量），而在前一个示例中，通过在命令行上向 awk 传递 -F":" 选项来设置 FS。通常，最好在脚本自身中设置字段分隔符，只是因为这表示您可以少输入一个命令行自变量。我们将在本文的后面详细讨论 FS 变量。</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>BEGIN 和 END 块</p><p>通常，对于每个输入行，awk 都会执行每个脚本代码块一次。然而，在许多编程情况中，可能需要在 awk 开始处理输入文件中的文本之 前 执行初始化代码。对于这种情况，awk 允许您定义一个 BEGIN 块。我们在前一个示例中使用了 BEGIN 块。因为 awk 在开始处理输入文件之前会执行 BEGIN 块，因此它是初始化 FS（字段分隔符）变量、打印页眉或初始化其它在程序中以后会引用的全局变量的极佳位置。</p><p>awk 还提供了另一个特殊块，叫作 END 块。awk 在处理了输入文件中的所有行之后执行这个块。通常，END 块用于执行最终计算或打印应该出现在输出流结尾的摘要信息。</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>规则表达式和块</p><p>awk 允许使用规则表达式，根据规则表达式是否匹配当前行来选择执行独立代码块。以下示例脚本只输出包含字符序列 foo 的那些行：</p><p><br />/foo/ { print }<br /> </p><p><br />当然，可以使用更复杂的规则表达式。以下脚本将只打印包含浮点数的行：</p><p>/[0-9]+\.[0-9]*/ { print }<br /> </p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>表达式和块</p><p>还有许多其它方法可以选择执行代码块。我们可以将任意一种布尔表达式放在一个代码块之前，以控制何时执行某特定块。仅当对前面的布尔表达式求值为真时，awk 才执行代码块。以下示例脚本输出将输出其第一个字段等于 fred 的所有行中的第三个字段。如果当前行的第一个字段不等于 fred ，awk 将继续处理文件而不对当前行执行 print 语句：</p><p><br />$1 == "fred" { print $3 }<br /> </p><p><br />awk 提供了完整的比较运算符集合，包括 "=="、"&lt;"、"&gt;"、"&lt;="、"&gt;=" 和 "!="。另外，awk 还提供了 "~" 和 "!~" 运算符，它们分别表示“匹配”和“不匹配”。它们的用法是在运算符左边指定变量，在右边指定规则表达式。如果某一行的第五个字段包含字符序列 root ，那么以下示例将只打印这一行中的第三个字段：</p><p><br />$5 ~ /root/ { print $3 }<br /> </p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>条件语句</p><p>awk 还提供了非常好的类似于 C 语言的 if 语句。如果您愿意，可以使用 if 语句重写前一个脚本：</p><p><br />{<br />    if ( $5 ~ /root/ ) {<br />        print $3<br />    }<br />}<br /> </p><p><br />这两个脚本的功能完全一样。第一个示例中，布尔表达式放在代码块外面。而在第二个示例中，将对每一个输入行执行代码块，而且我们使用 if 语句来选择执行 print 命令。这两个方法都可以使用，可以选择最适合脚本其它部分的一种方法。</p><p>以下是更复杂的 awk if 语句示例。可以看到，尽管使用了复杂、嵌套的条件语句， if 语句看上去仍与相应的 C 语言 if 语句一样：</p><p><br />{<br />    if ( $1 == "foo" ) {<br />        if ( $2 == "foo" ) {<br />            print "uno"<br />        } else {<br />            print "one"<br />        }<br />    } else if ($1 == "bar" ) {<br />        print "two"<br />    } else {<br />        print "three"<br />    }<br />}<br /> </p><p><br />使用 if 语句还可以将代码：</p><p><br />! /matchme/ { print $1 $3 $4 }<br /> </p><p><br />转换成：</p><p>{  <br />    if ( $0 !~ /matchme/ ) {<br />        print $1 $3 $4<br />    }<br />}<br /> </p><p><br />这两个脚本都只输出 不 包含 matchme 字符序列的那些行。此外，还可以选择最适合您的代码的方法。它们的功能完全相同。</p><p>awk 还允许使用布尔运算符 "||"（逻辑与）和 "&amp;&amp;"（逻辑或），以便创建更复杂的布尔表达式：</p><p>( $1 == "foo" ) &amp;&amp; ( $2 == "bar" ) { print }<br /> </p><p><br />这个示例只打印第一个字段等于 foo 且 第二个字段等于 bar 的那些行。</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>数值变量！</p><p>至今，我们不是打印字符串、整行就是特定字段。然而，awk 还允许我们执行整数和浮点运算。通过使用数学表达式，可以很方便地编写计算文件中空白行数量的脚本。以下就是这样一个脚本：</p><p>BEGIN   { x=0 }<br />/^$/    { x=x+1 }<br />END     { print "I found " x " blank lines. :)" }<br /> </p><p><br />在 BEGIN 块中，将整数变量 x 初始化成零。然后，awk 每次遇到空白行时，awk 将执行 x=x+1 语句，递增 x 。处理完所有行之后，执行 END 块，awk 将打印出最终摘要，指出它找到的空白行数量。</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>字符串化变量</p><p>awk 的优点之一就是“简单和字符串化”。我认为 awk 变量“字符串化”是因为所有 awk 变量在内部都是按字符串形式存储的。同时，awk 变量是“简单的”，因为可以对它执行数学操作，且只要变量包含有效数字字符串，awk 会自动处理字符串到数字的转换步骤。要理解我的观点，请研究以下这个示例：</p><p>x="1.01"<br /># We just set x to contain the *string* "1.01"<br />x=x+1<br /># We just added one to a *string*<br />print x<br /># Incidentally, these are comments :)<br /> </p><p><br />awk 将输出：</p><p>2.01<br /> </p><p><br />有趣吧！虽然将字符串值 1.01 赋值给变量 x ，我们仍然可以对它加一。但在 bash 和 python 中却不能这样做。首先，bash 不支持浮点运算。而且，如果 bash 有“字符串化”变量，它们并不“简单”；要执行任何数学操作，bash 要求我们将数字放到丑陋的 $( ) ) 结构中。如果使用 python，则必须在对 1.01 字符串执行任何数学运算之前，将它转换成浮点值。虽然这并不困难，但它仍是附加的步骤。如果使用 awk，它是全自动的，而那会使我们的代码又好又整洁。如果想要对每个输入行的第一个字段乘方并加一，可以使用以下脚本：</p><p>{ print ($1^2)+1 }<br /> </p><p><br />如果做一个小实验，就可以发现如果某个特定变量不包含有效数字，awk 在对数学表达式求值时会将该变量当作数字零处理。</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>众多运算符</p><p>awk 的另一个优点是它有完整的数学运算符集合。除了标准的加、减、乘、除，awk 还允许使用前面演示过的指数运算符 "^"、模（余数）运算符 "%" 和其它许多从 C 语言中借入的易于使用的赋值操作符。</p><p>这些运算符包括前后加减（ i++ 、 --foo ）、加／减／乘／除赋值运算符（ a+=3 、 b*=2 、 c/=2.2 、 d-=6.2 ）。不仅如此 -- 我们还有易于使用的模／指数赋值运算符（ a^=2 、 b%=4 ）。</p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>字段分隔符</p><p>awk 有它自己的特殊变量集合。其中一些允许调整 awk 的运行方式，而其它变量可以被读取以收集关于输入的有用信息。我们已经接触过这些特殊变量中的一个，FS。前面已经提到过，这个变量让您可以设置 awk 要查找的字段之间的字符序列。我们使用 /etc/passwd 作为输入时，将 FS 设置成 ":"。当这样做有问题时，我们还可以更灵活地使用 FS。</p><p>FS 值并没有被限制为单一字符；可以通过指定任意长度的字符模式，将它设置成规则表达式。如果正在处理由一个或多个 tab 分隔的字段，您可能希望按以下方式设置 FS：</p><p>FS="\t+"<br /> </p><p><br />以上示例中，我们使用特殊 "+" 规则表达式字符，它表示“一个或多个前一字符”。</p><p>如果字段由空格分隔（一个或多个空格或 tab），您可能想要将 FS 设置成以下规则表达式：</p><p>FS="[[:space:]+]"<br /> </p><p><br />这个赋值表达式也有问题，它并非必要。为什么？因为缺省情况下，FS 设置成单一空格字符，awk 将这解释成表示“一个或多个空格或 tab”。在这个特殊示例中，缺省 FS 设置恰恰是您最想要的！</p><p>复杂的规则表达式也不成问题。即使您的记录由单词 "foo" 分隔，后面跟着三个数字，以下规则表达式仍允许对数据进行正确的分析：</p><p>FS="foo[0-9][0-9][0-9]"<br /> </p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>字段数量</p><p>接着我们要讨论的两个变量通常并不是需要赋值的，而是用来读取以获取关于输入的有用信息。第一个是 NF 变量，也叫做“字段数量”变量。awk 会自动将该变量设置成当前记录中的字段数量。可以使用 NF 变量来只显示某些输入行：</p><p>NF == 3 { print "this particular record has three fields: " $0 }<br /> </p><p><br />当然，也可以在条件语句中使用 NF 变量，如下：</p><p>{  <br />    if ( NF &gt; 2 ) {<br />        print $1 " " $2 ":" $3<br />    }<br />}<br /> </p><p> </p><p> </p><p><br />＝＋＝＋＝＋＝＋＝＋＝＋＝＋＝<br /> </p><p> </p><p>记录号</p><p>记录号 (NR) 是另一个方便的变量。它始终包含当前记录的编号（awk 将第一个记录算作记录号 1）。迄今为止，我们已经处理了每一行包含一个记录的输入文件。对于这些情况，NR 还会告诉您当前行号。然而，当我们在本系列以后部分中开始处理多行记录时，就不会再有这种情况，所以要注意！可以象使用 NF 变量一样使用 NR 来只打印某些输入行：</p><p>(NR &lt; 10 ) || (NR &gt; 100) { print "We are on record number 1-9 or 101+" }<br /> </p><p><br />另一个示例：</p><p>{<br />    #skip header<br />    if ( NR &gt; 10 ) {<br />        print "ok, now for the real information!"<br />    }<br />}<br /> </p></font></b>
<img src ="http://www.blogjava.net/lingy/aggbug/278272.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-27 22:47 <a href="http://www.blogjava.net/lingy/archive/2009/05/27/278272.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>假如要将当前目录下的所有文件中的windows替换成linux,可以执行</title><link>http://www.blogjava.net/lingy/archive/2009/05/27/278139.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 27 May 2009 01:19:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/27/278139.html</guid><description><![CDATA[假如要将当前目录下的所有文件中的windows替换成linux,可以执行<br />
<br />
find -type f | xargs perl -i -pe s%windows%linux%g<br />
<br />
<br />
<p>今天工作需要，其实就是一行命令，<br />
比如要将所有包含abc文件里面的abc替换为def</p>
<div class="hl-surround">
<ol class="hl-main ln-hide" ondblclick="linenumber(this)" title="Double click to show line number.">
    <li class="hl-firstline">find -type f | xargs perl -i -pe s%abc%def%g </li>
</ol>
</div>
<p>如果要把子目录的内容一起替换</p>
<div class="hl-surround">
<ol class="hl-main ln-show" ondblclick="linenumber(this)" title="Double click to hide line number.">
    <li class="hl-firstline">ls -R | xargs perl -i -pe s%abc%def%g </li>
</ol>
</div>
<p class="hl-firstline"><br />
&nbsp;</p>
<p>这两天在构建一个应用的使用用到了maven,由于project很大,足足有700多个pom.xml文件,更郁闷的是在很多pom.xml文件里都单独指定了资源库的url,我需要把这些资源库的url统一指定到nexus本地中央库.</p>
<p>手工一个个改文件配置有点不太实际,所以google了一下,找到批量替换文件内容的好方法,命令结构如下:</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family: monospace"><span style="font-weight: bold; color: #c20cb9">find</span> <span style="color: #660033">-name</span> <span style="color: #ff0000">'要查找的文件名'</span> <span style="font-weight: bold; color: #000000">|</span> <span style="font-weight: bold; color: #c20cb9">xargs</span> <span style="font-weight: bold; color: #c20cb9">perl</span> <span style="color: #660033">-pi</span> <span style="color: #660033">-e</span> <span style="color: #ff0000">'s|被替换的字符串|替换后的字符串|g'</span></pre>
</div>
</div>
<p>下面这个例子就是将当前目录及所有子目录下的所有pom.xml文件中的&#8221;<strong>http://repo1.maven.org/maven2</strong>&#8220;替换为&#8221;<strong>http://localhost:8081/nexus/content/groups/public</strong>&#8220;.</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family: monospace"><span style="font-weight: bold; color: #c20cb9">find</span> <span style="color: #660033">-name</span> <span style="color: #ff0000">'pom.xml'</span> <span style="font-weight: bold; color: #000000">|</span> <span style="font-weight: bold; color: #c20cb9">xargs</span> <span style="font-weight: bold; color: #c20cb9">perl</span> <span style="color: #660033">-pi</span> <span style="color: #660033">-e</span> <span style="color: #ff0000">'s|http://repo1.maven.org/maven2|http://localhost:8081/nexus/content/groups/public|g'</span></pre>
</div>
</div>
<p>这里用到了Perl语言,</p>
<div class="wp_syntax">
<div class="code">
<pre class="bash" style="font-family: monospace"><span style="font-weight: bold; color: #c20cb9">perl</span> <span style="color: #660033">-pi</span> <span style="color: #660033">-e</span></pre>
</div>
</div>
<p>在Perl 命令中加上-e 选项，后跟一行代码，那它就会像运行一个普通的Perl 脚本那样运行该代码.</p>
<p>从命令行中使用Perl 能够帮助实现一些强大的、实时的转换。认真研究正则表达式，并正确地使用，将会为您省去大量的手工编辑工作。</p>
 <img src ="http://www.blogjava.net/lingy/aggbug/278139.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-27 09:19 <a href="http://www.blogjava.net/lingy/archive/2009/05/27/278139.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>2007-08-02 | linux 替换文件内容 sed工具 </title><link>http://www.blogjava.net/lingy/archive/2009/05/27/278138.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Wed, 27 May 2009 01:16:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/27/278138.html</guid><description><![CDATA[		<h3>2007-08-02&#160;|&#160;linux 替换文件内容 sed工具 </h3>
		<div class="item-label">
				<span class="itemOpr">
						<a style="MARGIN-RIGHT: 10px" onmousedown="CA.q('blog_entryview_share_topright');" onclick="shareIt(this)" href="javascript:void(0);" entrytitle="linux 替换文件内容 sed工具" data-sharetype="31" data-title="#{@entryTitle}" data-url="http://vseb.blog.sohu.com/58003389.html" data-abstracts="#{#main-content@innerText&lt;51}" data-ext="{v:'1',xpt:'#{$_xpt}'}">
								<img title="分享" alt="分享" src="http://js2.pp.sohu.com.cn/ppp/blog/styles_ppp/images/btn_share_s2.gif" />
						</a>
				</span>
				<span id="itemId_58003389" class="itemOpr">
				</span>
		</div>
		<div class="clear">
		</div>
		<div class="item-body">
				<div class="item-content">标签：<span id="tagsBox"><a href="http://tag.blog.sohu.com/sed/" target="_blank"><font color="#000000">sed</font></a>&#160; </span></div>
				<div id="main-content" class="item-content">sed 编辑器是 Linux 系统管理员的工具包中最有用的资产之一， <br />因此，有必要彻底地了解其应用 <br /><br />Linux 操作系统最大的一个好处是它带有各种各样的实用工具。存在如此之多不同的实用工具，几乎不可能知道并了解所有这些工具。可以简化关键情况下操作的一个实用工具是 sed。它是任何管理员的工具包中最强大的工具之一，并且可以证明它自己在关键情况下非常有价值。 <br /><br />sed 实用工具是一个&#8220;编辑器&#8221;，但它与其它大多数编辑器不同。除了不面向屏幕之外，它还是非交互式的。这意味着您必须将要对数据执行的命令插入到命令行或要处理的脚本中。当显示它时，请忘记您在使用 Microsoft Word 或其它大多数编辑器时拥有的交互式编辑文件功能。sed 在一个文件（或文件集）中非交互式、并且不加询问地接收一系列的命令并执行它们。因而，它流经文本就如同水流经溪流一样，因而 sed 恰当地代表了流编辑器。它可以用来将所有出现的 "Mr. Smyth" 修改为 "Mr. Smith"，或将 "tiger cub" 修改为 "wolf cub"。流编辑器非常适合于执行重复的编辑，这种重复编辑如果由人工完成将花费大量的时间。其参数可能和一次性使用一个简单的操作所需的参数一样有限，或者和一个具有成千上万行要进行编辑修改的脚本文件一样复杂。sed 是 Linux 和 UNIX 工具箱中最有用的工具之一，且使用的参数非常少。 <br /><br />sed 的工作方式 <br /><br />sed 实用工具按顺序逐行将文件读入到内存中。然后，它执行为该行指定的所有操作，并在完成请求的修改之后将该行放回到内存中，以将其转储至终端。完成了这一行上的所有操作之后，它读取文件的下一行，然后重复该过程直到它完成该文件。如同前面所提到的，默认输出是将每一行的内容输出到屏幕上。在这里，开始涉及到两个重要的因素—首先，输出可以被重定向到另一文件中，以保存变化；第二，源文件（默认地）保持不被修改。sed 默认读取整个文件并对其中的每一行进行修改。不过，可以按需要将操作限制在指定的行上。 <br /><br />该实用工具的语法为： <br /><br /><br />sed [options] '{command}' [filename] <br /><br />在这篇文章中，我们将浏览最常用的命令和选项，并演示它们如何工作，以及它们适于在何处使用。 <br /><br />替换命令 <br /><br />sed 实用工具以及其它任何类似的编辑器的最常用的命令之一是用一个值替换另一个值。用来实现这一目的的操作的命令部分语法是： <br /><br /><br />'s/{old value}/{new value}/' <br /><br />因而，下面演示了如何非常简单地将 "tiger" 修改为 "wolf"： <br /><br /><br />$ echo The tiger cubs will meet on Tuesday after school | sed <br />'s/tiger/wolf/' <br />The wolf cubs will meet on Tuesday after school <br />$ <br /><br />注意如果输入是源自之前的命令输出，则不需要指定文件名—同样的原则也适用于 awk、sort 和其它大多数 LinuxUNIX 命令行实用工具程序。 <br /><br />多次修改 <br /><br />如果需要对同一文件或行作多次修改，可以有三种方法来实现它。第一种是使用 "-e" 选项，它通知程序使用了多条编辑命令。例如： <br /><br /><br />$ echo The tiger cubs will meet on Tuesday after school | sed -e ' <br />s/tiger/wolf/' -e 's/after/before/' <br />The wolf cubs will meet on Tuesday before school <br />$ <br /><br />这是实现它的非常复杂的方法，因此 "-e" 选项不常被大范围使用。更好的方法是用分号来分隔命令： <br /><br /><br />$ echo The tiger cubs will meet on Tuesday after school | sed ' <br />s/tiger/wolf/; s/after/before/' <br />The wolf cubs will meet on Tuesday before school <br />$ <br /><br />注意分号必须是紧跟斜线之后的下一个字符。如果两者之间有一个空格，操作将不能成功完成，并返回一条错误消息。这两种方法都很好，但许多管理员更喜欢另一种方法。要注意的一个关键问题是，两个撇号 (' ') 之间的全部内容都被解释为 sed 命令。直到您输入了第二个撇号，读入这些命令的 shell 程序才会认为您完成了输入。这意味着可以在多行上输入命令—同时 Linux 将提示符从 PS1 变为一个延续提示符（通常为 "&gt;"）—直到输入了第二个撇号。一旦输入了第二个撇号，并且按下了 Enter 键，则处理就进行并产生相同的结果，如下所示： <br /><br /><br />$ echo The tiger cubs will meet on Tuesday after school | sed ' <br />&gt; s/tiger/wolf/ <br />&gt; s/after/before/' <br />The wolf cubs will meet on Tuesday before school <br />$ <br /><br />全局修改 <br /><br />让我们开始一次看似简单的编辑。假定在要修改的消息中出现了多次要修改的项目。默认方式下，结果可能和预期的有所不同，如下所示： <br /><br /><br />$ echo The tiger cubs will meet this Tuesday at the same time <br />as the meeting last Tuesday | sed 's/Tuesday/Thursday/' <br />The tiger cubs will meet this Thursday at the same time <br />as the meeting last Tuesday <br />$ <br /><br />与将出现的每个 "Tuesday" 修改为 "Thursday" 相反，sed 编辑器在找到一个要修改的项目并作了修改之后继续处理下一行，而不读整行。sed 命令功能大体上类似于替换命令，这意味着它们都处理每一行中出现的第一个选定序列。为了替换出现的每一个项目，在同一行中出现多个要替换的项目的情况下，您必须指定在全局进行该操作： <br /><br /><br />$ echo The tiger cubs will meet this Tuesday at the same time <br />as the meeting last Tuesday | sed 's/Tuesday/Thursday/g' <br />The tiger cubs will meet this Thursday at the same time <br />as the meeting last Thursday <br />$ <br /><br />请记住不管您要查找的序列是否仅包含一个字符或词组，这种对全局化的要求都是必需的。 <br /><br />sed 还可以用来修改记录字段分隔符。例如，以下命令将把所有的 tab 修改为空格： <br /><br /><br />sed 's// /g' <br /><br />其中，第一组斜线之间的项目是一个 tab，而第二组斜线之间的项目是一个空格。作为一条通用的规则，sed 可以用来将任意的可打印字符修改为任意其它的可打印字符。如果您想将不可打印字符修改为可打印字符—例如，铃铛修改为单词 "bell"—sed 不是适于完成这项工作的工具（但 tr 是）。 <br /><br />有时，您不想修改在一个文件中出现的所有指定项目。有时，您只想在满足某些条件时才作修改—例如，在与其它一些数据匹配之后才作修改。为了说明这一点，请考虑以下文本文件： <br /><br /><br />$ cat sample_one <br />one 1 <br />two 1 <br />three 1 <br />one 1 <br />two 1 <br />two 1 <br />three 1 <br />$ <br /><br />假定希望用 "2" 来替换 "1"，但仅在单词 "two" 之后才作替换，而不是每一行的所有位置。通过指定在给出替换命令之前必须存在一次匹配，可以实现这一点： <br /><br /><br />$ sed '/two/ s/1/2/' sample_one <br />one 1 <br />two 2 <br />three 1 <br />one 1 <br />two 2 <br />two 2 <br />three 1 <br />$ <br /><br />现在，使其更加准确： <br /><br /><br />$ sed ' <br />&gt; /two/ s/1/2/ <br />&gt; /three/ s/1/3/' sample_one <br />one 1 <br />two 2 <br />three 3 <br />one 1 <br />two 2 <br />two 2 <br />three 3 <br />$ <br /><br />请再次记住唯一改变了的是显示。如果您查看源文件，您将发现它始终保持不变。您必须将输出保存至另一个文件，以实现永久保存。值得重复的是，不对源文件作修改实际是祸中有福—它让您能够对文件进行试验而不会造成任何实际的损害，直到让正确命令以您预期和希望的方式进行工作。 <br /><br />以下命令将修改后的输出保存至一个新的文件： <br /><br /><br />$ sed ' <br />&gt; /two/ s/1/2/ <br />&gt; /three/ s/1/3/' sample_one &gt; sample_two <br /><br />该输出文件将所有修改合并在其中，并且这些修改通常将在屏幕上显示。现在可以用 head、cat 或任意其它类似的实用工具来进行查看。 <br /><br />脚本文件 <br /><br />sed 工具允许您创建一个脚本文件，其中包含从该文件而不是在命令行进行处理的命令，并且 sed 工具通过 "-f" 选项来引用。通过创建一个脚本文件，您能够一次又一次地重复运行相同的操作，并指定比每次希望从命令行进行处理的操作详细得多的操作。 <br /><br />考虑以下脚本文件： <br /><br /><br />$ cat sedlist <br />/two/ s/1/2/ <br />/three/ s/1/3/ <br />$ <br /><br />现在可以在数据文件上使用脚本文件，获得和我们之前看到的相同的结果： <br /><br /><br />$ sed -f sedlist sample_one <br />one 1 <br />two 2 <br />three 3 <br />one 1 <br />two 2 <br />two 2 <br />three 3 <br />$ <br /><br />注意当调用 "-f" 选项时，在源文件内或命令行中不使用撇号。脚本文件，也称为源文件，对于想重复多次的操作和从命令行运行可能出错的复杂命令很有价值。编辑源文件并修改一个字符比在命令行中重新输入一条多行的项目要容易得多。 <br /><br />限制行 <br /><br />编辑器默认查看输入到流编辑器中的每一行，且默认在输入到流编辑器中的每一行上进行编辑。这可以通过在发出命令之前指定约束条件来进行修改。例如，只在此示例文件的输出的第 5 和第 6 行中用 "2" 来替换 "1"，命令将为： <br /><br /><br />$ sed '5,6 s/1/2/' sample_one <br />one 1 <br />two 1 <br />three 1 <br />one 1 <br />two 2 <br />two 2 <br />three 1 <br />$ <br /><br />在这种情况下，因为要修改的行是专门指定的，所以不需要替换命令。因此，您可以灵活地根据匹配准则（可以是行号或一种匹配模式）来选择要修改哪些行（从根本上限制修改）。 <br /><br />禁止显示 <br /><br />sed 默认将来自源文件的每一行显示到屏幕上（或重定向到一个文件中），而无论该行是否受到编辑操作的影响，"-n" 参数覆盖了这一操作。"-n" 覆盖了所有的显示，并且不显示任何一行，而无论它们是否被编辑操作修改。例如： <br /><br /><br />$ sed -n -f sedlist sample_one <br />$ <br /><br />$ sed -n -f sedlist sample_one &gt; sample_two <br />$ cat sample_two <br />$ <br /><br />在第一个示例中，屏幕上不显示任何东西。在第二个示例中，不修改任何东西，因此不将任何东西写到新的文件中—它最后是空的。这不是否定了编辑的全部目的吗？为什么这是有用的？它是有用的仅因为 "-n" 选项能够被一条显示命令 (-p) 覆盖。为了说明这一点，假定现在像下面这样对脚本文件进行了修改： <br /><br /><br />$ cat sedlist <br />/two/ s/1/2/p <br />/three/ s/1/3/p <br />$ <br /><br />然后下面是运行它的结果： <br /><br /><br />$ sed -n -f sedlist sample_one <br />two 2 <br />three 3 <br />two 2 <br />two 2 <br />three 3 <br />$ <br /><br />保持不变的行全部不被显示。只有受到编辑操作影响的行被显示了。在这种方式下，可以仅取出这些行，进行修改，然后把它们放到一个单独的文件中： <br /><br /><br />$ sed -n -f sedlist sample_one &gt; sample_two <br />$ <br /><br />$ cat sample_two <br />two 2 <br />three 3 <br />two 2 <br />two 2 <br />three 3 <br />$ <br /><br />利用它的另一种方法是只显示一定数量的行。例如，只显示 2-6 行，同时不做其它的编辑修改： <br /><br /><br />$ sed -n '2,6p' sample_one <br />two 1 <br />three 1 <br />one 1 <br />two 1 <br />two 1 <br />$ <br /><br />其它所有的行被忽略，只有 2-6 行作为输出显示。这是一项出色的功能，其它任何工具都不能容易地实现。Head 将显示一个文件的顶部，而 tail 将显示一个文件的底部，但 sed 允许从任意位置取出想要的任意内容。 <br /><br />删除行 <br /><br />用一个值替换另一个值远非流编辑器可以执行的唯一功能。它还具有许多的潜在功能，在我看来第二种最常用的功能是删除。删除与替换的工作方式相同，只是它删除指定的行（如果您想要删除一个单词而不是一行，不要考虑删除，而应考虑用空的内容来替换它—s/cat//）。 <br /><br />该命令的语法是： <br /><br /><br />'{what to find} d' <br /><br />从 sample_one 文件中删除包含 "two" 的所有行： <br /><br /><br />$ sed '/two/ d' sample_one <br />one 1 <br />three 1 <br />one 1 <br />three 1 <br />$ <br /><br />从显示屏中删除前三行，而不管它们的内容是什么： <br /><br /><br />$ sed '1,3 d' sample_one <br />one 1 <br />two 1 <br />two 1 <br />three 1 <br />$ <br /><br />只显示剩下的行，前三行不在显示屏中出现。对于流编辑器，一般当它们涉及到全局表达式时，特别是应用于删除操作时，有几点要记住： <br /><br />上三角号 (^) 表示一行的开始，因此，如果 "two" 是该行的头三个字符，则 <br /><br /><br />sed '/^two/ d' sample_one <br /><br />将只删除该行。 <br />美元符号 ($) 代表文件的结尾，或一行的结尾，因此，如果 "two" 是该行的最后三个字符，则 <br /><br /><br />sed '/two$/ d' sample_one <br /><br />将只删除该行。 <br /><br />将这两者结合在一起的结果： <br /><br /><br />sed '/^$/ d' {filename} <br /><br />删除文件中的所有空白行。例如，以下命令将 "1" 替换为 "2"，以及将 "1" 替换为 "3"，并删除文件中所有尾随的空行： <br /><br /><br />$ sed '/two/ s/1/2/; /three/ s/1/3/; /^$/ d' sample_one <br />one 1 <br />two 1 <br />three 1 <br />one 1 <br />two 2 <br />two 2 <br />three 1 <br />$ <br /><br />其通常的用途是删除一个标题。以下命令将删除文件中所有的行，从第一行直到第一个空行： <br /><br /><br />sed '1,/^$/ d' {filename} <br /><br />添加和插入文本 <br /><br />可以结合使用 sed 和 "a" 选项将文本添加到一个文件的末尾。实现方法如下： <br /><br /><br />$ sed '$a <br />&gt; This is where we stop <br />&gt; the test' sample_one <br />one 1 <br />two 1 <br />three 1 <br />one 1 <br />two 1 <br />two 1 <br />three 1 <br />This is where we stop <br />the test <br />$ <br /><br />在该命令中，美元符号 ($) 表示文本将被添加到文件的末尾。反斜线 () 是必需的，它表示将插入一个回车符。如果它们被遗漏了，则将导致一个错误，显示该命令是错乱的；在任何要输入回车的地方您必须使用反斜线。 <br /><br />要将这些行添加到第 4 和第 5 个位置而不是末尾，则命令变为： <br /><br /><br />$ sed '3a <br />&gt; This is where we stop <br />&gt; the test' sample_one <br />one 1 <br />two 1 <br />three 1 <br />This is where we stop <br />the test <br />one 1 <br />two 1 <br />two 1 <br />three 1 <br />$ <br /><br />这将文本添加到第 3 行之后。和几乎所有的编辑器一样，您可以选择插入而不是添加（如果您希望这样的话）。这两者的区别是添加跟在指定的行之后，而插入从指定的行开始。当用插入来代替添加时，只需用 "i" 来代替 "a"，如下所示： <br /><br /><br />$ sed '3i <br />&gt; This is where we stop <br />&gt; the test' sample_one <br />one 1 <br />two 1 <br />This is where we stop <br />the test <br />three 1 <br />one 1 <br />two 1 <br />two 1 <br />three 1 <br />$ <br /><br />新的文本出现在输出的中间位置，而处理通常在指定的操作执行以后继续进行。 <br /><br />读写文件 <br /><br />重定向输出的功能已经演示过了，但需要指出的是，在编辑命令运行期间可以同步地读入和写出文件。例如，执行替换，并将 1-3 行写到名称为 sample_three 的文件中： <br /><br /><br />$ sed ' <br />&gt; /two/ s/1/2/ <br />&gt; /three/ s/1/3/ <br />&gt; 1,3 w sample_three' sample_one <br />one 1 <br />two 2 <br />three 3 <br />one 1 <br />two 2 <br />two 2 <br />three 3 <br />$ <br /><br />$ cat sample_three <br />one 1 <br />two 2 <br />three 3 <br />$ <br /><br />由于为 w (write) 命令指定了 "1,3"，所以只有指定的行被写到了新文件中。无论被写的是哪些行，所有的行都在默认输出中显示。 <br /><br />修改命令 <br /><br />除了替换项目之外，还可以将行从一个值修改为另一个值。要记住的是，替换是对字符逐个进行，而修改功能与删除类似，它影响整行： <br /><br /><br />$ sed '/two/ c <br />&gt; We are no longer using two' sample_one <br />one 1 <br />We are no longer using two <br />three 1 <br />one 1 <br />We are no longer using two <br />We are no longer using two <br />three 1 <br />$ <br /><br />修改命令与替换的工作方式很相似，但在范围上要更大些—将一个项目完全替换为另一个项目，而无论字符内容或上下文。夸张一点讲，当使用替换时，只有字符 "1" 被字符 "2" 替换，而当使用修改时，原来的整行将被修改。在两种情况下，要寻找的匹配条件都仅为 "two"。 <br /><br />修改全部但&#8230;&#8230; <br /><br />对于大多数 sed 命令，详细说明各种功能要进行何种修改。利用感叹号，可以在除指定位置之外的任何地方执行修改—与默认的操作完全相反。 <br /><br />例如，要删除包含单词 "two" 的所有行，操作为： <br /><br /><br />$ sed '/two/ d' sample_one <br />one 1 <br />three 1 <br />one 1 <br />three 1 <br />$ <br /><br />而要删除除包含单词 "two" 的行之外的所有行，则语法变为： <br /><br /><br />$ sed '/two/ !d' sample_one <br />two 1 <br />two 1 <br />two 1 <br />$ <br /><br />如果您有一个文件包含一系列项目，并且想对文件中的每个项目执行一个操作，那么首先对那些项目进行一次智能扫描并考虑将要做什么是很重要的。为了使事情变得更简单，您可以将 sed 与任意迭代例程（for、while、until）结合来实现这一目的。 <br /><br />比如说，假定您有一个名为 "animals" 的文件，其中包含以下项目： <br /><br />pig <br />horse <br />elephant <br />cow <br />dog <br />cat <br /><br />您希望运行以下例程： <br /><br /><br />#mcd.ksh <br />for I in $* <br />do <br />echo Old McDonald had a $I <br />echo E-I, E-I-O <br />done <br /><br />结果将为，每一行都显示在 "Old McDonald has a" 的末尾。虽然对于这些项目的大部分这是正确的，但对于 "elephant" 项目，它有语法错误，因为结果应当为 "an elephant" 而不是 "a elephant"。利用 sed，您可以在来自 shell 文件的输出中检查这种语法错误，并通过首先创建一个命令文件来即时地更正它们： <br /><br /><br />#sublist <br />/ a a/ s/ a / an / <br />/ a e/ s/ a / an / <br />/a i/ s / a / an / <br />/a o/ s/ a / an / <br />/a u/ s/ a / an / <br /><br />然后执行以下过程： <br /><br /><br />$ sh mcd.ksh 'cat animals' | sed -f sublist <br /><br />现在，在运行了 mcd 脚本之后，sed 将在输出中搜索单个字母 a （空格，"a"，空格）之后紧跟了一个元音的任意位置。如果这种位置存在，它将把该序列修改为空格，"an"，空格。这样就使问题更正后才显示在屏幕上，并确保各处的编辑人员在晚上可以更容易地入睡。结果是： <br /><br />Old McDonald had a pig <br />E-I, E-I-O <br />Old McDonald had a horse <br />E-I, E-I-O <br />Old McDonald had an elephant <br />E-I, E-I-O <br />Old McDonald had a cow <br />E-I, E-I-O <br />Old McDonald had a dog <br />E-I, E-I-O <br />Old McDonald had a cat <br />E-I, E-I-O <br /><br />提前退出 <br /><br />sed 默认读取整个文件，并只在到达末尾时才停止。不过，您可以使用退出命令提前停止处理。只能指定一条退出命令，而处理将一直持续直到满足调用退出命令的条件。 <br /><br />例如，仅在文件的前五行上执行替换，然后退出： <br /><br /><br />$ sed ' <br />&gt; /two/ s/1/2/ <br />&gt; /three/ s/1/3/ <br />&gt; 5q' sample_one <br />one 1 <br />two 2 <br />three 3 <br />one 1 <br />two 2 <br />$ <br /><br />在退出命令之前的项目可以是一个行号（如上所示），或者一条查找/匹配命令： <br /><br /><br />$ sed ' <br />&gt; /two/ s/1/2/ <br />&gt; /three/ s/1/3/ <br />&gt; /three/q' sample_one <br />one 1 <br />two 2 <br />three 3 <br />$ <br /><br />您还可以使用退出命令来查看超过一定标准数目的行，并增加比 head 中的功能更强的功能。例如，head 命令允许您指定您想要查看一个文件的前多少行—默认数为 10，但可以使用从 1 到 99 的任意一个数字。如果您想查看一个文件的前 110 行，您用 head 不能实现这一目的，但用 sed 可以： <br /><br /><br />sed 110q filename <br /><br />处理问题 <br /><br />当使用 sed 时，要记住的重要事项是它的工作方式。它的工作方式是：读入一行，在该行上执行它已知要执行的所有任务，然后继续处理下一行。每一行都受给定的每一个编辑命令的影响。 <br /><br />如果您的操作顺序没有十分彻底地考虑清楚，那么这可能会很麻烦。例如，假定您需要将所有的 "two" 项目修改为 "three"，然后将所有的 "three" 修改为 "four"： <br /><br /><br />$ sed ' <br />&gt; /two/ s/two/three/ <br />&gt; /three/ s/three/four/' sample_one <br />one 1 <br />four 1 <br />four 1 <br />one 1 <br />four 1 <br />four 1 <br />four 1 <br />$ <br /><br />最初读取的 "two" 被修改为 "three"。然后它满足为下一次编辑建立的准则，从而变为 "four"。最终的结果不是想要的结果—现在除了 "four" 没有别的项目了，而本来应该有 "three" 和 "four"。 <br /><br />当执行这种操作时，您必须非常用心地注意指定操作的方式，并按某种顺序来安排它们，使得操作之间不会互相影响。例如： <br /><br /><br />$ sed ' <br />&gt; /three/ s/three/four/ <br />&gt; /two/ s/two/three/' sample_one <br />one 1 <br />three 1 <br />four 1 <br />one 1 <br />three 1 <br />three 1 <br />four 1 <br />$ <br /><br />这非常有效，因为 "three" 值在 "two" 变成 "three" 之前得到修改。 <br /><br />标签和注释 <br /><br />可以在 sed 脚本文件中放置标签，这样一旦文件变得庞大，可以更容易地说明正在发生的事情。存在各种各样与这些标签相关的命令，它们包括： <br />接下来的步骤 <br /><br />访问并收藏 Linux 技术中心 <br /><br />阅读 Dale Dougherty 和 Arnold Robbins 的著作 sed &amp; awk, 2nd Edition （O'Reilly &amp; Associates 出版社）。 <br /><br />: 冒号表示一个标签名称。例如： <br /><br /><br />:HERE <br /><br />以冒号开始的标签可以由 "b" 和 "t" 命令处理。 <br /><br />b {label} 充当 "goto" 语句的作用，将处理发送至前面有一个冒号的标签。例如， <br /><br /><br />b HERE <br /><br />将处理发送给行 <br /><br /><br />:HERE <br /><br />如果紧跟 b 之后没有指定任何标签，则处理转至脚本文件的末尾。 <br /><br />t {label} 只要自上次输入行或执行一次 "t" 命令以来进行了替换操作，就转至该标签。和 "b" 一样，如果没有给定标签名，则处理转至脚本文件的末尾。 <br /><br /># 符号作为一行的第一个字符将使整行被当作注释处理。注释行与标签不同，不能使用 b 或 t 命令来转到注释行上。</div>
		</div>
 <img src ="http://www.blogjava.net/lingy/aggbug/278138.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-27 09:16 <a href="http://www.blogjava.net/lingy/archive/2009/05/27/278138.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>重要的几个热键[Tab], [ctrl]-c, [ctrl]-d</title><link>http://www.blogjava.net/lingy/archive/2009/05/24/277640.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Sun, 24 May 2009 01:56:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/24/277640.html</guid><description><![CDATA[重要的几个热键[Tab], [ctrl]-c, [ctrl]-d<br />在继续后面的章节之前，这里很需要跟大家再来报告一件事，那就是我们的文字模式里头具有很多的功能按键， 这些按键可以辅助我们进行指令的编写与程序的中断呢！这几个按键请大家务必要记住的！很重要喔！<br />&#8226; [Tab] 按键<br />[Tab] 按键就是在键盘的大写灯切换按键([Caps Lock])上面的那个按键！ 在各种 Unix-Like 的 Shell 当中，这个 [Tab] 按键算是 Linux 的 Bash shell 最棒的功能之一了！ 他具有『命令补全』与『档案补齐』的功能喔！可以让我们少打很多字，但重点是， 可以避免我们打错指令或文件名称呢！很棒吧！但是 [Tab] 按键在不同的地方输入，会有不一样的结果喔！ 我们举下面的例子来说明。上一小节我们不是提到 cal 这个指令吗？如果我在指令列输入 ca 再按两次 [tab] 按键，会出现什么讯息？<br />[root@linux ~]# ca[tab][tab] &lt;==[tab]按键是紧接在 a 字母后面！<br /># 上面的 [tab] 指的是『按下那个tab键』，不是要您输入 ca[... 的意思喔！<br />cadaver callgrind_control capiinit case<br />cal cancel capinfos cat<br />calibrate_ppa cancel.cups captoinfo catchsegv<br />caller capifax card<br />callgrind capifaxrcvd cardctl<br />callgrind_annotate capiinfo cardmgr<br />发现什么事？所有以 ca 为开头的指令都被显示出来啦！很不错吧！ 那如果你输入 ls -al ~/.bash 两个 [tab] 会出现什么？<br />[root@linux ~]# ls -al ~/.bash[tab][tab]<br />.bash_history .bash_logout .bash_profile .bashrc<br />咦！在该目录下面所有以 .bash 的文件名称都会被显示出来了呢！注意看上面两个例子喔， 我们按 [tab] 按键的地方如果是在 command (第一个输入的数据) 后面时，他就代表着 『命令补全』，如果是接在第二个字以后的，就会变成『档案补齐』的功能了！<br />&#8226; [Tab] 接在一串指令的第一个字的后面，则为命令补全；<br />&#8226; [Tab] 接在一串指令的第二个字以后时，则为『档案补齐』！<br />善用 [tab] 按键真的是个很好的习惯！可以让您避免掉很多输入错误的机会！！！<br />&#8226; [Ctrl]-c 按键<br />在 Linux 底下，如果您输入了错误的指令或参数， 有的时候这个指令或程序会在系统底下『跑不停』这个时候怎么办？别担心， 如果您想让当前的程序『停掉』的话，可以输入：[Ctrl] 与 c 按键 (先按着 [Ctrl] 不放，且再按下 c 按键，是组合按键！)， 那就是 中断目前程序 的按键啦！ 举例来说，如果您输入了『 find / -type vbird 』这个指令时， 系统会开始跑一些东西 (先不要理会这个指令串的意义)，此时你给他按下 [Ctrl]-c 组合按键，嘿嘿！是否立刻发现这个指令串被终止了！就是这样的意思啦！ 不过你应该要注意的是，这个组合键是可以将正在运作中的指令中断的， 如果您正在运作比较重要的指令，可别急着使用这个组合按键喔！ ^_^<br />&#8226; [Ctrl]-d 按键<br />那么 [Ctrl]-d 是什么呢？就是 [Ctrl] 与 d 按键的组合啊！这个组合按键通常代表着： 『键盘输入结束 (End Of File, EOF 或 End Of Input)』的意思！ 另外，他也可以用来取代 exit 的输入呢！例如您想要直接离开文字接口， 可以直接按下 [Ctrl]-d 就能够直接离开了(相当于输入 exit 啊！)。<br />总之，在 Linux 底下，文字接口的功能是很强悍的！要多多的学习他， 而要学习他的基础要诀就是......多使用、多熟悉啦！ <img src ="http://www.blogjava.net/lingy/aggbug/277640.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-24 09:56 <a href="http://www.blogjava.net/lingy/archive/2009/05/24/277640.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>find命令</title><link>http://www.blogjava.net/lingy/archive/2009/05/23/277600.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Sat, 23 May 2009 11:49:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/23/277600.html</guid><description><![CDATA[find命令
<p>
<p>　　功能：在目录结构中搜索文件，并执行指定的操作。此命令提供了相当多的查找条件，功能很强大。
<p>
<p>　　语法：find 起始目录 寻找条件 操作
<p>
<p>　　说明：find命令从指定的起始目录开始，递归地搜索其各个子目录，查找满足寻找条件的文件并对之采取相关的操作。
<p>
<p>　　该命令提供的寻找条件可以是一个用逻辑运算符not、and、or组成的复合条件。逻辑运算符and、or、not的含义为：
<p>
<p>　　（1）and：逻辑与，在命令中用&#8220;-a&#8221;表示，是系统缺省的选项，表示只有当所给的条件都满足时，寻找条件才算满足。例如：
<p>
<p>　　$ find &#8211;name &#8217;tmp&#8217; &#8211;xtype c -user &#8217;inin&#8217;
<p>
<p>　　该命令寻找三个给定条件都满足的所有文件。
<p>
<p>　　（2）or：逻辑或，在命令中用&#8220;-o&#8221;表示。该运算符表示只要所给的条件中有一个满足时，寻找条件就算满足。
<p>
<p>　　例如：
<p>
<p>　　$ find &#8211;name &#8217;tmp&#8217; &#8211;o &#8211;name &#8217;mina*&#8217;
<p>
<p>　　该命令查询文件名为&#8217;tmp&#8217;或是匹配&#8217;mina*&#8217;的所有文件。
<p>
<p>　　（3）not：逻辑非，在命令中用&#8220;！&#8221;表示。该运算符表示查找不满足所给条件的文件。
<p>
<p>　　例如：
<p>
<p>　　$ find ! &#8211;name &#8217;tmp&#8217;
<p>
<p>　　该命令查询文件名不是&#8217;tmp&#8217;的所有文件。
<p>
<p>　　需要说明的是：当使用很多的逻辑选项时，可以用括号把这些选项括起来。为了避免Shell本身对括号引起误解，在话号前需要加转义字符&#8220;\\&#8221;来去除括号的意义。
<p>
<p>　　例：$ find \\（&#8211;name &#8217;tmp&#8217; &#8211;xtype c -user &#8217;inin&#8217; \\） 寻找条件有以下选项：
<p>
<p>　　首先，下列各个选项中的n值可以有三种输入方式，假设n为20，则：
<p>
<p>　　+20 表示20以后（21，22，23等）
<p>
<p>　　-20 表示20以前（19，18，17等）
<p>
<p>　　20 表示正好是20
<p>
<p>　　1. 以名称和文件属性查找。
<p>
<p>　　- name &#8217;字串&#8217; 查找文件名匹配所给字串的所有文件，字串内可用通配符*、?、[ ]。
<p>
<p>　　- lname &#8217;字串&#8217; 查找文件名匹配所给字串的所有符号链接文件，字串内可用通配符*、?、[ ]。
<p>
<p>　　-gid n 查找属于ID号为n的用户组的所有文件。
<p>
<p>　　-uid n 查找属于ID号为n的用户的所有文件。
<p>
<p>　　-group &#8217;字串&#8217; 查找属于用户组名为所给字串的所有的文件。
<p>
<p>　　-user &#8217;字串&#8217; 查找属于用户名为所给字串的所有的文件。
<p>
<p>　　-empty 查找大小为0的目录或文件。
<p>
<p>　　-path &#8217;字串&#8217; 查找路径名匹配所给字串的所有文件，字串内可用通配符*、?、[ ]。
<p>
<p>　　-perm 权限 查找具有指定权限的文件和目录，权限的表示可以如711，644。
<p>
<p>　　-size n[bckw] 查找指定文件大小的文件，n后面的字符表示单位，缺省为b，代表512字节的块。
<p>
<p>　　-type x 查找类型为x的文件，x为下列字符之一：
<p>
<p>　　b 块设备文件
<p>
<p>　　c 字符设备文件
<p>
<p>　　d 目录文件
<p>
<p>　　p 命名管道（FIFO）
<p>
<p>　　f 普通文件
<p>
<p>　　l 符号链接文件（symbolic links）
<p>
<p>　　s socket文件
<p>
<p>　　-xtype x 与-type基本相同，但只查找符号链接文件。
<p>
<p>　　2. 以时间为条件查找
<p>
<p>　　- amin n 查找n分钟以前被访问过的所有文件。
<p>
<p>　　- atime n 查找n天以前被访问过的所有文件。
<p>
<p>　　- cmin n 查找n分钟以前文件状态被修改过的所有文件。
<p>
<p>　　- ctime n 查找n天以前文件状态被修改过的所有文件。
<p>
<p>　　- mmin n 查找n分钟以前文件内容被修改过的所有文件。
<p>
<p>　　- mtime n 查找n天以前文件内容被修改过的所有文件。
<p>
<p>　　3. 可执行的操作
<p>
<p>　　- exec 命令名称 {} 对符合条件的文件执行所给的Linux 命令，而不询问用户是否需要执行该命令。{}表示命令的参数即为所找到的文件；命令的末尾必须以&#8220; \\；&#8221;结束。
<p>
<p>　　- ok 命令名称 { } 对符合条件的文件执行所给的Linux 命令，与exec不同的是，它会询问用户是否需要执行该命令。
<p>
<p>　　- ls 详细列出所找到的所有文件。
<p>
<p>　　- fprintf 文件名 将找到的文件名写入指定文件。
<p>
<p>　　- print 在标准输出设备上显示查找出的文件名。
<p>
<p>　　- printf 格式 格式的写法请参考有关C语言的书。
<p>
<p>　　例1：查找当前目录中所有以main开头的文件，并显示这些文件的内容。
<p>
<p>　　$ find . - name &#8216;main*&#8217; - exec more {} \\;
<p>
<p>　　例2：删除当前目录下所有一周之内没有被访问过的a .out或*.o文件。
<p>
<p>　　$ find . \\（- name a.out - o - name &#8216;*.o&#8217;\\）\\
<p>
<p>　　&gt; - atime +7 - exec rm {} \\;
<p>
<p>　　说明如下： 命令中的&#8220;.&#8221;表示当前目录，此时find将从当前目录开始，逐个在其子目录中查找满足后面指定条件的文件。\\（和\\）表示括号（），其中的&#8220;\\&#8221;称为转义符。之所以这样写是由于对Shell而言，（和）另有不同的含义，而不是这里的用于组合条件的用途。&#8220;- name a.out&#8221;是指要查找名为a.out的文件；&#8220;- name &#8216;*.o&#8217;&#8221;是指要查找所有名字以 .o结尾的文件。这两个- name之间的- o表示逻辑或（or），即查找名字为a.out或名字以 .o结尾的文件，find在当前目录及其子目录下找到这佯的文件之后，再进行判断，看其最后访问时间是否在7天以前（条件－atime +7），若是，则对该文件执行命令rm（- exec rm{ }\\;）。其中{ }代表当前查到的符合条件的文件名，\\；则是语法所要求的。上述命令中第一行的最后一个\\是续行符。当命令太长而在一行写不下时，可输入一个\\，之后系统将显示一个&gt;，指示用户继续输入命令。
<p>
<p>　　locate命令
<p>
<p>　　locate命令用于查找文件，它比find命令的搜索速度快，它需要一个数据库，这个数据库由每天的例行工作（crontab）程序来建立。当我们建立好这个数据库后，就可以方便地来搜寻所需文件了。
<p>
<p>　　该命令的一般形式为：
<p>
<p>　　locate 相关字
<p>
<p>　　例如：查找相关字issue
<p>
<p>　　$ locate issue
<p>
<p>　　/etc/issue
<p>
<p>　　/etc/issue.net
<p>
<p>　　/usr/man/man5/issue.5
<p>
<p>　　/usr/man/man5/issue.net.5 。 </p>
 <img src ="http://www.blogjava.net/lingy/aggbug/277600.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-23 19:49 <a href="http://www.blogjava.net/lingy/archive/2009/05/23/277600.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux kill用法 、killall、pkill、xkill</title><link>http://www.blogjava.net/lingy/archive/2009/05/21/274623.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Thu, 21 May 2009 14:34:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/21/274623.html</guid><description><![CDATA[<div id="postmessage_11907" class="t_msgfont"><strong><font color="#ff0000">一. 终止进程的工具 kill 、killall、pkill、xkill</font></strong><br />
终止一个进程或终止一个正在运行的程序，一般是通过 kill 、killall、pkill、xkill 等进行。比如一个程序已经死掉，但又不能退出，这时就应该考虑应用这些工具。<br />
另外应用的场合就是在服务器管理中，在不涉及数据库服务器程序的父进程的停止运行，也可以用这些工具来终止。为什么数据库服务器的父进程不能用这些工具杀死呢？原因很简单，这些工具在强行终止数据库服务器时，会让数据库产生更多的文件碎片，当碎片达到一定程度的时候，数据库就有崩溃的危险。比如mysql服务器最好是按其正常的程序关闭，而不是用pkill mysqld 或killall mysqld这样危险的动作；当然对于占用资源过多的数据库子进程，我们应该用kill 来杀掉。<br />
<font color="#0000ff"><strong>1. kill</strong></font><br />
<font color="#0000ff"><strong>kill的应用是和ps 或pgrep 命令结合在一起使用的；</strong></font><br />
kill 的用法：<br />
kill ［信号代码］&nbsp; &nbsp;&nbsp;&nbsp;进程ID<br />
注：信号代码可以省略；我们常用的<font color="#0000ff"><strong><font color="#ff0000">信号代码是 -9 ，表示强制终止</font>；</strong></font><br />
举例：<br />
[root@localhost ~]# <font color="#0000ff">ps auxf |grep httpd</font><br />
root 4939 0.0 0.0 5160 708 pts/3 S+ 13:10 0:00 \_ grep httpd<br />
root 4830 0.1 1.3 24232 10272 ? Ss 13:02 0:00 /usr/sbin/httpd<br />
apache 4833 0.0 0.6 24364 4932 ? S 13:02 0:00 \_ /usr/sbin/httpd<br />
apache 4834 0.0 0.6 24364 4928 ? S 13:02 0:00 \_ /usr/sbin/httpd<br />
apache 4835 0.0 0.6 24364 4928 ? S 13:02 0:00 \_ /usr/sbin/httpd<br />
apache 4836 0.0 0.6 24364 4928 ? S 13:02 0:00 \_ /usr/sbin/httpd<br />
apache 4840 0.0 0.6 24364 4928 ? S 13:02 0:00 \_ /usr/sbin/httpd <br />
我们查看httpd 服务器的进程；您也可以用<font color="#0000ff">pgrep -l httpd</font> 来查看；<br />
<br />
我们看上面例子中的第二列，就是进程PID的列，其中<font color="#0000ff"><strong>4830</strong></font>是httpd服务器的父进程，从4833－4840的进程都是它4830的子进程；如果我们杀掉父进程4830的话，其下的子进程也会跟着死掉；<br />
[root@localhost ~]# kill 4840 注：杀掉4840这个进程；<br />
[root@localhost ~]# ps -auxf |grep httpd 注：查看一下会有什么结果？是不是httpd服务器仍在运行？<br />
[root@localhost ~]# kill 4830 注：杀掉httpd的父进程；<br />
[root@localhost ~]# ps -aux |grep httpd 注：查看httpd的其它子进程是否存在，httpd服务器是否仍在运行？<br />
<br />
对于僵尸进程，可以用<strong><font color="#0000ff">kill -9 来强制终止退出；</font></strong><br />
<br />
比如一个程序已经彻底死掉，如果kill 不加信号强度是没有办法退出，最好的办法就是加信号强度 -9 ，后面要接杀父进程；比如；<br />
[root@localhost ~]# ps aux |grep gaim<br />
beinan 5031 9.0 2.3 104996 17484 ? S 13:23 0:01 gaim<br />
root 5036 0.0 0.0 5160 724 pts/3 S+ 13:24 0:00 grep gaim<br />
或 [root@localhost ~]# pgrep -l gaim<br />
5031 gaim<br />
[root@localhost ~]# kill -9 5031<br />
<strong><font color="#0000ff">2. killall</font></strong><br />
killall 通过程序的名字，直接杀死所有进程，咱们简单说一下就行了。<br />
用法：killall 正在运行的程序名<br />
killall 也和ps或pgrep 结合使用，比较方便；通过ps或pgrep 来查看哪些程序在运行；<br />
举例：<br />
[root@localhost beinan]# pgrep -l gaim<br />
2979 gaim<br />
[root@localhost beinan]# killall gaim<br />
<font color="#0000ff"><strong>3. pkill</strong></font><br />
pkill 和killall 应用方法差不多，也是直接杀死运行中的程序；如果您想杀掉单个进程，请用kill 来杀掉。<br />
应用方法：<br />
<font color="#0000ff">#pkill 正在运行的程序名</font><br />
<br />
举例：<br />
[root@localhost beinan]# pgrep -l gaim<br />
2979 gaim<br />
[root@localhost beinan]# pkill gaim<br />
<strong><font color="#0000ff">4. xkill</font></strong><br />
<strong><font color="#0000ff">xkill 是在桌面用的杀死图形界面的程序</font></strong>。比如当firefox 出现崩溃不能退出时，点鼠标就能杀死firefox 。当xkill运行时出来和个人脑骨的图标，哪个图形程序崩溃一点就OK了。如果您想终止xkill ，就按右键取消；<br />
xkill 调用方法：<br />
[root@localhost ~]# xkill<br />
<strong><font color="#ff0000">二.Linux的kill命令与信号控制</font></strong><br />
信号是用来与守护程序和进程通信的。任何活动任务都是一个进程，而守护程序是等待对某些事件做出反应或者按照日程安排执行任务的后台服务。一个程序必须有建在其中的信号处理程序用于捕获和应答信号。在LINUX中的signal参考指南解释了各种不同信号和这些信号的用途。信号是由&#8220;kill&#8221;命令发出的。kill -l命令可以显示一个可用信号列表及其编号。<br />
所有的守护程序和进程都有一个进程ID(PID)，例如使用ps命名所显示的内容：<br />
$ ps aux<br />
USER PID %CPU %MEM TTY STAT COMMAND<br />
root 1 0.0 0.1 ? S init [2]<br />
105 7783 0.0 0.2 ? Ss /usr/bin/dbus-daemon --system<br />
hal 7796 0.0 0.7 ? Ss /usr/sbin/hald<br />
postfix 7957 0.0 0.2 ? S qmgr -l -t fifo -u -c<br />
nagios 8371 0.0 0.2 ? SNs /usr/sbin/nagios /etc/nagios/nagios.cfg<br />
这个输出是经过简化的。你在系统中可以看到更多的行和栏目。如果某些进程消耗了你的全部CPU或者内存，你可以在这个输出的%CPU和%MEM列中发现它们。找到失控的进程的一种更快捷的方法是使用top命令，因为按照默认的设置，使用占用CPU资源最多的进程在最上面显示。我们可以使用一条&#8220;yes&#8221;命令来测试一下:<br />
$ yes carla is teh awesum<br />
这个命令将以很高的速度反复显示&#8220;carla is teh awesum&#8221;，直到你停止它运行。这将使你的CPU使用率达到警戒线。<br />
$ top<br />
...<br />
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND<br />
12144 carla 25 0 31592 17m 13m R 93.4 3.5 0:50.26 konsole<br />
22236 carla 15 0 2860 468 400 S 4.3 0.1 0:00.97 yes<br />
　分析一下这个结果，你会发现一些有趣的事，你会发现，占用CPU最多的程序是konsole虚拟终端程序，而不是&#8220;yes&#8221;命令，这是因为&#8220;yes&#8221;命令是在konsole终端程序中运行的。如果在一个&#8220;真正的&#8221;控制台(按Ctrl+alt+f2键)中运行同样的命令序列，你将看到&#8220;yes&#8221;命令被排在第一位。<br />
有许多停止&#8220;yes&#8221;命令运行的方式。如果你要回到运行它的shell中，按CTRL+c键就可以了。或者你可以在另一个shell中用&#8220;kill&#8221;命令停止&#8220;yes&#8221;命令的运行，Kill命令后面跟PID或者命令名称，如下如示:<br />
$ kill 22236<br />
或者<br />
$ killall yes<br />
按CTRL+c键发出一个SIGINT（信号2），这个信号是键盘要求取得控制权的中断信号。kill和killall这两个命令按照默认的设置都发出一个SIGTERM信号(编号15)。程序中可以设置对SIGTERM信号(15)是捕捉或者忽略，或者以不同的方式解释。因此，如果你的程序对于KILL命令的反应与你预期不同，很可能是被KILL的目标程序的问题。<br />
终止一个父进程通常也终止了它的子进程。不过，情况并不总是如此。你知道子进程是什么吗?使用ps命令加上-f选项就可以看到，如下所示:<br />
$ ps axf<br />
22371 ? R 2:35 _ konsole [kdeinit]<br />
22372 pts/3 Ss 0:00 | _ /bin/bash<br />
24322 pts/3 S+ 0:00 | | _ yes carla is teh awesum<br />
22381 pts/4 Rs 0:00 | _ /bin/bash<br />
24323 pts/4 R+ 0:00 | | _ ps axf<br />
现在，回到SIGHUP的话题<br />
SIGHUP的发音是&#8220;sig-hup&#8221;，是signal hangup的缩写，含义是&#8220;中止信号&#8221;。你如何发送一个SIGHUP信号呢?这里有几种方式:<br />
# kill -HUP [pid]<br />
# killall -HUP [process-name]<br />
# kill -1 [pid]<br />
# killall -1 [process-name]<br />
因此，你可以使用PID或者名称，信号名称或者号码。那么为什么要这样做而不使用/etc/init.d/foo命令重新启动呢?使用它们自己的init(初始化)文件来控制服务是优先选择的方式，因为这些文件通常包含健全和错误检查以及额外的功能。使用&#8220;kill&#8221;命令和信号的主要原因是尽可能明确地终止挂起和失控的进程，而不必重新启动或者登出。<br />
<strong>终止进程</strong><br />
正如你在关于信号的man page中所看到的，有十几种控制进程的方法。下面是一些常用的方法:<br />
kill -STOP [pid]<br />
发送SIGSTOP (17,19,23)停止一个进程，而并不消灭这个进程。<br />
kill -CONT [pid]<br />
发送SIGCONT (19,18,25)重新开始一个停止的进程。<br />
kill -KILL [pid]<br />
发送SIGKILL (9)强迫进程立即停止，并且不实施清理操作。<br />
kill -9 -1<br />
终止你拥有的全部进程。<br />
SIGKILL和SIGSTOP信号不能被捕捉、封锁或者忽略，但是，其它的信号可以。所以这是你的终极武器。<br />
Bash shell的Kil命令l<br />
Bash外壳包含一个内置的kill命令，当执行下面命令:<br />
$ type -all kill<br />
kill is a shell built-in<br />
kill is /bin/kill<br />
命令的结果表明有两个kill命令，一个是BASH的内置命令，另一个是/bin/kill可执行程序。一般来说这两个命令不太可能遇到冲突的情况，不过，如果你确实遇到了kill命令行为异常时，你可以明确的指定/bin/kill命令。<br />
你一定要进一步查阅下面的资源中列出的参考资源来了解Linux中kill的妙用，因为这是你进入维护Linux系统领域的门票。这些知识能够让你像做外科手术一样对系统进行维护，而不用在遇到问题时每一次都重新启动系统，就像我们知道的某些蹩脚的操作系统那样。<br />
<strong>资源</strong><br />
Linux Cookbook一书的第七章&#8220;开始和终止Linux&#8221;<br />
bash (1) - GNU Bourne-Again Shell<br />
yes (1) - 在被终止前反复打印字符<br />
signal (7) - 可用信号列表<br />
ps (1) - 报告当前进程的快照<br />
kill (1) - 向一个进程发出信号<br />
killall (1) - 按名字消灭进程<br />
pkill (1) - 根据名字和其它属性查看或者发出进程信号<br />
skill (1) - 发送一个信号或者报告进程状态<br />
xkill (1) - 按照X资源消灭一个客户程序<br />
<br />
http://hi.baidu.com/zhujiawei/blog/item/9ed008f3154be2cd0b46e003.html</div>
 <img src ="http://www.blogjava.net/lingy/aggbug/274623.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-21 22:34 <a href="http://www.blogjava.net/lingy/archive/2009/05/21/274623.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux下Kill多个进程的妙招</title><link>http://www.blogjava.net/lingy/archive/2009/05/21/274478.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Thu, 21 May 2009 14:22:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/21/274478.html</guid><description><![CDATA[<h2>[转帖]Linux下Kill多个进程的妙招</h2>
<div id="postmessage_102542" class="t_msgfont">　　工作中经常需要Kill多个进程，如果这些进程有共同的特点，就可以用一条命令Kill掉它们。比如清除Oracle数据库的所有远程连接进程：<br />
<br />
<br />
<br />
　　ps -efww|grep LOCAL=NO|grep -v grep|cut -c 9-15|xargs kill -9<br />
<br />
<br />
<br />
　　管道符&#8220;|&#8221;用来隔开两个命令，管道符左边命令的输出会作为管道符右边命令的输入。下面说说用管道符联接起来的几个命令：<br />
<br />
　　ps -efww|grep LOCAL=NO|grep -v grep|cut -c 9-15|xargs kill -9<br />
<br />
<br />
　　管道符&#8220;|&#8221;用来隔开两个命令，管道符左边命令的输出会作为管道符右边命令的输入。下面说说用管道符联接起来的几个命令：<br />
<br />
&#8220;ps -efww&#8221;是Red Hat 7.0里查看所有进程的命令。这时检索出的进程将作为下一条命令&#8220;grep LOCAL=NO&#8221;的输入。<br />
<br />
<br />
　　&#8220;grep LOCAL=NO&#8221;的输出结果是，所有含有关键字&#8220;LOCAL=NO&#8221;的进程，这是Oracle数据库中远程连接进程的共同特点。<br />
<br />
<br />
　　&#8220;grep -v grep&#8221;是在列出的进程中去除含有关键字&#8220;grep&#8221;的进程。<br />
<br />
<br />
　　&#8220;cut -c 9-15&#8221;是截取输入行的第9个字符到第15个字符，而这正好是进程号PID。<br />
<br />
<br />
　　&#8220;xargs kill -9&#8221;中的xargs命令是用来把前面命令的输出结果（PID）作为&#8220;kill -9&#8221;命令的参数，并执行该命令。&#8220;kill -9&#8221;会强行杀掉指定进程，这样就成功清除了oracle的所有远程连接进程。其它类似的任务，只需要修改&#8220;grep LOCAL=NO&#8221;中的关键字部分就可以了。<br />
</div>
 <img src ="http://www.blogjava.net/lingy/aggbug/274478.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-21 22:22 <a href="http://www.blogjava.net/lingy/archive/2009/05/21/274478.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>awk文本处理总结(转载)</title><link>http://www.blogjava.net/lingy/archive/2009/05/19/271482.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Tue, 19 May 2009 02:43:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/19/271482.html</guid><description><![CDATA[<p>作为技术支持工程我们最最经常的工作就是经常碰到要处理文本文件，不管是什么数据库最后都可以导成文本，我们就可以对他进行处理了，这样即使你不是对所有数据库操作都很熟悉也可以对他的数据进行处理了。<br />
&nbsp;&nbsp;&nbsp; 我们必须的工具有两个一个是shell一个是awk，awk对于处理文本文件是最最适合的掌握了awk我们就可以很方便的处理文本文件再借助一些shell命令我们可以很方便得到自己想要的结果。现在从简单的例子来总结一下我觉得会经常碰到的问题。<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; awk入门篇<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; awk入门，文本内容example1.txt.<br />
&nbsp;&nbsp;&nbsp; <br />
user1 password1 username1 unit1 10<br />
user2 password2 username2 unit2 20<br />
user3 password3 username3 unit3 30&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp; 在unix环境中我们可以使用一下命令来打印出第一列<br />
&nbsp;&nbsp;&nbsp; <br />
[root@mail awk]# awk '{print $1}' example1.txt</p>
<p>&nbsp;&nbsp;&nbsp; 得到的结果是如下，解释一下"'{" 单引号大括号包含awk语句是为了和shell 命令区别，$1的意思就是文本文件的第一列，正常的awk命令跟随-F参数指定间隔符号，如果是空格或tab键就可以省略了。<br />
&nbsp;&nbsp;&nbsp; <br />
user1<br />
user2<br />
user3&nbsp; </p>
<p>[root@mail awk]# awk '{if($5&gt;20) {print $1}}' example1.txt</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这行命令和上一行比较增加了&#8220;if($5&gt;20)&#8221;，得到的结果是</p>
<p>user3</p>
<p>这个if语句就没有必要更详细的解释了吧！就是第5列大于20的显示出满足条件的第一列</p>
<p>[root@mail awk]# awk '{if($5&gt;20 || $5==10) {print $1}}' example1.txt</p>
<p>user1<br />
user3</p>
<p>在来一个初级的又增加了一个&#8220;if($5&gt;20 || $5==10)&#8221;&nbsp;&nbsp;&nbsp; 做逻辑判断逻辑判断的三个&#8220;|| &amp;&amp; !&#8221; 或、与、非三个可以任意加到里面，这个语句的意思是如果第5列大于20或者等于10的都显示处理，在我们的工作中可能有用户会要求找出所有空间大于多少的或者是空间等于多少的账户然后再做批量修改。</p>
<p>if是awk循环中的一个还有其他很多，man awk可以看到，<br />
&nbsp;&nbsp; Control Statements<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The control statements are as follows:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (condition) statement [ else statement ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (condition) statement<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do statement while (condition)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (expr1; expr2; expr3) statement<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (var in array) statement<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete array[index]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete array<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit [ expression ]<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { statements }</p>
<p><br />
学习awk可以经常使用一下man awk 可以看到所有的函数和使用方法。&nbsp;&nbsp;&nbsp; </p>
<p>了解每个符号的意义我们才能更好的使用awk，最开始先记住几个命令知道他可实现的结果我们慢慢的再去理解。<br />
================================================================================</p>
<p>&nbsp;&nbsp;&nbsp; awk中级篇</p>
<p>这里顺便介绍一下vi的一个替换命令，现在我们要把example1.txt文本里的空格都替换为&#8220;:&#8221;冒号这里在vi里使用的命令就是：</p>
<p>%s/ /:/g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>这个命令对于使用vi的人来说是用得最多的。我们现在做了一个新的文件example2.txt。</p>
<p>user1:password1:username1:unit1:10<br />
user2:password2:username2:unit2:20<br />
user3:password3:username3:unit3:30</p>
<p>现在我们来做一个awk脚本，之前都是在命令行操作，实际上所有的操作在命令行上是可以都实现的，已我们最经常使用的批量添加用户来开始！script1.awk</p>
<p>#!/bin/awk -f&nbsp;&nbsp; # 当文件有可执行权限的时候你可以直接执行<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # ./script1.awk example2.txt<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # 如果没有以上这行可能会出现错误，或者<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # awk -f script1.awk example2.txt 参数f指脚本文件 </p>
<p>BEGIN {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # &#8220;BEGIN{&#8221;是awk脚本开始的地方<br />
&nbsp;&nbsp;&nbsp; FS=":";&nbsp;&nbsp;&nbsp;&nbsp; # FS 是在awk里指分割符的意思<br />
}</p>
<p>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # 接下来的&#8220;{&#8221; 是内容部分<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "add {";&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # 一下都是使用了一个awk函数print <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "uid=" $1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "userPassword=" $2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "domain=eyou.com" ;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "bookmark=1";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "voicemail=1";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "securemail=1"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "storage=" $5;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "}";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ".";<br />
}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # &#8220;}&#8221;&nbsp;&nbsp;&nbsp; 内容部分结束<br />
END {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # &#8220;END{&#8221; 结束部分<br />
&nbsp;&nbsp;&nbsp; print "exit";<br />
}</p>
<p>执行结果<br />
[root@mail awk]# awk -f script1.awk example2.txt<br />
add {<br />
uid=user1<br />
userPassword=password1<br />
domain=eyou.com<br />
bookmark=1<br />
voicemail=1<br />
securemail=1<br />
storage=10<br />
}<br />
.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
.<br />
.<br />
.<br />
.<br />
.<br />
exit</p>
<p>文本操作就是更方便一些。</p>
<p>下面给两个返回效果一样的例子<br />
[root@mail awk]# awk -F: '{print $1"@"$2}' example2.txt<br />
[root@mail awk]# awk -F: '{printf "%s@%s\n",$1,$2}' example2.txt</p>
<p>user1@password1</p>
<p>这里的区别是使用print 和printf的区别，printf格式更自由一些，我们可以更加自由的指定要输出的数据，print会自动在行尾给出空格，而printf是需要给定"\n"的，如果感兴趣你可以把&#8220;\n&#8221;去掉看一下结果。%s代表字符串%d 代表数字，基本上%s都可以处理了因为在文本里一切都可以看成是字符串，不像C语言等其他语言还要区分数字、字符、字符串等。</p>
<p><br />
文章出处：http://www.diybl.com/course/3_program/shell/shelljs/200862/119688.html</p>
<p>awk还有一些很好的函数细细研究一下还是很好用的。<br />
这次碰到了一个问题客户有一个用户列表,大概有2w用户，他有一个有趣的工作要做，就是把每个账户目录放到特定的目录下，例如13910011234这个目录要放到139/10/这个目录下，从这里可以看出规律是手机号码的前三位是二级目录名，手机的第3、4为是三级目录名，我们有的就只有一个用户列表，规律找到了我们现在开始想办法处理吧。</p>
<p>example3.txt</p>
<p>13910011234&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
15920312343<br />
13922342134<br />
15922334422<br />
......</p>
<p>第一步是要找到一个方法来吧，就是要把每一个手机号分开，最初可能你就会想到这个也没有任何间隔，我们怎么用awk分开他们呢？说实话最初我也考虑了20多分钟，后来想起原来学习python的时候有split函数可以分就想找找awk里是不是有类似的函数，man awk 发现substr 这个函数子串,</p>
<p>[root@mail awk]# awk '{print substr($1,1,3)}'&nbsp; example3.txt</p>
<p>[root@mail awk]# awk '{printf "%s/%s\n",substr($1,1,3),substr($1,4,2)}'&nbsp; example3.txt</p>
<p>[root@mail awk]# awk '{printf "mv %s %s/%s\n",$1,substr($1,1,3),substr($1,4,2)}'&nbsp; example3.txt</p>
<p>以上的两步的返回自己做一下，最后我们就得到了我们想要的结果。</p>
<p>mv 13910011234 139/10<br />
mv 15920312343 159/20<br />
mv 13922342134 139/22<br />
mv 15922334422 159/22</p>
<p>把这部分输出拷贝到一个shell脚本里，在数据当前目录下执行就可以了！</p>
<p>substr(s, i [, n])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns&nbsp; the at most n-character substring of s<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; starting at i.&nbsp; If n is omitted, the rest of&nbsp; s<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is used.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
substr函数解释，s代表我们要处理的字符串，i 是我们从这个字符串的第几个位置上开始，n 是我们从开始的位置取多少个字符。多看看man英文也会有所提高的。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>awk有很多有趣的函数如果感兴趣可以自己去查查看，<br />
man awk<br />
String Functions&nbsp; 字符串函数，举几个觉得常用的函数<br />
&nbsp;&nbsp;&nbsp; length([s])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns&nbsp; the&nbsp; length&nbsp; of&nbsp; the&nbsp; string s, or the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length of $0 if s is not supplied.<br />
&nbsp;&nbsp;&nbsp; length 你可以得到字符串的长度，这个是比较常用的一个函数&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; split(s, a [, r])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Splits the string s into the&nbsp; array&nbsp; a&nbsp; on&nbsp; the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; regular expression r, and returns the number of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fields.&nbsp; If r is omitted, FS is&nbsp; used&nbsp; instead.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The&nbsp;&nbsp; array&nbsp; a&nbsp; is&nbsp; cleared&nbsp; first.&nbsp;&nbsp; Splitting<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; behaves&nbsp;&nbsp; identically&nbsp;&nbsp; to&nbsp;&nbsp; field&nbsp;&nbsp; splitting,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; described above.&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tolower(str)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns&nbsp; a copy of the string str, with all the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; upper-case&nbsp; characters&nbsp; in&nbsp; str&nbsp; translated&nbsp; to<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; their&nbsp; corresponding&nbsp; lower-case&nbsp; counterparts.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Non-alphabetic characters are left unchanged.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; toupper(str)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Returns a copy of the string str, with all&nbsp; the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lower-case&nbsp; characters&nbsp; in&nbsp; str&nbsp; translated&nbsp; to<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; their&nbsp; corresponding&nbsp; upper-case&nbsp; counterparts.<br />
&nbsp; &amp;</p>
<p>nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Non-alphabetic characters are left unchanged.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Time Functions&nbsp; 时间函数，我们最最常用到的是时间戳转换函数<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
strftime([format [, timestamp]])<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Formats&nbsp; timestamp&nbsp; according to the specification in format.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The timestamp should be of the same form as returned by&nbsp; sys-<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time().&nbsp;&nbsp; If timestamp is missing, the current time of day is<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; used.&nbsp; If format is missing, a default format&nbsp; equivalent&nbsp; to<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; the output of date(1) is used.&nbsp; See the specification for the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strftime() function in ANSI C for the format conversions that<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; are&nbsp; guaranteed&nbsp; to be available.&nbsp; A public-domain version of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strftime(3) and a man page for it come&nbsp; with&nbsp; gawk;&nbsp; if&nbsp; that<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; version&nbsp; was&nbsp; used to build gawk, then all of the conversions<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; described in that man page are available to gawk.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
这里举例说明时间戳函数是如何使用的</p>
<p>[root@ent root]# date +%s | awk '{print strftime("%F %T",$0)}'<br />
2008-02-19 15:59:19&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>我们先使用date命令做一个时间戳，然后再把他转换为时间&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
还有一些我们现在可能不经常用到的函数，详细内容man awk 自己可以看一下。<br />
&nbsp;Bit Manipulations Functions&nbsp;&nbsp; 二进制函数<br />
&nbsp;Internationalization Functions&nbsp; 国际标准化函数<br />
&nbsp;<br />
&nbsp;USER-DEFINED FUNCTIONS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用户也可以自己定义自己的函数，感兴趣自己可以再深入研究一下。<br />
&nbsp;<br />
&nbsp;For example:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function&nbsp; f(p, q,&nbsp;&nbsp;&nbsp;&nbsp; a, b)&nbsp;&nbsp; # a and b are local<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /abc/&nbsp;&nbsp;&nbsp;&nbsp; { ... ; f(1, 2) ; ... }<br />
&nbsp;DYNAMICALLY LOADING NEW FUNCTIONS&nbsp; 动态加载新函数，这个可能就更高级一些了！<br />
&nbsp;================================================================================<br />
&nbsp;awk高级篇<br />
&nbsp;<br />
&nbsp;不管学习任何语言，我们学到的都是工具，工具知道的越多，我们做起工作来就越方便，但是工具在你的手里并不一定能造出好的产品，编辑脚本和编程序也是一样的重要的是算法，别人不知道怎么处理的问题你要知道如何处理。这才能证明你比别人更高，工具只要你慢慢练习都会使用。<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp; 下面给大家一个我认为是比较高级的问题了，感兴趣的可以自己再想想更好的解决办法。</p>
<p><br />
&nbsp;</p>
<p>问题是这样的我们有一个从ldap里导出的文件，它都是一行一个字段来说明的，每个用户的数据是已空行分割的。我们必须把对应的uid 和userPassword找出来而且是对应的。<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; 例子：example4.txt<br />
&nbsp;&nbsp;&nbsp; <br />
dn: uid=cailiying,domain=ccc.com.cn,o=mail.ccc.com.cn<br />
uid: cailiying<br />
userPassword:: e21kNX0zREl4VEIwODBJdXZkTnU3WFFtS3lRPT0=<br />
letters: 300<br />
quota: 100</p>
<p>dn: uid=caixiaoning,domain=ccc.com.cn,o=mail.ccc.com.cn<br />
userPassword:: e21kNX1kejFXU0doZWprR2RNYnV5ajJJRWl3PT0=<br />
letters: 300<br />
quota: 100<br />
uid: chenzheng<br />
domain: cqc.com.cn</p>
<p>dn: uid=caixiaoning,domain=ccc.com.cn,o=mail.ccc.com.cn<br />
userPassword:: e21kNX1kejFXU0doZWprR2RNYnV5ajJJRWl3PT0=<br />
letters: 300<br />
quota: 100</p>
<p>dn: uid=caixiaoning,domain=ccc.com.cn,o=mail.ccc.com.cn<br />
userPassword:: e21kNX1kejFXU0doZWprR2RNYnV5ajJJRWl3PT0=<br />
letters: 300<br />
quota: 100<br />
uid: chenzheng<br />
domain: cqc.com.cn<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 处理这个文本我们需要考虑的问题是：<br />
1 uid&nbsp; 和userPassword 并不是每一个段落里都有<br />
2 在每一段里面uid和userPassword 先后顺序是随机的<br />
3 有的段落里可能只有uid 或者只有userPassword </p>
<p>从文本上分析可以看出必须使用的间隔符号，一个是空行，一个是冒号。<br />
冒号我们awk -F:就可以了，不过空行我们不好判断现在想到length()这个函数，在unix里空行最多只有一个\n字符，如果一行字符数小于2我们判断为空行，好现在间隔符号问题解决，空行只能通过循环来实现对空行的判断。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>现在碰到的另外一个问题是我们的某个段里的信息是不完全的，我们就要放弃这段这儿如何来做，就是要做两个标记变量u 和 p&nbsp; 再做一个循环如果u&nbsp; 和&nbsp; p 同事满足我们才输出结果下面的awk脚本就是通过这个思考来解决ldif文本的处理的！</p>
<p># 此脚本的目的是方便我们以后导ldap的其他邮件的数据，<br />
# 我们之前使用slapdcat -l 导出所有信息，然后我们需要<br />
# 整理出uid&nbsp; password , 这里的设置都是默认以":" 间隔的<br />
# 例slapcat -l user.ldif&nbsp; 如果想得到一份uid 和userPassword 对应的文件，<br />
# 修改username = "dn"; password = "userpassword"; awk -f ldap2txt.awk user.ldif | grep uid | more&nbsp; 可以查看结果 (有可能是多域的邮件)<br />
# 如果想得到domain 所对应的密码，修改username = "dn"; password = "userpassword";&nbsp; 运行 awk -f ldap2txt.awk user.ldif |grep domain | more </p>
<p><br />
#!/bin/awk -f <br />
# File name: ldap2txt.awk </p>
<p>BEGIN {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FS = ":";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; username = "uid";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; password = "userPassword";<br />
}</p>
<p>{</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(length($0) == 0 ) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (name != "u"&nbsp; &amp;&amp;&nbsp; pword != "p")<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf ("%s:%s\n", name,pword); <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = "u";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pword = "p";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($1 == username)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = "u";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = $0; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if($1 == password)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pword = "p";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pword = $0;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
}<br />
END {</p>
<p>}</p>
<p>实际上对于学习语言首先是熟悉一些常用的函数，然后就是试着去解决别人解决过的问题，然后自己再思考一下是不是有更好，速度更快的解决办法，实际上大部分的程序员都是在重复的使用着别人好的解决办法，把别人的方法转变为自己的方法，就是反复练习解决不同的问题，思考更好的方法！</p>
<p><br />
文章出处：http://www.diybl.com/course/3_program/shell/shelljs/200862/119688_4.html</p>
 <img src ="http://www.blogjava.net/lingy/aggbug/271482.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-19 10:43 <a href="http://www.blogjava.net/lingy/archive/2009/05/19/271482.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux Grep命令使用的详细介绍</title><link>http://www.blogjava.net/lingy/archive/2009/05/19/271481.html</link><dc:creator>林光炎</dc:creator><author>林光炎</author><pubDate>Tue, 19 May 2009 02:38:00 GMT</pubDate><guid>http://www.blogjava.net/lingy/archive/2009/05/19/271481.html</guid><description><![CDATA[<p>1. grep简介 <br />
　　grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具，它能使用正则表达式搜索文本，并把匹配的行打印出来。Unix的grep家族包括grep、 egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的扩展，支持更多的re元字符， fgrep就是 fixed grep或fast grep，它们把所有的字母都看作单词，也就是说，正则表达式中的元字符表示回其自身的字面意义，不再特殊。linux 使用GNU版本的grep。它功能更强，可以通过-G、-E、-F命令行选项来使用egrep和fgrep的功能。</p>
<p>　　grep的工作方式是这样的，它在一个或多个文件中搜索字符串模板。如果模板包括空格，则必须被引用，模板后的所有字符串被看作文件名。搜索的结果被送到屏幕，不影响原文件内容。</p>
<p>　　grep可用于shell脚本，因为grep通过返回一个状态值来说明搜索的状态，如果模板搜索成功，则返回0，如果搜索不成功，则返回1，如果搜索的文件不存在，则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。</p>
<p>　　2. grep正则表达式元字符集(基本集)</p>
<p>　　^</p>
<p>　　锚定行的开始 如：'^grep'匹配所有以grep开头的行。</p>
<p>　　$</p>
<p>　　锚定行的结束 如：'grep$'匹配所有以grep结尾的行。</p>
<p>　　匹配一个非换行符的字符 如：'gr.p'匹配gr后接一个任意字符，然后是p。</p>
<p>　　*</p>
<p>　　匹配零个或多个先前字符 如：'*grep'匹配所有一个或多个空格后紧跟grep的行。 .*一起用代表任意字符。</p>
<p>　　[]</p>
<p>　　匹配一个指定范围内的字符，如'[Gg]rep'匹配Grep和grep。</p>
<p>　　[^]</p>
<p>　　匹配一个不在指定范围内的字符，如：'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头，紧跟rep的行。</p>
<p>　　\(..\)</p>
<p>　　标记匹配字符，如'\(love\)'，love被标记为1。</p>
<p>　　\&lt;</p>
<p>　　锚定单词的开始，如:'\\&gt;</p>
<p>　　锚定单词的结束，如'grep\&gt;'匹配包含以grep结尾的单词的行。</p>
<p>　　x\{m\}</p>
<p>　　重复字符x，m次，如：'0\{5\}'匹配包含5个o的行。</p>
<p>　　x\{m,\}</p>
<p>　　重复字符x,至少m次，如：'o\{5,\}'匹配至少有5个o的行。</p>
<p>　　x\{m,n\}</p>
<p>　　重复字符x，至少m次，不多于n次，如：'o\{5,10\}'匹配5--10个o的行。</p>
<p>　　\w</p>
<p>　　匹配文字和数字字符，也就是[A-Za-z0-9]，如：'G\w*p'匹配以G后跟零个或多个文字或数字字符，然后是p。</p>
<p>　　\W</p>
<p>　　\w的反置形式，匹配一个或多个非单词字符，如点号句号等。</p>
<p>　　\b</p>
<p>　　单词锁定符，如: '\bgrepb\'只匹配grep。</p>
<p>　　3. 用于egrep和 grep -E的元字符扩展集</p>
<p>　　+</p>
<p>　　匹配一个或多个先前的字符。如：'[a-z]+able'，匹配一个或多个小写字母后跟able的串，如loveable,enable,disable等。</p>
<p>　　?</p>
<p>　　匹配零个或多个先前的字符。如：'gr?p'匹配gr后跟一个或没有字符，然后是p的行。</p>
<p>　　a|b|c</p>
<p>　　匹配a或b或c。如：grep|sed匹配grep或sed</p>
<p>　　()</p>
<p>　　分组符号，如：love(able|rs)ov+匹配loveable或lovers，匹配一个或多个ov。</p>
<p>　　x{m},x{m,},x{m,n}</p>
<p>　　作用同x\{m\},x\{m,\},x\{m,n\}</p>
<p><br />
文章出处：DIY部落(http://www.diybl.com/course/3_program/shell/shelljs/20090216/155758.html)</p>
 <img src ="http://www.blogjava.net/lingy/aggbug/271481.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/lingy/" target="_blank">林光炎</a> 2009-05-19 10:38 <a href="http://www.blogjava.net/lingy/archive/2009/05/19/271481.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>