<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>可可熊的窝 &#187; Shell</title>
	<atom:link href="http://cocobear.info/blog/category/my-programming/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://cocobear.info/blog</link>
	<description>K.I.S.S</description>
	<lastBuildDate>Sat, 10 Dec 2011 16:11:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>shell脚本解题6</title>
		<link>http://cocobear.info/blog/2008/08/19/07-issues-shell/</link>
		<comments>http://cocobear.info/blog/2008/08/19/07-issues-shell/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 07:42:19 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=297</guid>
		<description><![CDATA[问题： 执行命令前暂时回home目录，执行后跳回来 解法： 1. (cd ~; ls; ) 使用子shell的方式去执行命令； 把这行 gs.AddMany( [b_cls,b_bck,t_blank,b_close,b_7,b_8,b_9,b_div,b_4,b_5,b_6,b_mul,b_1,b_2,b_3,b_minus,b_0,b_dot,b_equal,b_plus, ]) 进行替换，每一个元素替换为类似(b_cls,0,EXPAND)这样 解法1. sed 's/[b&#124;t]_[^,]*,/(&#38;0,EXPAND)/g' 如果使用 's/[b&#124;t]_.*?,/(&#38;0,EXPAND)/g' 则不能得出正确的结果，因为sed不支持.*?这样的非贪婪匹配；解法中使用[^,]避免了贪婪匹配。 有如下内容,想要获取 8B E5 55 这些内容： [cocobear@cocobear ~]$ cat file.txt 00401038 8B E5 mov esp,ebp 0040103A 55 push ebp 0040103B 8B EC mov ebp,esp 0040103D 33 FF xor edi,edi 0040103F 57 push edi 004010A9 FF D0 [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/08/19/07-issues-shell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>shell脚本解题5</title>
		<link>http://cocobear.info/blog/2008/07/24/06-issues-shell/</link>
		<comments>http://cocobear.info/blog/2008/07/24/06-issues-shell/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 09:37:36 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>
		<category><![CDATA[awk]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=295</guid>
		<description><![CDATA[问题： 随机输出目录下5个文件： 解法： 1. #! /bin/bash arr=($(ls *.sql.gz)) len=$((${#arr[@]}+1)) for((i=1;i&#38;lt;6;i++)) do RANDOM=$(($$+i)) echo ${arr[$((RANDOM%len))]} done 这种方法有可能会产生重复的文件。 2. ls * 'BEGIN {srand()} {A[NR]=$0;} END {for(i=0;i&#38;lt;5;++i) { n=int(rand()*FNR)+1; print A[n]} }' 同样这个也有可能重复。 3. ls *sql.gz &#124; awk 'BEGIN{srand()}{a[rand()&#34; &#34;NR]=$0}END{for(i in a) {print a[i];if(++j==5) break}}' 将600个同一目录下的文件随机分成3份，每份200个： 解法： 1. ls &#124; awk 'BEGIN{srand();b[1]=&#34;path1&#34;;b[2]=&#34;path2&#34;;b[3]=&#34;path3&#34;}{a[rand()&#34; &#34;NR]=$0}END{for(i in a){if(k++%200==0) j++;print &#34;mv &#34;a[i]&#34; [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/24/06-issues-shell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>shell脚本解题4</title>
		<link>http://cocobear.info/blog/2008/07/18/05-issues-shell/</link>
		<comments>http://cocobear.info/blog/2008/07/18/05-issues-shell/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 08:24:32 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=294</guid>
		<description><![CDATA[问题： 用sed把每行的第二个字符到第六个字符替换成星号 原文 123456 1234567 12345 要求结果 1***** 1*****7 1**** 解法： 1. sed '/^../{h;s/^.\(.\{1,5\}\).*/\1/;s/./*/g;G;s/\(.*\)\n\(.\).\{1,5\}\(.*\)/\2\1\3/}' 2. sed -r '/.{6}/bb :a s/(.\**)[^*]/\1\*/ ta b :b /^.\*{5}/! { s/(.\**)[^*]/\1\*/ tb }' 3. awk '{a=(substr($0,2,5));gsub(/./,&#34;*&#34;,a);print substr($0,1,1)a&#34;&#34;substr($0,7,length($0))}' 4. sed 's/^\(.\)\(.\{1,5\}\)\(.*\)/\1\n\2\n\3/' file &#124; sed '{n; s/./*/g;n}' &#124; sed '{N; N; s/\n//g}' 5. sed ':a;/^.[*]\{5\}/!{s/\(.[*]*\)./\1*/;/*$/!ta}' 问题： 比较2008-01-29和2008-02-1之间相差月份 解法： 1. echo &#34;$date1-$date2&#34; &#124; [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/18/05-issues-shell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>shell脚本解题3</title>
		<link>http://cocobear.info/blog/2008/07/16/04-issues-shell/</link>
		<comments>http://cocobear.info/blog/2008/07/16/04-issues-shell/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 05:15:42 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=291</guid>
		<description><![CDATA[问题： 如何将命令的输出信息按行放入到数组里面? 解法： 1. n=0 while read line;do array[$n]=&#34;$line&#34; ((n++)) done &#60; &#60;(traceroute 192.168.1.1 -n) echo ${array[0]} 问题： sed 可以同时匹配多个条件？ 比如 file: AAA BBB CCC DDD AAA BBB DDD CCC DDD sed能实现，同时匹配AAA和CCC就打印,在一条命令中 解法： 1. sed -n '/AAA/{/CCC/p}' 同时匹配kobe和james: sed -n &#8216;/kobe/{/james/p}&#8217; awk &#8216;/kobe/&#038;&#038;/james/{ print $0 }&#8217; 匹配kobe或james: sed -n &#8216;/\(kobe\&#124;james\)/p&#8217; awk &#8216;/kobe/&#124;&#124;/james/{ print $0 }&#8217; [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/16/04-issues-shell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>shell脚本解题2</title>
		<link>http://cocobear.info/blog/2008/07/15/03-issues-shell/</link>
		<comments>http://cocobear.info/blog/2008/07/15/03-issues-shell/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 09:12:33 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=292</guid>
		<description><![CDATA[问题： 如下文本 60.208.0.224 60.208.103.192 131.9.124.72 60.208.20.119 60.208.20.9 2.110.213.40 8.56.32.24 60.53.37.128 60.3.98.1 怎么先按ip的第一位sort，然后按第二位sort，然后按第三，第四 结果是这样 2.110.213.40 8.56.32.24 60.3.98.1 60.53.37.128 60.208.0.224 60.208.20.9 60.208.20.119 60.208.103.192 131.9.124.72 解法： 1. sort -t. -k1,1n -k2,2n -k3,3n -k4,4n 2. sed 's/\./ /g' urfile&#124;sort -nt ' ' -k1 -k2 -k3 -k4&#124;sed 's/ /./g' 问题： httpd.conf文件中含有以下内容，要把Directory块中含有/usr/local/apache/cgi-bin这一块注释掉： Options Indexes MultiViews FollowSymLinks AllowOverride None Order allow,deny Allow [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/15/03-issues-shell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>sed的几个选项</title>
		<link>http://cocobear.info/blog/2008/07/11/options-of-sed/</link>
		<comments>http://cocobear.info/blog/2008/07/11/options-of-sed/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 09:07:24 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=290</guid>
		<description><![CDATA[sed中有些选项不太好理解,今天就顺一顺: 几个命令: n 把下一行输入读入模式空间中.当前行被发送到标准输出,下一行将成为当前行.控制将被转移给n后面所跟的命令,而不是回到脚本开始处. N 把下一行输入行添加到模式空间的内容之后;这两行文本用一个插入的换行符分隔开. p 打印出寻址到的行.除非使用命令行选项-n,否则该命令会导致输出相同的行. 这里有一段对D的解释 函数参数 D 表示删除 pattern space 内的第一行资料。其指令格式如下: [address1,address2]D 对上述格式有下面几点说明 : 函数参数 D 最多配合两个位址参数。 函数参数 D 与 d 的比较如下 : 当 pattern space 内只有一资料行时 , D 与 d 作用相同。 当 pattern space 内有多行资料行时 D 表示只删除 pattern space 内第一行资料 ; d 则全删除。 D 表示执行删除後 , pattern space 内不添加下一笔资料 , [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/11/options-of-sed/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>shell脚本解题1</title>
		<link>http://cocobear.info/blog/2008/07/10/02-issues-shell/</link>
		<comments>http://cocobear.info/blog/2008/07/10/02-issues-shell/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 05:28:24 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=289</guid>
		<description><![CDATA[问题：请教如何得到一个目录树的最深目录路径？ 解法： 1. find -type d &#124; awk '{if( max &#60; gsub(/\//,&#34;/&#34;)) {max= gsub(/\//,&#34;/&#34;);maxp=$0&#34;/&#34;} else if (max == gsub(/\//,&#34;/&#34;)) maxp=maxp&#34;\n&#34;$0&#34;/&#34;} END{print maxp}' 2. find -type d -printf &#34;%d %p\n&#34; &#124; sort -nrk1 &#124; awk 'NR==1{a=$1}a==$1{print $2}' 问题：我想将包含read的行放在echo单词的后面 解法： sed '/read/{h;d};/echo/{G;s/\(.*echo\)\(.*\)\n\(.*\)/\1\3\2/}' urfile 问题：怎么copy一个目录中的某些文件，但保持目录结构？ 比如说，我想copy sourcedir\a\b\c.java sourcedir\a\b\c.cs sourcedir\a\b\d.java sourcedir\a\b\d.cs 到 destdir\a\b\c.java destdir\a\b\d.java 解法: 1. rsync -av --include='*.java' [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/10/02-issues-shell/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>shell脚本解题</title>
		<link>http://cocobear.info/blog/2008/07/07/01-issue-shell/</link>
		<comments>http://cocobear.info/blog/2008/07/07/01-issue-shell/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 02:27:55 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/?p=287</guid>
		<description><![CDATA[以下内容来自http://bbs.chinaunix.net/thread-1189933-1-1.html 现有数据，每一行中各个段间的分隔为多个空格，如下 　　AAB BB CCC A2B CB ABC 如何实现，转换成下面的格式 AAB&#124;BB&#124;CCC&#124; A2B&#124;CB&#124;ABC&#124; awk解法： 1. [cocobear@cocobear ~]$ awk &#8216;BEGIN{OFS=&#8221;&#124;&#8221;} {$1=$1;print}&#8217; test.txt AAB&#124;BB&#124;CCC A2B&#124;CB&#124;ABC 结果最后少一个&#8221;&#124;&#8221; 2. [cocobear@cocobear ~]$ awk &#8216;{print $1&#8243;&#124;&#8221;$2&#8243;&#124;&#8221;$3&#8243;&#124;&#8221;}&#8217; test.txt AAB&#124;BB&#124;CCC&#124; A2B&#124;CB&#124;ABC&#124; 3. [cocobear@cocobear ~]$ awk -v OFS=&#8217;&#124;&#8217; &#8216;NF++&#8217; test.txt AAB&#124;BB&#124;CCC&#124; A2B&#124;CB&#124;ABC&#124; sed解法： 1. [cocobear@cocobear ~]$ sed &#8216;s/^ \+//;s/ \+/&#124;/g;s/ *$/&#124;/;&#8217; test.txt AAB&#124;BB&#124;CCC&#124; A2B&#124;CB&#124;ABC&#124; 2. [cocobear@cocobear [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2008/07/07/01-issue-shell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>诡异的shell脚本错误</title>
		<link>http://cocobear.info/blog/2007/12/21/erorr-of-shell-script/</link>
		<comments>http://cocobear.info/blog/2007/12/21/erorr-of-shell-script/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 14:32:37 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/2007/12/21/%e8%af%a1%e5%bc%82%e7%9a%84shell%e8%84%9a%e6%9c%ac%e9%94%99%e8%af%af/</guid>
		<description><![CDATA[今天为了方便拨号，改了改以前写的拨号脚本，没想到遇到了一个问题：使用awk分析一个文本文件时总会在我截取的第二个字段后多加一个^M（也就是r）。很自然的想到vim打开这个文本文件确认这个文件原本是否有没有“^M”,很奇怪vim里面这个文件行末没有这个东西。又和王聪在一起研究了半天我写的这个awk脚本，还试着用head+tail+cut把脚本重写了一遍，结果问题仍然存在。这时我不得不怀疑这个文本文件是不是有问题，不放心刚才看过一遍，又打开看了一次，仍然没有发现任何异常，在我们束手无策的时候突然注意到了在打开这个文本文件时vim左下脚闪了一下&#8221;dos&#8221;这几个字符，这时我们才恍然大悟，原来是眼睛欺骗了我们，vim把这个文本文件按照dos(windows)下文本进行了显示，所以^M是合法的换行，没有被显示出来。 眼睛看到的东西未必是可信的！]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2007/12/21/erorr-of-shell-script/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>目录下文件名大小写转换</title>
		<link>http://cocobear.info/blog/2007/11/04/%e7%9b%ae%e5%bd%95%e4%b8%8b%e6%96%87%e4%bb%b6%e5%90%8d%e5%a4%a7%e5%b0%8f%e5%86%99%e8%bd%ac%e6%8d%a2/</link>
		<comments>http://cocobear.info/blog/2007/11/04/%e7%9b%ae%e5%bd%95%e4%b8%8b%e6%96%87%e4%bb%b6%e5%90%8d%e5%a4%a7%e5%b0%8f%e5%86%99%e8%bd%ac%e6%8d%a2/#comments</comments>
		<pubDate>Sun, 04 Nov 2007 14:35:37 +0000</pubDate>
		<dc:creator>可可熊</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[编程相关]]></category>

		<guid isPermaLink="false">http://cocobear.info/blog/2007/11/04/%e7%9b%ae%e5%bd%95%e4%b8%8b%e6%96%87%e4%bb%b6%e5%90%8d%e5%a4%a7%e5%b0%8f%e5%86%99%e8%bd%ac%e6%8d%a2/</guid>
		<description><![CDATA[for f in *;do mv $f `echo $f &#124;tr &#8220;[A-Z]&#8221; &#8220;[a-z]&#8220;` done 上面这个脚本可以处理大多数情况，但是当文件名中有空格时该脚本就无效了。 经过尝试下面这段脚本可以解决上面的问题： [cocobear@cocobear 1027]$ cat re.sh mv &#8220;$1&#8243; &#8220;`echo $1 &#124; tr &#8216;[A-Z]&#8216; &#8216;[a-z]&#8216;`&#8221; [cocobear@cocobear 1027]$ find ./ -type f -exec ./re.sh &#8216;{}&#8217; \; 再想一想其实最前面的代码也一样能解决问题，也是需要在mv的两个参数的变量名上加上双引号： for f in *;do mv &#8220;$f&#8221; &#8220;`echo $f &#124;tr &#8220;[A-Z]&#8221; &#8220;[a-z]&#8220;`&#8221; done 我刚开始改的时候只对`echo $f &#124; tr &#8220;[A-Z]&#8221; &#8220;[a-z]&#8220;`用了双引号，结果出现下面的错误： [...]]]></description>
		<wfw:commentRss>http://cocobear.info/blog/2007/11/04/%e7%9b%ae%e5%bd%95%e4%b8%8b%e6%96%87%e4%bb%b6%e5%90%8d%e5%a4%a7%e5%b0%8f%e5%86%99%e8%bd%ac%e6%8d%a2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

