??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久亚洲精品中文字幕,欧美午夜精品久久久久久浪潮,区久久AAA片69亚洲 http://www.shnenglu.com/prayer/category/7884.html在一般中L卓越zh-cnThu, 04 Apr 2019 13:20:02 GMTThu, 04 Apr 2019 13:20:02 GMT60sed -i命o详解http://www.shnenglu.com/prayer/archive/2019/04/04/216337.htmlPrayerPrayerThu, 04 Apr 2019 09:49:00 GMThttp://www.shnenglu.com/prayer/archive/2019/04/04/216337.htmlhttp://www.shnenglu.com/prayer/comments/216337.htmlhttp://www.shnenglu.com/prayer/archive/2019/04/04/216337.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/216337.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/216337.htmlhttps://www.cnblogs.com/ev-zhk/p/4277023.html
[root@www ~]# sed [-nefr] [动作] 选项与参敎ͼ -n Q用安?silent)模式。在一?sed 的用法中Q所有来?STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经qsed Ҏ处理的那一?或者动?才会被列出来?-e Q直接在命o列模式上q行 sed 的动作编辑; -f Q直接将 sed 的动作写在一个文件内Q?-f filename 则可以运?filename 内的 sed 动作Q?-r Q?span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">sed 的动作支持的是g伸型正规表示法的语法?默认是基正规表示法语? -i Q直接修改读取的文g内容Q而不是输出到l端? 动作说明Q?[n1[,n2]]function n1, n2 Q不见得会存在,一般代表『选择q行动作的行数』,举例来说Q如果我的动作是需要在 10 ?20 行之间进行的Q则?10,20[动作行ؓ] ? functionQ?a Q新增, a 的后面可以接字串Q而这些字串会在新的一行出?目前的下一??c Q取代, c 的后面可以接字串Q这些字串可以取?n1,n2 之间的行Q?d Q删除,因ؓ是删除啊Q所?d 后面通常不接M咚咚Q?i Q插入, i 的后面可以接字串Q而这些字串会在新的一行出?目前的上一?Q?p Q列华ͼ亦即某个选择的数据印出。通常 p 会与参数 sed -n 一赯行~ s Q取代,可以直接q行取代的工作哩Q通常q个 s 的动作可以搭配正规表C法Q例?1,20s/old/new/g 是啦!
复制代码


sed -i 是直接Ҏ本文件进行操作的

sed -i 's/原字W串/新字W串/' /home/1.txt sed -i 's/原字W串/新字W串/g' /home/1.txt


q两条命令的区别是Q看CZ?/p>

q是1.txt的内?/p>

#cat 1.txt d ddd #ff

再看执行q两条命令的区别?/p>

复制代码
sed -i 's/d/7523/' /home/1.txt 执行l果 7523 7523dd #ff  sed -i 's/d/7523/g' /home/1.txt 执行l果 7523 752375237523 #ff
复制代码

 

L “行首” ?#8220;@”的首字母@

sed -i 's/^@//' file

 

特定字符串的行前插入新行

sed -i '/特定字符?i 新行字符?/span>' file

 

特定字符串的行后插入新行

sed -i '/特定字符?a 新行字符?/span>' file

 

特定字符串的删除

sed -i '/字符?d' file

 



Prayer 2019-04-04 17:49 发表评论
]]>
sed中用变量替换以及执行外部命?/title><link>http://www.shnenglu.com/prayer/archive/2019/04/04/216336.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 04 Apr 2019 09:46:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2019/04/04/216336.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/216336.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2019/04/04/216336.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/216336.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/216336.html</trackback:ping><description><![CDATA[<div>https://blog.csdn.net/halazi100/article/details/41722021<br /><br /><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">在用sedҎ志或者其它文本进行parse的过E当中,有时候我们需要引用外部变量的|<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />或者获取一个shell命o执行的结果,以便辑ֈ更加可观的输出结果。这里介l如何做到?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />sed中用变量替?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />1.sed命o使用双引L情况下,使用$var直接引用<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed "s/^/$RANDOM.rmvb_/g"<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 29328.rmvb_</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">如果替换的变量内容中含有/W号则会提示如下错误Q如<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />查找当前目录下的目录文gq将相对路径替换为绝对\?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />find . -type f | sed -n "s/\./$PWD/p"<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />sed: -e expression #1, char 19: unknown option to `s'<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />从语法上看,没有M问题Q但׃变量中包含有“/”作ؓ分隔W,q会和sed的替换操作的分隔W?#8220;/”引vhQ所以,只要不?#8220;/”做分隔符可以解册个问题,如果使用“%”而不?#8220;/”来作为sed的替换操作的分隔W,׃会出错。其实???作ؓ分隔W也是可以的Q只要不会与替换中有相同的而且不是元字W的ҎW号都是可以的;使用时可以根据情늁z选择?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />find . -type f | sed -n "s%\.%$PWD%p"<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />find . -type f | sed -n "s#\.#$PWD#p"</p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"># 上面例子引用了一个环境变?RANDOM的?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />2.sed命o使用单引L情况下,使用'"$var"'引用<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />cMQ我们可以看?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed 's/^/'"$RANDOM"'.rmvb_/g'<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 31338.rmvb_<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />sed中执行外部命?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />1.sed命o使用单引L情况下?`shell command`'或?$(shell command)'引用命o执行的结?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />q是以上面案例分析,例子如下<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed 's/^/'`echo $RANDOM`'.rmvb_/g'<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 8063.rmvb_<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> </p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"># 上面的例子用了旧式的命令替换,也可以采用新式的命o替换ҎQ如?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed 's/^/'$(echo $RANDOM)'.rmvb_/g'<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 18554.rmvb_<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> </p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"># 下面例子取用当前日期作ؓl果的一部分Q如?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed 's/^/'$(date +"%Y%m%d")'.rmvb_/g'<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 20120108.rmvb_<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> </p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">2.sed命o使用双引L情况下直接`shell command`或?(shell command)引用命o执行的结?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> </p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;">cM的,双引L情况Q如?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed "s/^/$(date +"%Y%m%d").rmvb_/g"<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 20120108.rmvb_<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> </p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; font-size: 16px; color: #4f4f4f; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"># 使用环境变量$RANDOM以及旧式命o替换的例?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />$ echo|sed "s/^/`echo $RANDOM`.rmvb_/g"<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 4988.rmvb_<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />ȝ<br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> <br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" />在sed语句里面Q变量替换或者执行shell命oQ双引号比单引号绕一些弯?/p></div><img src ="http://www.shnenglu.com/prayer/aggbug/216336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2019-04-04 17:46 <a href="http://www.shnenglu.com/prayer/archive/2019/04/04/216336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux 比较两个文g夹不?(diff命o, md5列表)http://www.shnenglu.com/prayer/archive/2019/04/04/216335.htmlPrayerPrayerThu, 04 Apr 2019 09:46:00 GMThttp://www.shnenglu.com/prayer/archive/2019/04/04/216335.htmlhttp://www.shnenglu.com/prayer/comments/216335.htmlhttp://www.shnenglu.com/prayer/archive/2019/04/04/216335.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/216335.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/216335.html比较文g夹diffQ可以直接用diff命o

[root@~]# diff -urNa dir1 dir2
  -a Treat all files as text and compare them line-by-line, even if they do not seem to be text.
  -N, --new-file
    In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
  -r When comparing directories, recursively compare any subdirectories found.

  -u Use the unified output format.

 

比较文g夹diffQ也可以比较文gMD5列表。下面命令可以获取文件夹中文件md5列表

find /home/ -type f -not \( -name '.*' \) -exec md5sum {} \;

说明Q?1) /home/文g目录

     (2) -type f 文gcd为普通文?/p>

     (3) -not \( -name '.*' \)  qo掉隐藏文件。可以过滤掉不需要考虑的文?/p>

         (4) -exec md5sum {} \;  Ҏ个文件执行md5sum命o 

 

 用tar命o压羃后,比较文g的MD5是不行的。tar压羃会带上文件的旉



Prayer 2019-04-04 17:46 发表评论
]]>
SHELL 在指定行的前/后插入指定内?/title><link>http://www.shnenglu.com/prayer/archive/2019/04/04/216334.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 04 Apr 2019 09:13:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2019/04/04/216334.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/216334.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2019/04/04/216334.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/216334.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/216334.html</trackback:ping><description><![CDATA[<div>https://www.jianshu.com/p/66f79ad53406<br /><br /><h3>如果知道行号可以用下面的Ҏ</h3><pre bash"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">sed -i <span style="box-sizing: border-box; color: #98c379;">'88 r b.file'</span> a.file <span style="box-sizing: border-box; color: #929292;">#在a.txt的第88行插入文件b.txt</span> awk <span style="box-sizing: border-box; color: #98c379;">'1;NR==88{system("cat b.file")}'</span> a.file > a.file </code></pre><h3>如果不知道行P可以用正則匹?/h3><pre bash"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">sed -i <span style="box-sizing: border-box; color: #98c379;">'/regex/ r b.txt'</span> a.txt <span style="box-sizing: border-box; color: #929292;"># regex是正则表辑ּ</span> awk <span style="box-sizing: border-box; color: #98c379;">'/target/{system("cat b.file")}'</span> a.file > c.file </code></pre><h3>sed的話如果不改变源文gQ可以去?i开养I修改会输出到STDOUT</h3><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">原文Ӟ</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># cat -n file </span> <span style="box-sizing: border-box; color: #d19a66;">1</span> aaaa <span style="box-sizing: border-box; color: #d19a66;">2</span> bbbb <span style="box-sizing: border-box; color: #d19a66;">3</span> cccc <span style="box-sizing: border-box; color: #d19a66;">4</span> dddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">现在要在W二行即“bbbb”行的下面d一行,内容?#8220;xiaowu”</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed '/bbbb/a\xiaowu' file </span> aaaa bbbb xiaowu cccc dddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">如果要加两行“xiaowu”可以用一下语句,注意?#8220;\n”换行</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed '/bbbb/a\xiaowu\nxiaowu' file </span> aaaa bbbb xiaowu xiaowu cccc dddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">如果要在W二行即“bbbb”行的上添加一行,内容?#8220;xiaowu”Q可以把参数“a”换成“i”</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed '/b/i\xiaowu' file </span> aaaa xiaowu bbbb cccc dddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">以上文g中只有一行匹配,如果文g中有两行或者多行匹配,l果有是如何呢?</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># cat -n file </span> <span style="box-sizing: border-box; color: #d19a66;">1</span> aaaa <span style="box-sizing: border-box; color: #d19a66;">2</span> bbbb <span style="box-sizing: border-box; color: #d19a66;">3</span> cccc <span style="box-sizing: border-box; color: #d19a66;">4</span> bbbb <span style="box-sizing: border-box; color: #d19a66;">5</span> dddd </code></pre><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed '/bbbb/a\xiaowu' file </span> aaaa bbbb xiaowu cccc bbbb xiaowu dddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">q果可知,每个匚w行的下一行都会被d“xiaowu”</p><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">那么如果指向在第二个“bbbb”的下一行添加内?#8220;xiaowu”Q该如何操作呢?<br style="box-sizing: border-box;" />可以考虑先获取第二个“bbbb”行的行号Q然后根据行号在此行的下一行添?#8220;xiaowu”</p><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">获取W二?#8220;bbbb”行的行号的方法:<br style="box-sizing: border-box;" />Ҏ一Q?/p><pre objectivec"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #61aeee;"># cat -n file |grep b |awk '{print $1}'|sed -n <span style="box-sizing: border-box; color: #98c379;">"2"</span>p</span> <span style="box-sizing: border-box; color: #d19a66;">4</span> </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">Ҏ二:</p><pre objectivec"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #61aeee;"># sed -n '/bbbb/=' file |sed -n <span style="box-sizing: border-box; color: #98c379;">"2"</span>p</span> <span style="box-sizing: border-box; color: #d19a66;">4</span> </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">q果可知第二个“bbbb”行的行号?Q然后再在第四行的前或后d相应的内?</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed -e '4a\xiaowu' file </span> aaaa bbbb cccc bbbb xiaowu dddd </code></pre><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed -e '4a\xiaowu\nxiaowu' file </span> aaaa bbbb cccc bbbb xiaowu xiaowu dddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;">向指定行的末添加指定内容,比如?#8220;ccccc”行的行尾介绍“ eeeee”</p><pre ruby"="" style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 15px; margin-top: 0px; margin-bottom: 20px; line-height: 1.42857; word-wrap: normal; color: #abb2bf; background-color: #282c34; border: 1px solid #cccccc; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; word-break: break-word !important;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; color: inherit; background-color: transparent; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; padding: 0px; border: none; vertical-align: middle;">[root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># cat file</span> aaaaa bbbbb ccccc ddddd [root@xiaowu shell]<span style="box-sizing: border-box; color: #929292;"># sed 's/cc.*/& eeeee/g' file</span> aaaaa bbbbb ccccc eeeee ddddd </code></pre><p style="box-sizing: border-box; margin: 0px 0px 25px; color: #2f2f2f; font-family: -apple-system, 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27px; background-color: #ffffff; word-break: break-word !important;"><a target="_blank" rel="nofollow" style="box-sizing: border-box; background-color: transparent; color: #3194d0; text-decoration: none; cursor: pointer;">原文链接</a></p></div><img src ="http://www.shnenglu.com/prayer/aggbug/216334.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2019-04-04 17:13 <a href="http://www.shnenglu.com/prayer/archive/2019/04/04/216334.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux Sftp 下蝲整个文g目录http://www.shnenglu.com/prayer/archive/2019/03/18/216302.htmlPrayerPrayerMon, 18 Mar 2019 06:42:00 GMThttp://www.shnenglu.com/prayer/archive/2019/03/18/216302.htmlhttp://www.shnenglu.com/prayer/comments/216302.htmlhttp://www.shnenglu.com/prayer/archive/2019/03/18/216302.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/216302.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/216302.html直接看图Q?/span>

   

有两个特点:

1Q?r参数

2Q在目录后面使用"."?br style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;" /> 

q样的话可以把/opt/merkle 目录和子目录里面的文件全部下载到本地了?/span>



Prayer 2019-03-18 14:42 发表评论
]]>
shell脚本报错"[: =: unary operator expected"的解x?/title><link>http://www.shnenglu.com/prayer/archive/2019/03/01/216275.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Fri, 01 Mar 2019 07:20:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2019/03/01/216275.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/216275.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2019/03/01/216275.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/216275.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/216275.html</trackback:ping><description><![CDATA[<p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">  在匹配字W串相等Ӟ我用了类DL语句Q?/span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">if [ $STATUS == "OK" ]; then     </span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">echo "OK"</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">fi</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">    在运行时出现?#8220; [: =: unary operator expected” 的错误,׃直找不到原因Q尝试了删除{号两侧的空格和括号里的I格都不用。最?a target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #6795b5; text-decoration: none; cursor: pointer; word-wrap: break-word;">baidu</a>了一下,才找到原因,在条件表辑ּ外再d一?#8220;[]”Q就不会出错了,如下Q?/span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">if [[ $STATUS = "OK" ]]; </span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">then  </span>  </span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">echo "OK"</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">fi </span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">    I其原因Q是因ؓ如果变量STATUSgؓI,那么<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: Arial; word-wrap: break-word;">条g表达?/span>成?[ = "OK"] Q显?[ ?"OK" 不相{ƈ且缺了 </span><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">[ W号Q所以报了这L错误。当然不L出错Q如果变量STATUSg为空Q程序就正常了,所以这L错误q是很隐蔽的?/span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">    或者,用下面的Ҏ也能避免q种错误Q?/span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; word-wrap: break-word;">if [ "$STATUS"x == "OK"x ]; then   </span>  </span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">echo</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">"OK"</span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">fi?/span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">当然Qx也可以是其他字符。顺便提一点,shell中有没有双引号在很多情况下是一致的?/span></p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"> </p><p style="box-sizing: border-box; outline: 0px; margin: 0px 0px 16px; padding: 0px; font-family: Arial; color: #333333; line-height: 26px; overflow-x: auto; word-wrap: break-word; background-color: #ffffff;"><span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; word-wrap: break-word;">转自Q?a rel="nofollow" target="_blank" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; color: #336699; text-decoration: none; cursor: pointer; word-wrap: break-word;">http://hi.baidu.com/vishare/blog/item/bd8ab9ee289753252cf53417.html</a></span></p><img src ="http://www.shnenglu.com/prayer/aggbug/216275.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2019-03-01 15:20 <a href="http://www.shnenglu.com/prayer/archive/2019/03/01/216275.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell脚本字符串截取的8U方?/title><link>http://www.shnenglu.com/prayer/archive/2019/01/24/216219.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 24 Jan 2019 06:18:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2019/01/24/216219.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/216219.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2019/01/24/216219.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/216219.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/216219.html</trackback:ping><description><![CDATA[     摘要: https://www.cnblogs.com/zwgblog/p/6031256.html假设有变?var=http://www.aaa.com/123.htm.1. # h取,删除左边字符Q保留右边字W?echo ${var#*//} 其中 var 是变量名Q? hq算W,*// 表示从左边开始删除第一?// 号及左边的所有字W即删除 http://l果?Qwww.aaa...  <a href='http://www.shnenglu.com/prayer/archive/2019/01/24/216219.html'>阅读全文</a><img src ="http://www.shnenglu.com/prayer/aggbug/216219.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2019-01-24 14:18 <a href="http://www.shnenglu.com/prayer/archive/2019/01/24/216219.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux fg bg ctrl + z jobs & {命?/title><link>http://www.shnenglu.com/prayer/archive/2010/11/29/135006.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 29 Nov 2010 10:50:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/11/29/135006.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/135006.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/11/29/135006.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/135006.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/135006.html</trackback:ping><description><![CDATA[<p>fg、bg、jobs?amp;、ctrl + z都是跟系lQ务有关的Q虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的<br>一?amp; 最l常被用?br>   q个用在一个命令的最后,可以把这个命令放到后台执?br>二。ctrl + z<br>     可以一个正在前台执行的命o攑ֈ后台Qƈ且暂?br>三。jobs<br>     查看当前有多在后台q行的命?br>四。fg<br>     后C的命令调臛_台l运?br>   如果后台中有多个命oQ可以用 fg %jobnumber选中的命令调出,%jobnumber是通过jobs命o查到的后台正在执行的命o的序?不是pid)<br>五。bg<br>     一个在后台暂停的命令,变成l箋执行<br>   如果后台中有多个命oQ可以用bg %jobnumber选中的命令调出,%jobnumber是通过jobs命o查到的后台正在执行的命o的序?不是pid)</p> <img src ="http://www.shnenglu.com/prayer/aggbug/135006.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-11-29 18:50 <a href="http://www.shnenglu.com/prayer/archive/2010/11/29/135006.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell数组详解http://www.shnenglu.com/prayer/archive/2010/09/15/126653.htmlPrayerPrayerWed, 15 Sep 2010 05:26:00 GMThttp://www.shnenglu.com/prayer/archive/2010/09/15/126653.htmlhttp://www.shnenglu.com/prayer/comments/126653.htmlhttp://www.shnenglu.com/prayer/archive/2010/09/15/126653.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/126653.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/126653.html

Bash中还可以使用数组变量,其赋值有两种:

(1) name = (value1 ... valuen) 此时下标?开?br>(2) name[index] = value

数组下标的范围没有Q何限?同时也不必用连l的分量.


$ A=(a b c def)

==================================================
$ echo ${A[@]}          //取全部元?br>a b c def

=================================================

$ echo ${A[0]}         //取第一个元?br>a

=================================================

//取得数组元素的个?br>$ echo ${#A[@]}
4
$ echo ${#A[*]}
4
$ echo ${#A[3]}     //取得元素3的长?/div>
$

==================================================

$ A[3]=yaoshuyin    //第三个元素重新赋?br>$ echo ${A[@]}
a b c yaoshuyin

==================================================
//清除变量
$ unset A
$ echo ${A[@]}
$

==================================================

//清空变量,卛_值变为空
$ A=
$ echo ${A[@]}
$
==================================================

A=B
B=C
unset $A 事实上所取消的变量是 B 而不?A


=======================CZ while循环========================

#建立数组
arrSource=("arrJobs.php"  "arrSubHangye.php" "arrFirst.php" )

arrDest=("buildhr"            \
         "buildtrain/htdocs"  \
         "bankhr"             \
         "healthr"            \
         "elehr"              \
         )

#取数l无元素个数
lenArrSource=${#arrSource[*]}
lenArrDest=${#arrDest[*]}


#循环列出数组元素
i=0
while  [ $i -lt $lenArrSource ]
do
   echo ${arrSource[$i]}
   let i++
done


i=0
while  [ $i -lt $lenArrDest ]
do

   echo ${arrDest[$i]}


   let i++


done




=======================CZQ?for循环===============================



#源文?/span>

arrSource=("/home/800hr/htdocs/login_jump.php")



#目标|站

arrDest=(ithr elehr buildhr bankhr healthr  ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)



for outer in ${arrSource[*]}   

#${arrSource[*]} 是数l中的所有元?/span>

do


   for inner in ${arrDest[*]}


   do


      echo  "ln -s $outer /home/${inner}/campus/"


   done


done



Prayer 2010-09-15 13:26 发表评论
]]>
sed的一强例子集锦的翻??http://www.shnenglu.com/prayer/archive/2010/09/15/126638.htmlPrayerPrayerWed, 15 Sep 2010 03:13:00 GMThttp://www.shnenglu.com/prayer/archive/2010/09/15/126638.htmlhttp://www.shnenglu.com/prayer/comments/126638.htmlhttp://www.shnenglu.com/prayer/archive/2010/09/15/126638.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/126638.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/126638.html译了一?
http://bbs.chinaunix.net/forum/24/20040514/325187.html

我是新手Q翻译得不好Q加注得马马虎虎Q很多地斚w是凭自己的理解写的。由于刚开始学sedQ所以很多地方写得很初Q呵呵,隑օ有些|嗦。写到最后又有些头晕Q还请大虾们多多指点Q里头好几个命o我解释不清楚Q如不吝赐教Q感Ȁ不尽Q?nbsp; 

FILE SPACING:
# double space a file
#使一个文件中每一行都占用两行I间(是在每一行后Ҏ一行空?
sed G
###sed 中G命o的解释ؓappend hold space to pattern space.
###是在当前位|后加一行保留空间中的内容,无Q何动作时Q保留空间ؓI
###所以就double space a file 了,呵呵Q?
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
#假若文g已经含有I白行在其中Q一个文件中每一行占用两?
#I间。输出文件中文本行之间不含有过一行的I
sed '/^$/d;G'
###先用sed '/^$/d' 查找q删除空行;然后?sed G插入I
# triple space a file
#使一个文件中每一行都占用三行I间(是在每一行后Ҏ两行I)
sed 'G;G'
###不用说了吧,重复两次sed G.
# undo double-spacing (assumes even-numbered lines are always blank)
#撤销占用两行I间??假设偶数行都是空白行)
sed 'n;d'
###sed 中命令n的解释ؓRead the next line of input into the pattern spaceQ?
###所以我理解为用sed n d下一行兵紧接着用sed d 删除Q如果隔两行删除一行那?
###用sed 'n,n,d',要是?00行删除一行呢....什???!!!你要?00个n???!!!
# insert a blank line above every line which matches "regex"
#在每个含有字W串regex的行上插入一行空白行
sed '/regex/{x;p;x;}'
###sed 中命令x解释为Exchange the contents of the hold and pattern spaces.
###我的理解也就是交换保留空间与模式I间的内?
###sed 中命令p为Print the current pattern spaceQ就是打印模式空间中的内容.
###所以理解ؓ保留I间中开始ؓIQ模式空间中l过sed '/regex/'查询后ؓ包含
###regex内容的那一行,1)x;交换模式I间和保留空间的内容Q此时模式空间中内容
###为空行,保留I间中内容ؓ含有regex内容的行Q?2)pQ命令打印模式空间内?
###I)Q在原文中含有regex内容的那一行的位置出现两行IQ其中后一行ؓ
###模式I间中的内容Q?)x;后交换模式空间和保留I间中的内容Q.Q.Q结果就是在?
###来出现regex的位|前一行加入了一行空行?
# insert a blank line below every line which matches "regex"
# 在每个含有字W串regex的行下插入一行空白行
sed '/regex/G'
###比较单,查找后在后边加入一I
# insert a blank line above and below every line which matches "regex"
#在每个含有字W串regex的行上,下各插入一行空白行
sed '/regex/{x;p;x;G;}'
###兄弟两个sed '/regex/G'和sed '/regex/{x;p;x;}'合作的结果.
NUMBERING:
# number each line of a file (simple left alignment) Using a tab (see
# note on '\t' at end of file)instead of space will preserve margins.
#l文件每一行加上数字序受用TAB制表W替换空间来保留I白(?)
#(在数字序号和文本中间加一TAB制表W?
sed = filename | sed 'N;s/
/\t/'
###sed = filename的功能是 Print the current line number.
###但是q个功能是在每一行前面另加一行,q且昄行号,而不是直接在行首加序?
###sed中命令N的解释ؓAppend the next line of input into the pattern space.
###也就是把当前行后一行的内容加在当前行后边.
###sed中命令s/regexp/replacement/解释为Attempt to match regexp against the
###pattern space. If successful, replace that portion matched with
### replacement.大概意思是在模式空间外匚wregexpQ如果成功,使用匚wreplace
###ment的内Ҏ换regexp.说白了就是查找替换吧Q?br>是换行符,\t是TAB制表W?
###所以整个命令的意思也出来了Q?
# number each line of a file (nnumber on left, right-aligned)
#l文件每一行加上数字序?数字在左边,向右寚wQ?
sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)
/\1 /'
### 前面不用说了Q但是后?.....
###s/ *\(.\{6,\}\)
/\1 /' q个地方实不是很明?~~
# number each line of file, but only print numbers if line is not blank
#l文件每一行加上数字序P但是仅当行非I时打印数字
sed '/./=' filename | sed '/./N; s/
/ /'
###sed '/./=' filename的用处是查找除非I赋予行号,sed '/./N; s/
/ /'查找?
##Iq把后一行附加到当前?然后用空格替换换行符

# count lines (emulates "wc -l")
#l计行数(cM?"wc -l")
sed -n '$='
#sed中参数n的含义是suppress automatic printing of pattern space,也就是限?
###自动打印模式I间中内容的功能Q?'$='?的含义是Match the last lineQ?前边
###已经说过了,是打印行号Q所以匹配最后一行而且只打印行号不打印内容Q就?
###"wc -l"?
TEXT CONVERSION AND SUBSTITUTION:
# IN UNIX ENVIRONMENT: convert DOS newlines (cR/LF)to Unix format
#在UNIX环境下:转换DOS换行W??)(cR/LF)UNIX格式
sed 's/.$//' # assumes that all lines end with CR/LF
# 假设所有的行都以CR/LFl尾
###可能在DOS中的ASCII?包括CR/LF)CUNIX中都成了单字W吧Q又因ؓ".$"代表
###每行最后一个字W,所以把它们替换掉就OK了.CR/LF是啥QCR---ASCII Carriage
###Return(ASCII 回R) ,LF----ASCII Linefeed (ASCII换行)
sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M
#在bash/tcsh中,按下Ctrl-V然后?Ctrl-M
###没啥说的Q就是查找替换,注意命o?^M"在输入时一定是按下Ctrl-V然后?Ctrl-M
###如果输入成ctrl+6键,然后输入一个大写M,什么替换也完成不了啦.
sed 's/\x0D$//' # gsed 3.02.80, but top script is easier
# ???
###不是很了解!高手说一下吧Q?
# IN UNIX ENVIRONMENT: convert Unix newlines (F)to DOS format
#在unix环境中:转换Unix换行W?F)DOS格式
sed "s/$/`echo -e \\\r`/" # command line under ksh
             #在ksh下的命o?
sed 's/$'"/`echo \\\r`/" # command line under bash
             #在bash下的命o?
sed "s/$/`echo \\\r`/" # command line under zsh
#在zsh下的命o?
sed 's/$/\r/' # gsed 3.02.80
       # gsed3.02.80版本下的命o?
###以上四个命o是在不同的shell版本下用\r(好象是ASCII码下的CR)替换行尾
# IN DOS ENVIRONMENT: convert Unix newlines (F)to DOS format
#在DOS环境下{换Unix换行W到DOS格式
sed "s/$//" # method 1
sed -n p # method 2
###q句又不是很了解Q本?是行尾了,把行替换成I,那就变成了DOS格式了吗Q?
###下边一句也很奇怪,参数-n是suppress automatic printing of pattern spaceQ命
###令p是Print the current pattern spaceQ一正一反就换成DOS格式了?乖乖~~
# IN DOS ENVIRONMENT: convert DOS newlines (cR/LF)to Unix format
#在Dos环境下:转换DOS换行WؓUNIX格式
# Cannot be done with DOS versions of sed. Use "tr" instead.
#用DOS版本的sed不能做到q点Q用"tr"代替Q?
tr -d \r outfile # GNU tr version 1.22 or higher
             #GNU tr 1.22版本或者更高版?
# delete leading whitespace (spaces, tabs)from front of each line
# aligns all text flush left
#删除每一行开头的I白(I格QTAB)左对齐排列全文.
sed 's/^[ \t]*//' # see note on '\t' at end of file
         # ???
### 又是替换成空Q^[ \t]* 的含义ؓ以空格或者TAB键开始的(或者是他们的组?行.
# delete trailing whitespace (spaces, tabs)from end of each line
#从每一行结֤删除最后的I格(I格,TAB)
sed 's/[ \t]*$//' # see note on '\t' at end of file
#??
### 跟上边的命o"前呼后拥"呀Q?
# delete BOTH leading and trailing whitespace from each line
#删除每一行的开头和l尾的空?
sed 's/^[ \t]*//;s/[ \t]*$//'
###两步赎ͼ
# insert 5 blank spaces at beginning of each line (ake page offset)
#在每一行开始处插入5个空?整页偏移)
sed 's/^/ /'
###没啥说的Q?
# align all text flush right on a 79-column width
#叛_齐,?9列宽排列所有文?
sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # set at 78 plus 1 space
###q个语句好像很麻烦,不过看懂了还挺有意思.Q)
###首先出现了几个新东东1.":" 2."&". 3. "-e " 4."t"Q解释一?
###1.":" Label for b and t commands.(lb和t命o加注标签)
###2."&" 表示重复整个匚w的规则表辑ּQ?
###3. "-e" add the script to the commands to be executed
### 把脚本加到命令里用以执行
###4. t label If a s/// has done a successful substitution since the last
###input line was read and since the last t or T command, then branch to
###label; if label is omitted, branch to end of script.
###如果从读入最后一个输入行q且执行最后一个t或者T命o后,一个s///命o成功替换Q?
###那么程分支指向label处,如果label被忽?是没有成功替换吧,我想),那么程
###分支指向脚本l束Q?
###回过头来看,整个sed命o行是一个@环执行的语句Q每一行都要替?78-当前行的?
###W数)?所以如果整个文件比较大Q或者一行字W数比较,做这个动作就有点吃力了.
###不信你试试吧Q呵呵.
# center all text in the middle of 79-column width. In method 1,
# spaces at the beginning of the line are significant, and trailing
# spaces are appended at the end of the line. In method 2, spaces at
# the beginning of the line are discarded in centering the line, and
# no trailing spaces appear at the end of lines.
#使所有文本居?9列宽的格式中央。在W一U方法中Q每一行开头处的空格是
#很重要的Q最后的I格被附在行。第二种Ҏ中,一行开头的I格在中心对
#齐的行中被丢弃,行尾也没有原来结֤的空根{?
sed -e :a -e 's/^.\{1,77\}$/ & /;ta' # method 1
sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\(*\)1/\1/' # method 2
###跟上边的差不?当两辚w加空格的时候,效率要高一些~~
# substitute (ind and replace)"foo" with "bar" on each line
#在每一行中?bar"替换(扑ƈ替换)foo"
sed 's/foo/bar/' # replaces only 1st instance in a line
# 在一行中Q仅在第一ơ出现时替换
sed 's/foo/bar/4' # replaces only 4th instance in a line
#在一行中Q仅在第四次出现时替?
sed 's/foo/bar/g' # replaces ALL instances in a line
#在一行中替换所有出现的?
###q三个命令很?不多说了.
sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
#替换紧邻最后一个匹配出现的?
###'s///---- The replacement may contain the special character & to refer to that
###portion of the pattern space which matched, and the special escapes \1
### through \9 to refer to the corresponding matching sub-expressions in the regexp.
###׃直接译了,大概意思就是replacement处可以包?amp;代表匚w的模式空间中
###的部?Ҏ的\1-\9可以代表regexp中相应的"子表辑ּ",也就是说前面regexp
###可以分ؓ几个子表辑ּ,而后边replacement中可以用\1-\9分别代表它们.q样?
###增加了灵zL,便于修改sed命o.
###把regexp中的\L后,变?.*)foo(*foo),其中(.*)表示零个或者多个字W,
###q样加上后边的\1bar\2变成了改变倒数W二个foo,而倒数W一个不?
sed 's/\(*\)foo/\1bar/' # replace only the last case
#只替换最后一个?
###比上一个简?
# substitute "foo" with "bar" ONLY for lines which contain "baz"
#在每一含有"baz"的行中用"bar"替换(查找q替?foo"
sed '/baz/s/foo/bar/g'
### /baz/用来查找Q后边的用来替换
# substitute "foo" with "bar" EXCEPT for lines which contain "baz"
#在每一不含?baz"的行中用"bar"替换(扑ƈ替换)foo"
sed '/baz/!s/foo/bar/g'
###反其道而行之.
# change "scarlet" or "ruby" or "puce" to "red"
#?scarlet"或?ruby"或?puce"替换?red"
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds
#大多数sed可用
###三步赎ͼ
gsed 's/scarlet\|ruby\|puce/red/g' # GNU sed only
#仅GNU sed可用
# reverse order of lines (emulates "tac")
#反{文章行的序(cM"tac" )
# bug/feature in HHsed v1.5 causes blank lines to be deleted
#???????
sed '1!G;h;$!d' # method 1
###
###首先看第一个命?!GQ这个是啥意?"!"表示后边的命令对所有没?
###被选定的行发生作用QG呢?获得保留I间(专业名词叫内存缓冲区?)?
###的内容,q追加到当前模式I间的后?1是选定W一?h的含义是拯
###模式I间内容C留空?内存~冲?。那么先?sed '1!G'什么含?
###执行一下这个命令,假若文g?
### $ cat test.txt
### 1
### 2
### 3
### 4
###那么 sed '1!G' test.txt的结果是
### $ sed '1!G' test.txt
### 1
### 2
###
### 3
###
### 4
###
### $
### 也就是说除了W一?后边每行都加了空?q是因ؓ内存~冲Z默认?
###是空行吧。然后我们加上h,看看发生什?
### $ sed '1!G;h' test.txt
### 1
### 2
### 1
### 3
### 2
### 1
### 4
### 3
### 2
### 1
### $
### I没了Q咋回事?我是q样理解的,不知道对不对Q大家帮助看看:Q?
###首先要确定,执行到每一行,sed把当前处理的行存在时的~冲区内Q?
###UCؓ模式I间(pattern space).一旦sed完成Ҏ式空间中行的处理Q模?
###I间中的行就被送往屏幕Q行被处理完成后Q就被移出模式空_Q.
###
###命o执行W一行时Q由于匹配了W一行,所?!G"不v作用Q只打印?
###W一行的内容Q然?h"把模版块中的内容也就是第一行的内容拯q缓冲区Q?
###注意此时是用W一行的内容替换I.模式I间中的内容要打华ͼ所以出?.
###执行到第二行Ӟ打印W二行内容,而且׃不匹?1",所以在后边"G"命o?
###作用,获得了缓冲区中的W一行的内容Q然后加到当前模式空间中Qƈ打印,出现
###21。然后把模式I间中的内容写入~冲区,也就是把21写入~冲区。执行到W三?
###匚w不成?所以缓冲区的内容应该是W二行的内容加上W一行的内容Q追加到?
###式空间的W三行后边:321.然后?21拯到缓冲区Q.Q.以此cL出C?
###面的l果.
###我不知道q么解释对不对,但是当我把命令中?换成2Q?Q?后执行,得到了我
###惌的结果。还请高手指点~~
###加上最后一?$!d",那就是前面三行的l果删除Q保留最后一行。这样就形成?
### tac的效果啦?
sed -n '1!G;h;$p' # method 2
###与上边类似的Q不|嗦!
# reverse each character on the line (emulates "rev")
#反{一行中每个字符的顺?cM"rev")
sed '/
/!G;s/\(.\)\(.*
\)/&\2\1/;//D;s/.//'
###q个命o真是.....
###我是在解释不?所以按照我的想法来说一下吧,完全是瞎?
###'/
/!G'是判断本行是否有换行W?如果没有执行G命o
###'s/\(.\)\(.*
\)/&\2\1/'命o是在原来?W二个字W?或者没?开始到换行W?W一个字W?
###//D命o是在模式I间删除W一?注意执行完成后如果模式空间不为空Ql下一?
###循环执行.
###s/.//命o是删除第一个字W?
###假设一行文字是 123

###那么执行后模式空间中的内容应该按下边的顺序变?
### 123

### 123
23
1
### 23
1
### 23
13
21
### 13
21
### 3
21
### 3
21
321
###
321
### 321
### 我的疑问是,Z么第一ơ没有执行s/.//?!如果执行?那么得不到l果了啊!
### 救~~~~命~~~啊!????????????????????????????????
# join pairs of lines side-by-side (like "paste")
#把两行合Z?cM?paste")
sed '$!N;s/
/ /'
###q个命oҎ sed 'N;s/
/ /'一样可以达到目的,不知前面
###?!有什么用?..
# if a line ends with a backslash, append the next line to it
#如果一行以"\"l束Q把下一行加在此行上
sed -e :a -e '/\\$/N; s/\\
//; ta'
###循环%作,两次替换?
# if a line begins with an equal sign, append it to the previous line
# and replace the "=" with a single space
#如果一{号开始某一行,把这一行加到前一行后边,q且用一个空格替换等?
sed -e :a -e '$!N;s/
=/ /;ta' -e 'P;D'
###和上边差不多Q要注意两个新的命o:
### P命o--Print up to the first embedded newline of the current
###pattern space.打印当前模式I间中第一行?
###D命o--Delete up to the first embedded newline in
### the pattern space. Start next cycle, but skip reading from
###the input if there is still data in the pattern space.
###删除当前模式I间中第一行。开始新的@环,但是如果在模式空间中仍然
###有数据,那么跌d输入?
# add commas to numeric strings, changing "1234567" to "1,234,567"
#l数字串加逗号Q把"1234567"变ؓ"1,234,567"
gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds
###(.*[0-9])表示零个或者多个字W?可能包含数字)+一个数??
###([0-9]{3})表示三个数字,然后不停的替?直到条g不成?也就是没?
###四个以上q箋的数字出现就停止?
# add commas to numbers with decimal points and minus signs (NU sed)
#l带有小数点和负L数字的数字加上逗号
gsed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta'
###没有gsedQ不解释?
# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
#每五行后加一I
gsed '0~5G' # GNU sed only
sed 'n;n;n;n;G;' # other seds
###一大早pq了?..
SELECTIVE PRINTING OF CERTAIN LINES:
# print first 10 lines of file (emulates behavior of "head")
#打印一个文件的?0?模仿动作"head")
sed 10q
# print first line of file (emulates "head -1")
#打印一个文件的W一??head -1")
sed q
### q命o的解释Immediately quit the sed script without processing
###any more input, except that if auto-print is not disabled the
###current pattern space will be printed.
### 所以上边两个命令都清楚了,执行到第10行退出就打印?0行,执行W一?
###退出就打印W一?
# print the last 10 lines of a file (emulates "tail")
#打印一个文件的?0??tail")
sed -e :a -e '$q;N;11,$D;ba'
###Label b : Branch to label; if label is omitted, branch to end of script.
###命oD 删除模式I间内第一?newline 字母
前的资料?
###命oN 把输入的下一行添加到模式I间中?
### b label:分支到脚本中带有标号的地方,如果标号不存分支到脚本的末?
###
waker 写到:
试着注一下,不知道对不对
如果我们只看sed -e :a -e '$q;N;ba'
q个循环不停的读入下 一行直到结,q样整个文本Ş成一个由
分割的链
现在加上11,$D
sed -e :a -e '$q;N;11,$D;ba'
如果文本不超q?0?
模式I间保留整个文本打印出?
如果文本大于10?
从第11行开始,在下一行加入到链中后,模式I间W一个由
分割的记录将被删除,q样看v来就是链头被铑ְ挤出整个链,L保持10个链环,循环l束后,链中保存的就是文件的?0?最后印出结?

# print the last 2 lines of a file (emulates "tail -2")
#打印一个文件的最后两??tail -2")
sed '$!N;$!D'
### 开始看不太懂,抄了CU_֍一D?
###sed '$!N;$!D' : Ҏ件倒数W二行以前的行来_N 当前行的下一行放到模
###式空间中以后QD 将模式I间的内容删除了Q到倒数W二行的时候,最后一?
###附加到倒数W二行下面,然后最后一行不执行 D Q所以文件的最后两行都保存下来了?
###不知道是q段话说得有些含p,q是我理解得有偏差,总觉得D命o解释?
###"模式空间的内容删除?有些让hp涂.
###而我是这L解的Q不知道对不?首先说D命o?Delete up to the first
###embedded newline in the pattern space.也就是说D命o是删除模式空间中
###W一个换行符之前的内容,也就是删除第一?然后D命o的解释还有一?我认?
###q句很重? Start next cycle, but skip reading from the input if there
### is still data in the pattern space.开始下一个@环,但是如果模式I间中有
###数据Q则跌从输入中d数据.
###具体怎么工作? 假设文g?
### $ cat test.txt
### 1
### 2
### 3
### 4
### 5
### 那么当执行第一行时,$!N把第二行加入到模式空间中W一行后?然后$!D把第一?
###内容删除Q模式空间中只剩下第二行的内?注意,此时׃D命o开始下一个@环,
###所以不打印模式I间中的内容! (q个地方也是我想了半天才q么解释的,我也知道
###很可能不对,Ƣ迎拍砖Q呵?׃D命o要求模式I间中有数据的话pq读取下一行,
###所以l下一个@环又C$!NQ此时读入第三行加到W二行后边,....以此cL?
###执行到读入第5行附加在W四行后边,然后׃$!D得不到执行,所以第4行和W??
###都被保留Q命令结束,打印模式I间...
# print the last line of a file (emulates "tail -1")
#打印一个文件的最后一??tail -1")
sed '$!d' # method 1
sed -n '$p' # method 2
###哈哈Q终于看懂了一个,你也看懂了吧 Q)
# print only lines which match regular expression (emulates "grep")
#只打印匹配的一定字W的??grep")
sed -n '/regexp/p' # method 1
sed '/regexp/!d' # method 2
###明白参数-n和命令p和d明白这两个命oQ?
# print only lines which do NOT match regexp (emulates "grep -v")
#只打C一定字W不匚w的行(?grep -v")
sed -n '/regexp/!p' # method 1, corresponds to above
sed '/regexp/d' # method 2, simpler syntax
###和上边相反,正如注释所_
# print the line immediately before a regexp, but not the line
# containing the regexp
#打印包含"regexp"那一行的上一?但是不打印包?regexp"的行.
sed -n '/regexp/{g;1!p;};h'
###在命令执行到包含"regexp"那一行的上一行时,模式I间中这行的内容?
###拯C留空间中Q执行到包含"regexp"那一行时打印它?
# print the line immediately after a regexp, but not the line
# containing the regexp
#打印?regexp"之后紧跟那一行,但是除去包含"regexp"的行.
sed -n '/regexp/{n;p;}'
###与上边类|比上边简单.
# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (imilar to "grep -A1 -B1")
#?regexp"前后打印一行上下文Q其行hC?regexp"在哪里出?
#grep -A1 -B1怼)
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
###看上d像挺复杂Q其实倒是不难解释Q?
###假设文档是这?
###$ cat test.txt
### 1 abc
### 2 cde
### 3 regexp
### 4 fgh
### 5 xyz
###命o执行到regexp前一行,引号里边的命令不执行,只执行h命o得到l果
### command parttern space holdspace output
### 执行到前一?2cde 2cde
### 执行到regexp?"=" 3regexp 3
### "x" 2cde 3regexp
### "1!p" 2cde 3regexp 2cde
### "g" 3regexp 3regexp
### "$N" 3regexp  4fgh 3regexp
### "p" 3regexp  4fgh 3regexp 3regexp
### 4fgh
### "D" 4fgh 3regexp
### "h" 4fgh 4fgh
###
### 看一下最双的输出结果,q不错吧Q?
# grep for AAA and BBB and CCC (n any order)
#查找"AAA"?BBB"?CCC".(L序)
sed '/AAA/!d; /BBB/!d; /CCC/!d'
# grep for AAA and BBB and CCC (n that order)
# 查找"AAA"?BBB"?CCC".(一定顺?
sed '/AAA.*BBB.*CCC/!d'
# grep for AAA or BBB or CCC (emulates "egrep")
#查找"AAA"?BBB"?CCC".(L序)
sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # most seds
gsed '/AAA\|BBB\|CCC/!d' # GNU sed only
###上边三个没什么说的,是查找功能呗.
# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
#如果某段包含"AAA",则打印这一Dc?I用来分隔D落)
#HHsed v1.5必须?x;'之后插入一?G;'
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
###前边一部分命o用保留空间来存储整个D落内容Q后边一个命令用来查?
# print paragraph if it contains AAA and BBB and CCC (n any order)
#如果某段包含"AAA"?BBB"?CCC",则打印这一D?
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
###同上
# print paragraph if it contains AAA or BBB or CCC
# 如果某段包含"AAA"?BBB"?CCC",则打印这一D?
sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d' # GNU sed only
###同上
# print only lines of 65 characters or longer
#仅打印长?5个字W的?
sed -n '/^.\{65\}/p'
###q也没什么好说的Q正则表辑ּ的运用.
# print only lines of less than 65 characters
#仅打印少?5个字W的?
sed -n '/^.\{65\}/!p' # method 1, corresponds to above
sed '/^.\{65\}/d' # method 2, simpler syntax
###又没啥吧
# print section of file from regular expression to end of file
#打印从字W?regexp"开始到文gl束的部?
sed -n '/regexp/,$p'
###q没啥,注意","的作用是选择行的范围Q从包含regexp的行到最后一?
# print section of file based on line numbers (ines 8-12, inclusive)
#Ҏ行号来打印文件的一部分(-12行,包括在内)
sed -n '8,12p' # method 1
sed '8,12!d' # method 2
# print line number 52
#打印W?2?
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3, efficient on large files
###仅注意第三种Ҏ效率比较高就行了
# beginning at line 3, print every 7th line
#从第三行开始,?行打C?
gsed -n '3~7p' # GNU sed only
sed -n '3,${p;n;n;n;n;n;n;}' # other seds
###好像很容易理解了?
# print section of file between two regular expressions (nclusive)
#打印文g中指定字W之间的部分(含字W在?
sed -n '/Iowa/,/Montana/p' # case sensitive
###现在单了吧.Q)
SELECTIVE DELETION OF CERTAIN LINES:
# print all of file EXCEPT section between 2 regular expressions
#打印除指定字W之间部分之外的全文
sed '/Iowa/,/Montana/d'
###与上边相似的?
# delete duplicate, consecutive lines from a file (emulates "uniq")
# First line in a set of duplicate lines is kept, rest are deleted.
#删除文g中重复的q箋的行(g"uniq"命o)
#重复行中W一行保留,其他删除
sed '$!N; /^\(.*\)
\1$/!P; D'  
###如果不是最后一行,把下一行附加在模式I间Q然后进行查??
###"^"?$"中间的内容如果有重复匹配成功.如果匚w不成功就用P打印
###W一行. 然后删除W一行.
# delete duplicate, nonconsecutive lines from a file. Beware not to
# overflow the buffer size of the hold space, or else use GNU sed.
#删除文g中重复的Q但不连l的行。注意不要溢Z留空间的~冲器的大小Q?
#否则使用GNU sed.
sed -n 'G; s/
/&&/; /^\([ -~]*
\).*
\1/d; s/
//; h; P'
###在我的linux环境执行不了Q出错是sed: -e expression #1, char 34:
###Invalid range end.是不是所谓的溢出保留I间的大了呢?我也不得而知Q?
###大家补充吧.!!?????????????????
# delete the first 10 lines of a file
#删除一个文件中?0?
sed '1,10d'
# delete the last line of a file
#删除一个文件中最??
sed '$d'
###与上边一个都是查扑ֈ?
# delete the last 2 lines of a file
#删除一个文件中最??
sed 'N;$!P;$!D;$d'
###如果理解了sed '$!N;$!D'是如何工作的Q这句话也不在话下吧Q?
# delete the last 10 lines of a file
#删除一个文件中?0?
sed -e :a -e '$d;N;2,10ba' -e 'P;D' # method 1
sed -n -e :a -e '1,10!{P;N;D;};N;ba' # method 2
###和打印后10行相|什么?打印?0那个没看? /shakehand Q)
###?????????????????
# delete every 8th line
# ?行删??
gsed '0~8d' # GNU sed only
sed 'n;n;n;n;n;n;n;d;' # other seds
###没说?
# delete ALL blank lines from a file (ame as "grep '.' ")
#删除文g所有空白行(g"grep '.' ")
sed '/^$/d' # method 1
sed '/./!d' # method 2
###q两句就是告诉我?.无内容的删除,2.有内容的保留 : )
# delete all CONSECUTIVE blank lines from file except the first; also
# deletes all blank lines from top and end of file (emulates "cat -s")
#删除文g中除一行空白行之外的所有连l空白行Q也同时删除所有从头到所
#有空白行(g"cat -s")
sed '/./,/^$/!d' # method 1, allows 0 blanks at top, 1 at EOF
#Ҏ1不允许文仉部有IQ文件尾部可?
sed '/^$/N;/
$/D' # method 2, allows 1 blank at top, 0 at EOF
#Ҏ2不允许文件尾部有IQ文仉部可?
###两个先选择Q后删除命o.不多说了.
# delete all CONSECUTIVE blank lines from file except the first 2:
#删除文g中连l空行中除前两行I白行之外的所有空白行
sed '/^$/N;/
$/N;//D'
###跟上边的命o怼Q多了一步而已.
# delete all leading blank lines at top of file
#删除文g开头部分中的所有空白行
sed '/./,$!d'
###从有字符开始的行直到最后一行保留,其他删除.
# delete all trailing blank lines at end of file
#删除文gl尾部分中的所有空白行
sed -e :a -e '/^
*$/{$d;N;ba' -e '}' # works on all seds
sed -e :a -e '/^
*$/N;/
$/ba' # ditto, except for gsed 3.02*
###不行了要MQ还是高手说吧,我再看下M疯的Q?
###?????????????????????????????
# delete the last line of each paragraph
#删除每个D落中最??
sed -n '/^$/{p;h;};/./{x;/./p;}'
###应该是假设段落间用空行分?
###命o执行Ӟ如果不是I那么交换模式I间和保留空_如果交换?
###模式I间不ؓI,则打印模式空间中内容Q如果是IQ那么打印模式空?
###间中的内?也就是打印空?..以此cL,出现l果.
###l于完了Q下边的Ҏ应用没有加注Q随便翻译了一下,可能不够准确Q大家参考一下吧.  
SPECIAL APPLICATIONS:
# remove nroff overstrikes (char, backspace)from man pages. The 'echo'
# command may need an -e switch if you use Unix System V or bash shell.
# 从man page里删除所有overstrikes(字符,backspace).如果使用unixpȝv
#或者bash shell,echo命o可能需?e参数.
sed "s/.`echo \\\b`//g" # double quotes required for Unix environment
#unix环境下需要双引号
sed 's/.^H//g' # in bash/tcsh, press Ctrl-V and then Ctrl-H
#在bash/tcsh中,按Ctrl-V然后按Ctrl-H
sed 's/.\x08//g' # hex expression for sed v1.5
#sed v1.5中的hex表达?
# get Usenet/e-mail message header
# 获得新闻l?e-mail信息的标题部?
sed '/^$/q' # deletes everything after first blank line
# get Usenet/e-mail message body
#获得新闻l?e-mail信息的主体部?
sed '1,/^$/d' # deletes everything up to first blank line
# get Subject header, but remove initial "Subject: " portion
#获得题目的标题,但是删去开始的"Subject: "部分
sed '/^Subject: */!d; s///;q'
# get return address header
#获得q回的地址标题()
sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
# parse out the address proper. Pulls out the e-mail address by itself
# from the 1-line return address header (ee preceding script)
#正确解析地址。把email地址从一行中单独提出来ƈq回地址?)
sed 's/ *(*)/; s/>.*//; s/.*[: /'
# delete leading angle bracket & space from each line (nquote a message)
#删除每行的尖括号和空?)信息不被引用)
sed 's/^> //'
# remove most HTML tags (ccommodates multiple-line tags)
#删去大部分HTML标签(供多行标{?)
sed -e :a -e 's/]*>//g;/zipup.bat
dir /b *.txt | sed "s/^\(*\).TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat
               
               
               

本文来自ChinaUnix博客Q如果查看原文请点:http://blog.chinaunix.net/u1/44068/showart_391470.html

Prayer 2010-09-15 11:13 发表评论
]]>
sed学习W记?-高命o http://www.shnenglu.com/prayer/archive/2010/09/15/126634.htmlPrayerPrayerWed, 15 Sep 2010 02:33:00 GMThttp://www.shnenglu.com/prayer/archive/2010/09/15/126634.htmlhttp://www.shnenglu.com/prayer/comments/126634.htmlhttp://www.shnenglu.com/prayer/archive/2010/09/15/126634.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/126634.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/126634.html
版权声明Q?/strong>原创作品Q允许{载,转蝲时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将q究法律责Q?a >http://licong.blog.51cto.com/542131/204226
之前写过一?/span>sed学习W记?/span>http://licong.blog.51cto.com/542131/152541Q讲了一?/span>sed入门的东西,本篇l介l?/span>sed高命o部分。所谓高U,主要是指q里要提到的命令都能改?/span>sed执行或者控制的程序Q?/span>sed通常都是一行被d模式I间Qƈ用脚本中?/span>sed命o一个接一个的应用于那一行) 

sed命o分成3个组Q?/span>

1?span>    处理多行模式I间Q?/span>N?/span>D?/span>PQ?/span>

2?span>    采用保持I间来保存模式空间的内容q他们可用于后l的命oQ?/span>H?/span>h?/span>G?/span>g?/span>xQ?/span>

3?span>    ~写使用分支和条件指令的脚本来更Ҏ制流Q:?/span>b?/span>tQ?nbsp;

N命oQ追加下一?/span>
多行Next(N)命o通过d当前行的下一行,q把两行拼成一行来q行接下来的处理?/span>
$ cat file
line 1
line 2
line 3
line 4
file文g中的每一行后面都有一个隐藏的换行W?/span>”\n”Q?/span>sed不对每行末尾?/span>”\n”q行处理?/span>
$ sed N file
line 1
line 2
line 3
line 4
l过N处理q的输出和原文g没有区别Q但是本质是不一L。这?/span>sed其实认ؓ自己打印的是2行,W一行ؓ”line 1\nline 2”,而第2行ؓ”line 3\nline 4”,注意q里?/span>2行末依焉藏换行符”\n”Q?/span>sed依然不处理行”\n”Q但是处理行内的”\n”。因里默认的动作是打华ͼ所以处理行内的”\n”我们也看不出来?/span>
值得注意的是Q处?/span>line 1Ӟline 2被读入ƈq加?/span>line 1后面Q所?/span>line 1处理完后不再dline 2Q前面已l读q了Q而直接读?/span>line 3q行下一?/span>N命oQ即dline 4q追加到line 3后面Q?/span>
上面2D如果不理解的话Q看下面q个命oQ?/span>
$ sed ‘N;s/\n/ /g’ file
line 1 line 2
line 3 lin3 4
q个命o在原来的基础上把行内?/span>”\n”替换成空gQ尽用了全局替换标志gQ?/span>sed依然不处理行换行符Q)Q看明白N的作用了吗,单独?/span>N可以创徏2行模式空间?nbsp;
D命oQ删除多行模式空间中Q直到第一个行内的”\n”为止的所有内宏V?/span>
D命o通常位于N命o之后Q用于处?/span>N命o创徏的多行模式空间?/span>D命o删除多行模式I间行内W一?/span>”\n”及其之前的内容后Q对余下的内容(W一个行?/span>”\n”之后的内容)重新?/span>sedW一个命令进行处理?/span>

D?/span>d的相同点是,删除内容后,重新?/span>sedW一个命令开始处理,q一点看?/span>D?/span>d都有改变sed执行序的能力;区别在于d删除模式I间中的所有内容,然后重新d文本下一行从sed剙q行处理Q?/span>D删除模式I间的一部分内容Q而将模式I间剩下的内容从sed剙q行处理?/span>
$ sed ‘N;d’ file
$
$sed ‘N;D’ file
line 4
‘N;d’每次创徏2行模式空_q将模式I间的内容全部删除,所以结果所?/span>4行(sed看来?/span>2行)都删除了。如?/span>file?/span>5行数据,则第5行将被打印出来,因ؓW?/span>5行执?/span>N时读不到下一行了Q所?/span>d不执行,打印W?/span>5行,你可以自p一试?/span>
‘N;D’每次创徏2行模式空_删除前面一行,q将模式I间的第2行l执?/span>’N;D’,直到最后一?/span>N没内容可读,不执?/span>DQ而是打印最后一行?/span>’N;D’实际构成了一个@环(xN直接d下一行,?/span>2行进行处理,下一ơ对W?/span>2行就不处理了而是dW?/span>3行处理;而加?/span>D之后Q先处理2行,再对剩下的第2行接着q行处理Q?nbsp;
P命oQ打印模式空间直到第一?/span>”\n”为止的所有内宏V?/span>
pQ小写)是打印模式空间的所有内容,PQ大写)是打印模式空间的一部分Q这一区别?/span>D/dcM。有一点要注意Q不要把M命o攑֜d或?/span>D后面Q因为那栯命o永远也没有执行的Z了。例?/span>sed ‘N;D;P’ file?/span>sed ‘N;D’ file是完全相同的效果?/span>
$ cat file
line 1 li
cong line 2
line 3 licong
line 4 li
cong line 5
我们要把分在两行?/span>li?/span>cong合到一行应该怎么做呢Q?/span>
$ sed 'N;s/li\ncong */licong\n/g;P;D' file
line 1 licong
line 2
line 3 licong
line 4 licong
line 5
执行q个sed命o的流E是q样的:
一、首行文本应?/span>Nd下一行,构成2行模式空?/span>
line 1 li\n
cong line 2
s命o?/span>li\ncong (注意cong后有一个空?/span>)替换?/span>licong\n,?/span>
line 1 licong
line 2
接着执行PQ打?/span>line 1 licongQ?/span>Pl束后两行模式空间没有改变)
再对两行模式I间执行DQ删?/span>line 1 licong\n,q将剩下?/span>line 2做ؓ新的模式I间从头执行sed命oQ?/span>
二、对模式I间内容line 2先执?/span>NQ读取下一?/span>line 3 licongQ创Z行模式空?/span>
line 2
line 3 licong
q次s没找到匹配内容,所以执?/span>s后面?/span>P;D命oQ重新打印、删除,q行下一轮@环;
。。?/span>
三、最后一?/span>line 5执行N没有内容可读Q直接打印该行?/span>
 
h?/span>H?/span>g?/span>G?/span>xq几个命令都是用于模式空间和保持I间转换的。这里有必要解释一下模式空间和保持I间了:
模式I间——容U_前输入行的缓冲区?/span>
除了h/H/g/G/xQ其他所?/span>sed命o都是针对模式I间的内容进行处理的Q?/span>
保持I间——模式空间以外的一个预留缓冲区?/span>
只有h/H/g/G/x命o可以讉KC持空间的内容Qƈ用于与模式空间内容的转换?/span>

hQ将模式I间的内容复制到保持I间Q类g重定向符?/span>>Q会覆盖原由保持I间内容
HQ将模式I间的内容追加到保持I间Q类gq加重定?/span>>>Q追加到保持I间的尾行下
gQ类?/span>hQ将保持I间的内容复制到模式I间
GQ类?/span>HQ将保持I间的内容追加到模式I间
xQ交换模式空间与保持I间中的内容 

$ cat file
1
2
11
22
$ sed ‘/1/{h;d};/2/G’ file
2
1
22
11
q个sed命o完成了反转的功能Q我们来看看是怎么实现的:
一?/span>/1/{h;d}命o搜烦模式I间包含1的行然后先后执行h?/span>d命o。结果首?/span>1被应用Q?span>h把该行(内容?/span>1Q复制到保持I间Q此时模式空间和保持I间中的内容为:
模式I间Q?/span>1
保持I间Q?/span>1
接着执行dQ?/span>d不能讉K保持I间Q只处理模式I间的内容,模式空间的内容删除Q此Ӟ
模式I间Q?/span>      Q空Q没有内容)
保持I间Q?/span>1
二?/span>d命o执行完之后,执行/2/GQ即在模式空间搜索包?/span>2的行q行G处理Q而此时模式空间无内容Q所以不做Q何操作?/span>
三、读取下一行文本到模式I间Ql执?/span>’/1/{h;d};/2/G’。下一行文本的内容?/span>2Q此Ӟ
模式I间Q?/span>2
保持I间Q?/span>1
与步骤二的道理相同,h?/span>d得不到执行(因ؓ模式可空间没扑ֈ包含1的行Q;?/span>’/2/G’被执行Q将保持I间的内容追加到模式I间后面Q此Ӟ
模式I间Q?/span>2
         1
保持I间Q?/span>1
q一步完成以后,模式I间的内容将?/span>sed默认地打印出来(如果?/span>-n参数才不打印Q,于是前两行处理完之后Q将输出Q?/span>
2
1

同理Q后两行处理完之后,输出:
22
11
于是Q我们看C最后的l果Q?/span>
2
1
22
11 
q个最单的例子有很多地方值得挖掘Q我们就来看看最重要的几炏V?/span>

1?nbsp;?/span>sed后面带有一个以上的命oӞ它的处理序是这LQ?/span>

一ơ读入一行到模式I间Q然后将所有的命o应用于该行;而不是一ơ将一个命令作用与所有行Q?/span>

2?nbsp;当某个条件后面有{}Ӟ{}里面的命令依ơ作用于满该条件的行;

3?nbsp;h/H/g/G命o操作~冲区时Q都会在目的~冲区后加一个换行(即该缓冲区无内容)Q然后复制或q加源缓冲区内容在该换行W之后;

4?nbsp;没有使用-n参数Ӟ当一行执行完所有命令后Q?/span>sed会默认打印最l模式空间中的内容;

如果使用-n参数Q只有应用了p/P命o的模式空间内Ҏ会被打印出来?/span>


x命o暂不举例Q我们来看看最后一l高U命令(Q?/span>b?/span>tQ?/span>
分支Q?/span>bQ和试Q?/span>tQ命令几乎可以Q意改?/span>sed命o的执行顺序,他们都将sed转移到包含标{行,如果没有指定标签Q则{Ud命o末尾?/span>b用于无条件的转移Q即一到b立刻转移Q;t用于有条件{U,只有当替换命令改变当前行时才会执行?nbsp;
标签定义Q?/span>
:lable
lable可以随便用什么名字,自己能方便看明白pQ冒号和标签之间不允许有I格Q?/span>lable后面如果有空格将被认为是lable的一部分Q不?/span>lable后面带空|
b?/span>t的用法:
[address]b [lable]
[address]t [lable]
b/t?/span>lable之间有空|?/span>lable后面不要插入I格?/span>
举例Q?/span>
$ cat file
line 1 li
cong line 2
line 3 licong
line 4 li
cong line 5 

$ sed -n '$!{/licong/!{h;N;D}
x;G;N;p;b
}
${/licong/{x;G;p}}' file
cong line 2
line 3 licong
line 4 li 
q个sed命o的功能是L包含字符?/span>licong的行Qƈ打印该行及其前后一行。这个命令ƈ不完善,它不能很好的处理W一行匹配的情况Q加入该功能命o会变得更复杂。我们来解释一下上面的l果是怎么来的?/span>
一?/span>sed -n '$!{/licong/!{h;N;D}
x;G;N;p;b
寚w末行以外的所有行($!的作?/span>)Q执?/span>/licong/!{h;N;D}Q?/span>x;G;N;p;b
二?/span>/licong/!{h;N;D}
对不包含字符?/span>licong的行依次执行hQ?/span>NQ?/span>DQ第一?/span>line 1 li不包?/span>licongQ于是执行:
把该行复制到保持I间Q读取追加下一行,此时
模式I间Q?/span>line 1 li
cong line 2
保持I间Q?/span>line 1 li
再对模式I间执行DQƈ接着从头处理新的模式I间Q此?/span>
模式I间Q?/span>cong line 2
保持I间Q?/span>line 1 li
三、从头对模式I间的内?/span>cong line 2执行$!{/licong/!{h;N;D}Q此时的模式I间依然不包?/span>licongQ所以进行一二两步同L操作Q结束后Q?/span>
模式I间Q?/span>line 3 licong
保持I间Q?/span>cong line 2
四、再从头Ҏ的模式空?/span>line 3 licong执行/licong/!{h;N;D}Q?/span>x;G;N;p;b。因为此时的模式I间包含licongQ所以不执行h;N;D了,而是执行x;G;N;p;b?/span>x用与交换模式I间和保持空间的内容Q交换的l果是:

模式I间Q?/span>cong line 2
保持I间Q?/span>line 3 licong

接下来执?/span>GQ将保持I间的内容追加到模式I间之后Q?/span>

 模式I间Q?/span>cong line 2
           line 3 licong
保持I间Q?/span>line 3 licong

再执?/span>NQ将下一行读取追加到模式I间之后Q?/span>

 模式I间Q?/span>cong line 2
line 3 licong
                    line 4 li
保持I间Q?/span>line 3 licong
再执?/span>pQ打印模式空间的内容Q?/span>
cong line 2
    line 3 licong
       line 4 li            Q这是我们看到的结果)
之后q有一?/span>bQ就是蟩q?/span>b后面的命令,再读下一行。命令最后一部分${/licong/{x;G;p}}是用来处理最后一行的Q如果最后一行包?/span>licong则打印该行很上面一行(而不打印下一行,因ؓ没有N命oQ?/span>

到此Q?/span>sed的高U命令就介绍完了。?/span>sed&awk》第二版有一句话Q?#8220;一旦你理解了这里所l出的命令,那么可以认己是真正?/span>sed的主Z?#8221;
再ȝ一?/span>sed所有的功能Qȝ来说q是替换命o’[address]s/source/replace/flag’最常用Q而高U命令则用于完成复杂的Q务;q有sed?/span>-nQ抑刉认输出)?/span>-iQ更改原文gQ等参数也很常用?nbsp;
写到这里吧Q等待拍砖了。。?/span>

本文 “licong” 博客Q请务必保留此出?a >http://licong.blog.51cto.com/542131/204226

本文 51CTO.COM技术博?/a>

Prayer 2010-09-15 10:33 发表评论
]]>
用sed命o如何能把一个匹配模式替换ؓ换行W?/title><link>http://www.shnenglu.com/prayer/archive/2010/09/15/126632.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 15 Sep 2010 02:15:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/09/15/126632.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/126632.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/09/15/126632.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/126632.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/126632.html</trackback:ping><description><![CDATA[空格替换ؓ换行W?br>两种犯方?br>1使用cutr V+ctrl J代替换行W?br>sed 's/ /\^J/g'<br>2 直接使用换行W?br>s/ /\<br>/g <img src ="http://www.shnenglu.com/prayer/aggbug/126632.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-09-15 10:15 <a href="http://www.shnenglu.com/prayer/archive/2010/09/15/126632.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用shell提供的命令行~辑功能, 选择set -o vi q是 set -o emacs http://www.shnenglu.com/prayer/archive/2010/09/01/125489.htmlPrayerPrayerWed, 01 Sep 2010 01:36:00 GMThttp://www.shnenglu.com/prayer/archive/2010/09/01/125489.htmlhttp://www.shnenglu.com/prayer/comments/125489.htmlhttp://www.shnenglu.com/prayer/archive/2010/09/01/125489.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/125489.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/125489.html今天dpȝQ?敲了N多命令后Q习惯性的用向上的方向键来扑֯历史命oQ突然看Cq个Q?/p>

  $^[[A^[[A^[[A 

按删除键Q出来的是这个:

  $ ^H^H^H^H^H^H^H 

只能?Ctrl+BackSpace 才能删除光标左边的内?..用的那个不习惯啊~~~Q!Q?- -b

后来问到 Wells Q原来只需要做一个小的讑֮Q在Console中启用:

  $set -o emasc 

原因后来上网查了一下,暂且引用一下《FreeBSD使用大全》中的一D内容:

-----------------------------------------------------------------------------------------------

配置用戶shell
  事實上sh與cshQƈ不是普通用戶的最佳選擇,因為這兩個shell在易用性方面做的不夠好Q例如命令行R?輯功能不弗不能利用熱鍵重復執行命令等。較新的shellQ如bash和tcsh都具有這些方便用戶使用的功能,?必須安裝了這些shellE序之后Q才能更Ҏ通用戶的shella置以用這些shellE序。更改用戶的shell 可以使用pw、vipw或chsh?/p>

  這兩個shell分別늅sh和csh的風|因此喜好csh風格的可以選擇tcshQ而喜歡sh風格的選?bash。除了這兩Eshell之外Q還有ksh、zsh{shell可供選擇。如果對兩種shell沒有偏好Q可?選擇bashQ它是一個GNU軟gQ比標准sh功能強大的多?/p>

  使用了這兩些增強型shell之后Q用戶就可以使用shell提供的命令行R輯功能。按照習慣的不同Q可以選 擇vi風格的編輯方式或emacs風格的方式,~省為emacs方式Q這種方式使用方向鍵來回溯埯過的命oQƈ進行 修改Q而vi風格使用字母命o作編輯鍵Q例如用h、j、k、l四個鍵來移動光標,vi風格的好處是不用基本鍵盤之 外的控制鍵,因而可以適用于Mi端a備Qƈ且進行R輯時手不需離開基本늛Q熟R操作之后最為快捗emacs風格 比vvi風格更適合從個ha算轉向Unix的用者的慣。bash, ksh ,zsh下用下面的命o在emacs 風格和vi風格切換Q?/p>

  bash$ set -o emacs

  bash$ set -o vi

  tcsh中用不同的命oQ?/p>

  % bind emacs

  % bind vi

  還可以為常用的命令設|別名,化用戶輸入Q例如:

  bash$ alias ec=”echo This is a alias”

  bash$ ec

  This is a alias

  shell用于方便用戶操作的另一項能力是自動補全命o或文件名的功能,因為FreeBSD下的文g名可能很 P它們全部入比較麻煩。事實上可以輸入部分名字Q然后按Tab鍵(在vi風格下是連續兩次按Esc鍵)Qshell 自動補全文件名的剩余部分。如果已E入的這部分名字不能確定具體的命o或文Ӟ那么shell只將能確定的?分補上,然后韉K通知使用者繼U入以明確具體的文件?/p>

  事實上即使在基本的sh或csh下,也可以?“*” {特D字W,用模式匹配的方式來簡化入?/p>

  bash$ cd /usr/loca*

  bash$ pwd

  /usr/local

  Unix中的多數E序都具備模式匹配的處理能力Q而shell的模式匹配功能最為常用。shell可以使用?些特D模式來配置多個文Ӟ達到化操作的目的。如果要熟練掌握UnixQ必須掌握模式匹配?/p>

  當試圖在bash下入字的時候,除了必須a置i端屬性能接受8位字W之外(埯stty pass8命o Q,還需要設|bash的入換,可以在登錄腳?profile文g中包括以下設|:

  bind 'set convert-meta off'

  bind 'set meta-flag on'

  bind 'set output-meta on'

 

本文来自CSDN博客Q{载请标明出处Q?a >http://blog.csdn.net/chenguoda/archive/2008/06/08/2524292.aspx



Prayer 2010-09-01 09:36 发表评论
]]>
awk中用shell的环境变?/title><link>http://www.shnenglu.com/prayer/archive/2010/08/26/124862.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 26 Aug 2010 10:26:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/08/26/124862.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/124862.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/08/26/124862.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/124862.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/124862.html</trackback:ping><description><![CDATA[<strong>一:"'$var'"<br><br></strong>q种写法大家无需改变?括vawkE序的习?是老外常用的写??<br><br>var="test"<br><br>awk 'BEGIN{print "'$var'"}'<br><br>q种写法其实际是双括号变为单括号的常?传递给了awk.<br><br>如果var中含I格,Zshell不把I格作ؓ分格W?便应该如下?<br><br>var="this is a test"<br><br>awk 'BEGIN{print "'"$var"'"}'<br><br><strong>?'"$var"'</strong><br><br>q种写法与上一U类?如果变量含空?则变?""$var""'较ؓ可靠.<br><br><strong>?把括起awkE序?'变ؓ"",使用"$var"</strong><br><br>?<br><br>$var="this is a test"<br><br>awk 'BEGIN{print "$var"}"<br><br>q是因ؓ?"?是特D字W?而在''?是普通字W?<br><br><strong>?export 变量,使用ENVIRON["var"]形式,</strong><br><br>?<br><br>$var="this is a test";export $var<br><br>awk 'BEGIN{print ENVIRON["var"]}'<br><br><strong>?当然也可以?v选项</strong><br><br>?<br><br>$var="this is a test"<br><br>awk -vnvar="$var" '{print nvar}'<br><br>q样便把pȝ变量定义成了awk变量. <img src ="http://www.shnenglu.com/prayer/aggbug/124862.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-08-26 18:26 <a href="http://www.shnenglu.com/prayer/archive/2010/08/26/124862.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>把shell变量传给awk?U方?/title><link>http://www.shnenglu.com/prayer/archive/2010/08/26/124860.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Thu, 26 Aug 2010 10:16:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/08/26/124860.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/124860.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/08/26/124860.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/124860.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/124860.html</trackback:ping><description><![CDATA[<div>1.利用shell cmdline的特?/div> <div>A=123</div> <div>awk '/^'"$A"'/ { print $2 }'</div> <div> </div> <div>上面的cmdline中,shell?/^'"$A"'/ { print $2 }'作ؓawk的第一个参敎ͼq个'/^'"$A"'/ { print $2 }'可以分ؓ3块</div> <div>Q?Q?/^'Q因?^被单引号引v来,所以shell传这个小部分lawk时候,传的是:/^</div> <div>Q?Q?$A"Q因?A是被双引号引P所以shell传这个小部分lawk时候,先把$A,做给“变量替换”Q所以传的是Q?23</div> <div>Q?Q?/ { print $2 }'Q因? { print $2 }被单引v来,所以传的是Q? { print $2 }</div> <div> </div> <div>q?个小部分做ؓ一个参ClawkQ合?个小部分Q那么传的就?/div> <div>/^123/ { print $2 }</div> <div> </div> <div> </div> <div>2.使用-v参数</div> <div>A=123</div> <div>awk -v B="$A" '/^B/ { print $2 }'</div> <div> </div> <div>3.pass variable settings into awk as "fake file names"</div> <div>awk '/^B/ { print $2 }' B="$A"</div> <img src ="http://www.shnenglu.com/prayer/aggbug/124860.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-08-26 18:16 <a href="http://www.shnenglu.com/prayer/archive/2010/08/26/124860.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>awk的next使用 http://www.shnenglu.com/prayer/archive/2010/08/26/124779.htmlPrayerPrayerThu, 26 Aug 2010 02:35:00 GMThttp://www.shnenglu.com/prayer/archive/2010/08/26/124779.htmlhttp://www.shnenglu.com/prayer/comments/124779.htmlhttp://www.shnenglu.com/prayer/archive/2010/08/26/124779.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/124779.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/124779.html
next  可以单理解ؓ跌后面的代码,如果next被执行,那么它后面的代码都不会被执行了?/td>


Prayer 2010-08-26 10:35 发表评论
]]>
AWK高~程http://www.shnenglu.com/prayer/archive/2010/08/26/124778.htmlPrayerPrayerThu, 26 Aug 2010 02:26:00 GMThttp://www.shnenglu.com/prayer/archive/2010/08/26/124778.htmlhttp://www.shnenglu.com/prayer/comments/124778.htmlhttp://www.shnenglu.com/prayer/archive/2010/08/26/124778.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/124778.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/124778.html1. E序元素

一个awk E序是一对以模式(pattern) 与大括号框v来的操作(action) l合而成的,或许Q还会加上实现操作细节的函数(function ) 。针Ҏ个匹配于输h数据的模式,操作会被执行Q且所有模式都会针Ҏ条输录而检查。模式或操作可省略其中一个。如果模式省略,则操作将被应用到每条输h记录; 如果操作省略Q则默认操作为打印匹配之记录在标准输Z。以下是传统awk E序的配|?

pattern  {action} 如模式匹配,则执行操?

pattern  {action} 如模式匹配,则打印记?

虽然Q模式多半是数字或字W串表达式,不过awk 以保留字BEGIN 与END 提供两种Ҏ模式?

与BEG 工N 兌的操作只会执行一ơ,在Q何命令行文g或一般命令行赋D处理之前Q但是在M开头的一V 选项指定已经完成之后?

END 操作也是只执行一ơ,用于所有输入数据已被处理完之后。它多半用于产生摘要报告Q或是执行清除操作?

BEGIN 与END 模式可以是Q意顺序,可以存在于awk E序内的M位置。不q,Z方便Q我们通常BEGIN 模式攑֜E序的第一个位|,而将END 模式攑֜最后?

2. 注释与空?

awk 里的注释是从# 开始到该行l束Q就像在Shell 里那栗空行等同于I的注释?

3. 字符串与字符串表辑ּ

awk 字符串包含零臛_个字W,且在字符串的长度上没有限Ӟ视可用内存而定?

字符串的比较Q用的是传统的关p运符Q?=( 相等) ?=( 不等) ?lt;( 于) ?lt;=( 于{于) ?gt;( 大于) Q以?gt;=( 大于{于》。比较后q回l 为真Q? 为假。比较不同长度的字符Ԍ且其中一个字W串为另一个的初始子字W串Ӟ较短的会定义为小于较长的那个Q因此,“A ”< “AA ”的gؓ真?

awk q无Ҏ的字W串接箋q算W。也是_两个q箋字符Ԍ会自动地q接在一赗以下每一l赋D|标量变量。ؓ相同的具有四个字W的字符?

s = “ABCD ”

s = “AB ”“CD ”

s = “A ”“B ”“CD ”

s = “A ”“B ”“C ”“D ”

字符串不需要是常数Q如果我们l上q的赋?

t= s s s

则t 的gؓ“ABCDABCDABCD “?

 

数字{换ؓ字符Ԍ通过数字q接I字W串卛_

n =123 Q?

接着是:

s = ““ n Q把?#8220;123 ”赋给s ?

 

awk 功能强大的地方大多来自于它对正则表达式的支持。有两个q算W:~( 匚w) ?~( 不匹? 让awk 更容易用正则表辑ּQ?#8221;ABC ”~ ”^[A-Z]+$ “Q结果ؓ真?

4. 数g数D辑ּ

所有awk 里的数字Q都以双_度的点DC。QҎ可以包含一个末端以字母e( 或E) 所表示?0 ơ方指数以及可选地带正负号的一个整数。D例来?0.03125, 3.125e-2, 3125e-5 ?.003125E1 Q同样都是表C?/32 。因为awk 里所有算术都是Q点算术?

awk q没有提供字W串转数字的函数Q不qawk 的做法很单:只要加个零到字符串里Q例如:s="123" Q接着是n=0+s Q便数?23 赋值给n 了?

5. awk 的数D符

? 一Qawk 的数D符( 优先U由大到排?

q算W?nbsp;                  说明

  ++ --                    增加与减? 前置或后|?

  ^ **                     指数( 右结合?

  ! + -                     非、一?unary) 加号、一元减?

  * / %                    乘、除、余?

  + -                      加、减

< <= == != > >=           比较

&&                         逻辑AND( ?

||                        逻辑OR( ?

?:                        三元条g?

= += -= *= /= %= ^= **=      赋? 右结合?

6. 标量变量

保存单一值的变量叫做标量变量?

a wk 的变量名U必MACSII 字母或下划线开始,然后选择性地接上字母、下划线及字。因此,变量名称要匹配正则表辑ּ[A-Za-z-][A-Za-z_0-9]* 。变量名U在实际上ƈ没有长度的限制。awk 的变量名U是与大写有关?foo, Fo 。与FOO 是完全不同的三个名称。一般用上以及用法? L习惯Q将局部变量全设ؓ写、全局变量W一个字母ؓ大写Q而内建变量则全是大写?

7. 数组变量

awk 允许在数l名UC后,以方括号Q意数字或字符串表辑ּ 括v来作为烦引。例?

telephone["Alice"]= “555-0134"

telephone["Bob"]= “555-0135"

telephone["Carol”]= “555-0136"

telephone["Don"]= “555-0141"

以Q意gؓ索引的数l,UC为关联数l,因ؓ它们的名UC值是相关联的。重要的是,awk 其应用于数l中Q允许查? find ) 、插?insert ) 以及删除( remove) {操作,在一定的旉内完成,与存储多项目无兟?

一个变量不能同时用作标量变量和数组变量。当你应用delet 。语句删除数l的元素

(element] 的时候,不会删除它的名称。因此。像q样的代?

x[1]=3

delete x

x=789

会引发awk 发出提示Q告诉你不可以给数组名称赋?

8. 命o行参?

awk 通过内徏变量ARGC( 参数计数) 与ARGV( 参数向量Q或参数? Q让命o行参?

可用。下面简短的E序说明其用?

[root@local~]#cat showargs.awk

BEGIN{

      print ”ARGC= ”QARGC

      for (k=0;k<ARGC; k++)

          print "ARGV[”k”]=[”ARGV[k] “

}

再来看看它用在一般awk 命o行上Q会产生什么样的结?

[root@local~]# awk -v One=1 -v Two=2 -f showargs.awk Three=3 file1 Four=4 filet2 file3

ARGC=6

ARGV[0]=[awk]

ARGV[1]=[Three=3]

ARGV[2]=[file1]

ARGV[3]=[Four=4]

ARGV[4]=[file2]

ARGV[5]=[file3]

9. 环境变量

awk 提供讉K内徏数组ENV 工RON 中所有的环境变量:

[root@local~]#awk 'BEGIN {print ENVIRON["HOME"];print ENVIRON["USER]} ‘

/home/Jones

hones

通常你应ENVIRON 看成是一个只Ll?

10. 模式

模式由字W串?/ 或数D辑ּ构徏而成。常用的模式如下Q?

NF==0    选定I?

NF>3     选定拥有三个字段以上的记?

NR<5    选定W?1 到第 4 条记?

(FNR==3)&&(FILENAME~/[.] [ch]$/) 来源?C 源文件中选定记录 3

$1~/Jones/   选定字段 1 里有 . ” jones “的记?

/[Xx][Mm][Ll]/ 选定含有‘ 'XML' 。的记录Qƈ忽略大小写差?

$0~/[Xx][Mm][Ll]/ 同上

11. 操作

以最单的形式来说Q纯print 意指在标准输ZQ打印当前的输入记录($0) Q接着是输录分隔字W?ORS 的|默认为单一换行字符。因此,下面q些E序所做的全是相同的操?

1    模式为真Q默认操作ؓ打印

NR>0 {print} 有记录时打印( 恒ؓ?

1    {print}  模式为真。则打印Q这是默认?

{print}  无模式则视ؓ真,明确的打华ͼq是默认?

{print $0}  相同Q但打印明确的?

下面的例子已l是完整的awk E序。在每一个中Q我们都只显C前三个输入字段Qƈ通过省略选定模式Q选定所有的记录。awk E序语句以分号分隔,而且我们会用些略微不同的操作代码,以修改输出字D分隔字W?

[root@local~]#echo ‘one two three four'| awk ‘{print $1,$2,$3}’

one two three

[root@local~]#echo ‘one two three four'| awk ‘{OFS=”…”;print $1,$2,$3}’

one…two…three

[root@local~]#echo ‘one two three four'| awk ‘{OFS=”\n”;print $1,$2,$3}’

one

two

three

 

改变输出字段分隔字符而没有指定Q何字D,不会改变$0:

[root@local~]#echo ‘one two three four'| awk ‘{OFS=”\n”;print $0}’

one two three four

不过Q如果我们更改输出字D分隔字W,q指定至一个字D? 即我们未变更其? Q?

强制以新的字D分隔字W重新组合记录,则结果ؓ:

[root@local~]#echo ‘one two three four'| awk ‘{OFS=”\n”;$1=$1;print $0}’

one

two

three

four

 

12. 在awk 中的单行E序

1.UNIX 单词计数E序wc;

[root@local~]#awk ‘{C+=length($0)+1;w+=NF} END {print NR, W, C}’

 

2. 撇开NUL 字符问题Qawk 其实可以L取代cat Q下面这两个例子会生相同输?

[root@local~]# cat*.xml

[root@local~]# awk 1*.xml

 

3. 要将原始数据值及它们的对数打Cؓ单栏的数据文Ӟ可?

[root@local~]# awk ‘{print $1, log($1)}’file(s)

 

4. 在以I白分隔字段的表gQ报告第n 栏的?

[root@local~]# awk -v COLUMN=n ‘{sum+=$COLUMN} END {print sum} ’file (s)

 

5. 微调上述报告Q生字Dn 的^均?

[root@local~]# awk -v COLUMN=n ‘{sum+=$COLUMN} END {print sum/NR } ’file (s)

6. 针对p文g( 其记录包含描qC金额于最后一个字D? Q打印花ҎL。可使用内徏变量NF 计算d?

[root@local~]# awk’{sum+=$NF; print $0, sum}’files)

 

7. q里是三U查找文件内文本的方?

[root@local~]#egrep ‘pattern|pattern’ file (s)

[root@local~]#awk ‘/pattern|pattern/’file (s)

[root@local~]#awk ‘/pattern}pattern/ {print FILENAME ”: ”FNR ”: ”$0} ’file(s)

 


8. 如果你要限制仅查?00 一150 行,可以通过两个工具E序Q再搭配道Q不q这么做会漏掉位|信?

[root@local~]#sed -n -e 100,150p -s file(s) | egrep 'pattern'

使用GNU sed 要搭?s 选项Q才能ؓ每个文g重新开始行~号。另外,你也可以通过awk Q用比较花哨的模式来做:

[root@local~]#awk ‘(100<=FNR) && (FNR <= 150) && /pattern/\

          {print FILENAME ”:” FNR ”:”$0}’file(s)

9. 要在一个四栏表格里Q调换第二与W三栏,假设它们是以制表字符分隔Q那么可?

使用下面三种方式的其中一U?

[root@local~]#awk -F'\t’-v OFS='\t’{print $1, $3, $2, $4}’old > new

[root@local~]#awk ‘BEGIN{FS=OFS="\t"}{print $1, $3 Q?2 Q?4} ’old>new

[root@local~]#awk –F ‘\t’{print $1"\t" $3"\t" $2"\t" $4}’old>new

 

10. 要将各栏分隔字符由制表字W? 在此?#183;昄) 转换?amp; Q可在以下两U方式择一:

[root@local~]#sed -e 's/ ·/&/g' file(s)

[root@local~]#awk ‘{BEGIN{FS="\t";OFS= “& ”}{$1=$1; print} ’file(s)

 

11. 下面q两个管道,都ؓ删除已排序流里的重复?

[root@local~]#sort file(s)|uniq

[root@local~]# sort file(s)|awk ‘Last!=$0 { print }{Last=$0}’

 

12. 回车字W? 换行字符的行l结Q一致{换ؓ以换行字W作l结Q可在下列方

式中选择一U?

[root@local~]#sed –e ‘s/\r$//’ file(s)

[root@local~]#sed –e ‘s/^M$//’ file(s)

[root@local~]# mawk ‘BEGIN {RS=“\r\n"} {print}’file(s)

 

13. 要将单空格的文本行,转换为双I格的行Q可在下列方式选择一U?

[root@local~]#sed –e ‘/s/$/\n/ ’file(s)

[root@local~]#awk ‘BEGTN{ ORS = "\n\n"){print}’file(s)

[root@local~]#awk ‘BEGIN{ ORS="\n\n" }1 ’ file(s)

[root@local~]#awk {print $0 “\n”} ’ file(s)

[root@local~]#awk ‘{print;print ” ”} ’ file(s)

13. 语句

13.1. 条g语句

if(expressionl)

      stateme 刀t1

else if(expression2)

      statement2

else if(expression3)

      statement3

else if(expressionk)

      statementk

else

        statementk+l

13.2. 重复执行

awk 提供?4 U重复执行语?( 循环 ):

1. 循环在v始处使用l束试 :

while(expression)

statement

2. 循环在结֤使用l束试 :

do

        statement

while (expression)

3. 循环执行可计数的ơ数 :

for(expr1;expr2; expr3)

statement

4. 循环处理兌数组里的元素 :

for(key in array)

statement

例如Q?

for Qname in telephone)

print name“\t" telephone[name]

13.3 数组成员试

成员试key in array 是一个表辑ּ: 如果key 为array 的一个烦引元素,则计ؓ1( ? 。如果key 不是array 的一个烦引元素,?(key in array) ? ?

对于h多下?subscript) 的数l,在测试时Q请使用圆括Pq以逗点分隔下标列表:(i Qj Q?#8230;Qn)in array

成员试不可能徏立数l元素,然而引用元素时Q如果元素不存在Q便会徏立它。因此你应该q么?

if("Sally" in telephone)

print "Sally is in the directory"

而非:

if (telephone["Sally"]!= ””)

    print "Sally is in the directory"

因ؓW二UŞ式会在她(Sally) 不存在时Q将其加入到目录里,q拥有一个空电话L?

重点? 你必能够区分寻扄?index) 与寻扄定?value) 的差异。烦引成员测试需要固定的旉Q而值的查找旉是与数组里元素的个数成正比,q点我们在先前已通过break 语句内的for 循环解释q了。如果你需要时常用到这两种q算Q那么构建反索引数组会比较实?

for (name in telephone)

name_by_telephone[telephone[name]]=name

接下来,你就可以使用name_by_telephone ["555-0136"] 在一定时间内扑ֈ”Carol" 。当Ӟq里假定所有的值是唯一? 如果q两人共享同一个电话,则name_by_telephone 数组只会记录最后一个名U。只要稍做修改就能解册个问?

for (name in telephone)

{

      if (telephone[name] in name_by_telephone)

        name_by_telephone[telephone[name]]=\

              name_by_telephone [telephone[name]) “\t”name

e1se

name_by_telephone[telephone[name]]=name

现在Qname_by_telephone 卛_含了以制表字W分隔的h相同电话L的h名列表?

14. 用户控制输入

awk 也可以通过的getline 语句做这件事。getline 会返回一个|当输入被成功dӞ它的q回gؓ++I Q而返回gؓ0 Ӟ则表C在文gl尾Q?1 则表C错误。它的用法很多,见表?

语法             说明

getline       从当前输入文件中Q读取下一条记录,存入$0 Qƈ更新NF, NR 与FNR

getline var   从当前输入文件中Q读取下一条记录,存入var Qƈ更新NR 与FNR

getline<file   从file 文g中,d下一条记录,存入$0 Qƈ更新NF, NR 与FNR

getline var<file 从file 文g中,d下一条记录,存入var Qƈ更新NF, NR 与FNR

cmd|getline    从外部命令cmd d下一条记录,存入$0 Qƈ更新NF

cmd|getline var 从外部命令cmd d下一条记录,存入var

 


命o道在awk 里可以发挥强大的功能。管道可以在字符字符串中标明Q也可以包含?

意的Shell 命o。这里是与getline 搭配使用Q如?

"date" I getline now

close("date")

print "The current time is".now

 

接下来说明的? 如何在@环里使用命o道:

command="head -n 15 /etc/hosts"

while((command I getline s)>0)

      print s

close(command)

 

15. 执行外部E序

q里是解决电话名录排序问题较短的E序ҎQ用时性文件与systemty Q而非awk 道:

tmpfile= “/tmp/telephone.tmp^

for (name in telephone>

print name "\t" telephone[name]>tmpfile

close(tmpfilej

system("sort < tmpfile “)

临时性文件必d调用system() 之前关闭Q以保M~冲出都正确地记录在文g内?

对于被system() 执行的命令ƈ不需要调用close() Q因为close() 仅针对以I/O 重定向运符所打开的文件或道Q还有getline, print 或printf ?

传递给system[f 的命令可包含数行

system("cat <<EOFILE\nuno\ndos\ntres\nEOFILE"

它生的输出和从嵌入文g复制到标准输Z?

Un0

das

tres

16. 用户自定义函?

函数定义如下:

function name(argl, }rg2, …Qargn

{

statements

}

指定的参数在函数体中用来当作局部变量,它们会隐藏Q何相同名U的全局性变量。函C可用于程序它处,调用的Ş式ؓ:

name(exprl, expr2, …Qexpn)  忽略M的返回?

result=name(exprl, expr2, …Qexprn) 返回值存储到result ?

 

在每个调用点上的表达式,都提供初始值给函数参数型变量。以圆括hh的参敎ͼ必须紧接于函数名UC后,中间没有MI白?

Ҏ量参数所做的变动Q调用者无从得知,不过Ҏl的变动可看见了。换句话_标量Z?by vaule ) Q而数l则Z引用(by reference): q对C 语言也是q样?

函数体里的return expression 语句会终止主体的执行Qƈexpression 的g控制权传l调用点。如果expression 省略Q则q回值由实现期定义。我们测试过的所有系l,q回的不是数字零是I字W串?

17. 字符串函?

17.1. 子字W串提取

提取子字W串的函?substr(string, start, 1en) Q会q回一份由string 的start 字符开始,共len 个字W长度的子字W串副本。字W的位置Q从1 开始编?substr("abcde", 2, 3) 返回。bcd" ?len 参数可省略,省略Ӟ则默认ؓlength(string)-start+1 Q选出字符串的剩余部分?

17.2. 字符串大写转换

tolower(string) 会返回将所有字母改为同义的写的string 副本Q而toupper(string) 则返回被改ؓ大写字母的string 副本。所以tolower("aBcDeF123") q回”abcdef123",toupper("aBcDeF123") q回"ABCDEF123" ?

17.3. 字符串大写转换

index(string,  find) 查找string 里是否有字符串find Q然后返回string 里find 字符串的起始位置Q如果在string 里找不到find Q则q回0 。例如index("abcdef","de") 会返? ?

17.4. 字符串匹?

match Qstring, regexp) string 与正则表辑ּregexp 匚wQ如果匹配,则返?

匚wstring 的烦引,不匹配,则返? 。这U方式提供了比表辑ּ(string~regexp) q多的信息,后者只能得到计? ? 。另外match ( ) 也具有一个有用的副作? 它会全局变量RSTART 设ؓ在string 中要开始匹配的索引|而将RLENGTH 设ؓ要匹配的长度。而匹配子字符串则以substr(string, RSTART, RLENGTH) 表示?

17.5. 字符串替?

awk 在字W串替换功能上,提供两个函数:sub(regexp, replacement, target) 与gsub(regexp, replacement, target), sub() target 与正则表辑ּregexp q行匚wQ将最左边最长的匚w部分替换为字W串replacement 。gsub() 的运行则有点cMQ不q它会替换所有匹配的字符? 前置g 表示global 全局之意) ?

17.6. 字符串替?

awk 针对当前输h记录$0 自动提供了方便的分割为字1,  $}, …?NF Q也可以函数来做:split(string, array, regexp) string 切割为片D,q存储到array 里的q箋元素。在数组里,片段攄在匹配正则表辑ּregexp 的子字符串之间。如果regexp 省略Q则使用内徏字段分隔字符FS 的当前默认倹{函Cq回array 里的元素数量?

17.7. 字符串重?

join() 可确保参数数l不会被引用刎ͼ除非索引是在范围之内。否则,一个具有数l长度ؓ0 的调用可能会建立arrayfl3 Q而修改了调用者的数组。插人的字段分隔字符为普通字W串Q而非正则表达式,所以针对传递给split() 的一般正则表辑ּQjoin() 不会重徏_的原始字W串?

17.8. 字符串格式化

最后一个与字符串相关的函数是在用户控制下格式化数字与字W串:sprintf (format,expression1, expression2, …) Q它会返回已格式化的字符串作为其函数倹{printf() 的运行方式也是这P只不q它会在标准输出或重定向的文件上昄格式化后的字W串Q而不是返回其函数倹{较新的E序语言以更强大的格式化函数来取代格式控制字W串Q但相对而言让代码变得很冗长。按照传l的文本处理应用来说Qsprintf 与printf

18. 数值函?

函数           说明

atan2(y, x)   y q回y/x 的反正切Qg?pai ?pai 之间?

cos(x)        q回x 的余弦? 以弧?radians) 计算) Q该g?1 ?1 之间

exp(x)        q回x 的指敎ͼex,

int(x)        q回x 的整数部分,截去前置?

log(x)        q回x 的自然对数?

rand()        q回q_分布的虚拟随机r,O<=r<l

sin(x)        q回x 的正弦? 以弧?radians] 计算) Q该g?1 ?1 之间

sqrt(x)       q回x 的^?

srand(x)      讄虚拟随机产生器的U子为x Qƈq回正确的种子。如果省略x Q则使用当前旉( 以秒? 。如果。rand ( ) 未被调用Q则awk 在每ơ执行时会从相同的默认种子开?mawk 则不会?


本文来自CSDN博客Q{载请标明出处Q?a >http://blog.csdn.net/wzhwho/archive/2010/04/21/5513791.aspx



Prayer 2010-08-26 10:26 发表评论
]]>
awk中用的shell命ohttp://www.shnenglu.com/prayer/archive/2010/08/25/124714.htmlPrayerPrayerWed, 25 Aug 2010 10:53:00 GMThttp://www.shnenglu.com/prayer/archive/2010/08/25/124714.htmlhttp://www.shnenglu.com/prayer/comments/124714.htmlhttp://www.shnenglu.com/prayer/archive/2010/08/25/124714.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/124714.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/124714.htmlawk中用的shell命oQ有2U方法:

一。用所以systemQ)

awk 'BEGIN {system("echo \"Input your name:\\c\""); getline d;print "\nYour name is",d,"\b!\n"}'

用(""Q和\"   \"   \\转义?/p>

 

system - execute a shell command Q)里面接入命o

awkE序中我们可以用system() 函数去调用shell命o
如:awk 'BEGIN{system("echo abc")}' file
echo abc ׃做ؓ“命o?#8221;Q由shell来执行,所以我们会得到以下l果Q?br>

root@ubuntu:~# awk 'BEGIN{system("echo abc")}'
abc
root@ubuntu:~#


root@ubuntu:~# awk 'BEGIN{v1="echo";v2="abc";system(v1" "v2)}'
abc
root@ubuntu:~#


root@ubuntu:~# awk 'BEGIN{v1="echo";v2="abc";system(v1 v2)}'
/bin/sh: echoabc: command not found
root@ubuntu:~#


root@ubuntu:~# awk 'BEGIN{v1=echo;v2=abc;system(v1" "v2)}'
root@ubuntu:~#

从上面的例子Q我们简单的分析一下awk是怎样调用system的:
如果systemQ)括号里面的参数没有加上双引号的话Qawk认ؓ它是一个变量,它会从awk的变量里面把它们先置换ؓ帔RQ然后再回传lshell

如果systemQ)括号里面的参数有加上双引L话,那么awkq接把引号里面的内容回传给shellQ作为shell?#8220;命o?#8221;

二。用print cmd | “/bin/bash”

root@ubuntu:~# awk 'BEGIN{print "echo","abc"| "/bin/bash"}'
abc
root@ubuntu:~#

root@ubuntu:~# awk 'BEGIN{print "echo","abc",";","echo","123"| "/bin/bash"}'
abc
123
root@ubuntu:~#


三。ȝ

无论使用systemQ)q是print cmd | “/bin/bash”
awk都是新开一个shellQ在相应的cmdline参数送回lshellQ所以要注意当前shell变量与新开shell变量问题

1.1
root@ubuntu:~# abc=12345567890
root@ubuntu:~# awk 'BEGIN{system("echo $abc")}'

root@ubuntu:~#


1.2
root@ubuntu:~# export abc=12345567890
root@ubuntu:~# awk 'BEGIN{system("echo $abc")}'
12345567890
root@ubuntu:~#

2.1
root@ubuntu:~# abc=1234567890
root@ubuntu:~# awk 'BEGIN{print "echo","$abc"| "/bin/bash"}'

root@ubuntu:~#


2.2
root@ubuntu:~# export abc=1234567890
root@ubuntu:~# awk 'BEGIN{print "echo","$abc"| "/bin/bash"}'
1234567890
root@ubuntu:~#


以上例子Q没有export的话Q那些变量都是只存在于当前shell变量中,所以都是echo不出来的 Q?br>而用了 export的都是环境变量,所以awk调用新的shell时候,可以echo出来



Prayer 2010-08-25 18:53 发表评论
]]>
Linux Shell真是无所不能之split命o?/title><link>http://www.shnenglu.com/prayer/archive/2010/08/25/124637.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 25 Aug 2010 03:08:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/08/25/124637.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/124637.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/08/25/124637.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/124637.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/124637.html</trackback:ping><description><![CDATA[     H然发现到Linux shell命o的强大,有时在Windows需要一个Y件的操作Q在ubuntul端执行一条命令就可以? <p>比如Q?/p> <p>split命o</p> <p>语法:komy@komy-vmware:~$ <font color=#ff0000><strong>split [-bl] 输入文g 输出文g</strong></font></p> <p><font color=#000000>参数说明</font></p> <p><font color=#000000>-b :以文件大来?/font></p> <p><font color=#000000>-lQ以行数来分</font></p> <p><font color=#000000>例如split -l 100 test.txt out</font></p> <p><font color=#000000>是把test.txt?00行输Z个文件outaa,outab,outcd以此cL。。。实C文本文g的分剌Ӏ?/font></p> <p><font color=#000000>split -b 100k test.txt out</font></p> <p><font color=#000000>是把test.txt?00KB输出一个文件outaa,outab,outcd以此cL?/font></p> <p><font color=#000000>q两个命令对于手机看说有限制的情况下很受用。。?/font></p> <p><font color=#000000>不仅仅如此cat 命o可将q些分割后的文g合ƈZ个文Ӟq定向?#8220;largefile”文g中(largefile定的文g名)?</font></p> <p style="TEXT-INDENT: 2em"></p> <p style="TEXT-INDENT: 2em">komy@komy-vmware:~$ <font color=#ff0000><strong>cat out* > largeflie</strong></font></p> <img src ="http://www.shnenglu.com/prayer/aggbug/124637.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-08-25 11:08 <a href="http://www.shnenglu.com/prayer/archive/2010/08/25/124637.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shelld行经总码(ZZQ?/title><link>http://www.shnenglu.com/prayer/archive/2010/08/14/123455.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Sat, 14 Aug 2010 11:44:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/08/14/123455.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/123455.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/08/14/123455.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/123455.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/123455.html</trackback:ping><description><![CDATA[#!/usr/bin/ksh<br>#<br># SCRIPT: 12_ways_to_parse.ksh.ksh<br>#<br>#<br># REV: 1.2.A<br>#<br># PURPOSE:   This script shows the different ways of reading<br>#    a file line by line.   Again there is not just one way<br>#    to read a file line by line and some are faster than<br>#    others and some are more intuitive than others.<br>#<br># REV LIST:<br>#<br>#    03/15/2002 - Randy Michael<br>#    Set each of the while loops up as functions and the timing<br>#    of each function to see which one is the fastest.<br>#<br>#######################################################################<br>#<br>#    NOTE: To output the timing to a file use the following syntax:<br>#<br>#       12_ways_to_parse.ksh file_to_process   > output_file_name 2>&1<br>#<br>#    The actaul timing data is sent to standard error, file <br>#    descriptor (2), and the function name header is sent<br>#    to standard output, file descriptor (1).<br>#<br>#######################################################################<br>#<br># set -n   # Uncomment to check command syntax without any execution<br># set -x   # Uncomment to debug this script<br>#<br><br>FILENAME="$1"<br>TIMEFILE="/tmp/loopfile.out"<br>>$TIMEFILE<br>THIS_SCRIPT=$(basename $0)<br><br>######################################<br>function usage<br>{<br>echo "\nUSAGE: $THIS_SCRIPT   file_to_process\n"<br>echo "OR - To send the output to a file use: "<br>echo "\n$THIS_SCRIPT   file_to_process   > output_file_name 2>&1 \n"<br>exit 1<br>}<br>######################################<br>function while_read_LINE<br>{<br>cat $FILENAME | while read LINE<br>do<br>       echo "$LINE"<br>       :<br>done<br>}<br>######################################<br>function while_read_LINE_bottom <br>{<br>while read LINE<br>do<br>       echo "$LINE"<br>       :<br><br>done < $FILENAME<br>}<br>######################################<br>function while_line_LINE_bottom<br>{<br>while line LINE<br>do<br>       echo $LINE<br>       :<br>done < $FILENAME<br>}<br>######################################<br>function cat_while_LINE_line  <br>{<br>cat $FILENAME | while LINE=`line`<br>do<br>       echo "$LINE"<br>       :<br>done<br>}<br>######################################<br>function while_line_LINE<br>{<br>cat $FILENAME | while line LINE<br>do<br>       echo "$LINE"<br>       :<br>done<br>}<br>######################################<br>function while_LINE_line_bottom <br>{<br>while LINE=`line`<br>do<br>       echo "$LINE"<br>       :<br><br>done < $FILENAME <br>}<br>######################################<br>function while_LINE_line_cmdsub2 <br>{<br>cat $FILENAME | while LINE=$(line)<br>do<br>       echo "$LINE"<br>       :<br>done<br>}<br>######################################<br>function while_LINE_line_bottom_cmdsub2 <br>{<br>while LINE=$(line)<br>do<br>       echo "$LINE"<br>       :<br><br>done < $FILENAME <br>}<br>######################################<br>function while_read_LINE_FD <br>{<br>exec 3<&0<br>exec 0< $FILENAME<br>while read LINE<br>do<br>       echo "$LINE"<br>       :<br>done<br>exec 0<&3<br>}<br>######################################<br>function while_LINE_line_FD <br>{<br>exec 3<&0<br>exec 0< $FILENAME<br>while LINE=`line`<br>do<br>       echo "$LINE"<br>       :<br>done<br>exec 0<&3<br>}<br>######################################<br>function while_LINE_line_cmdsub2_FD<br>{<br>exec 3<&0<br>exec 0< $FILENAME<br>while LINE=$(line)<br>do<br>       print "$LINE"<br>       :<br>done<br>exec 0<&3<br>}<br>######################################<br>function while_line_LINE_FD <br>{<br>exec 3<&0<br>exec 0< $FILENAME<br><br>while line LINE<br>do<br>       echo "$LINE"<br>       :<br>done<br><br>exec 0<&3<br>}<br>######################################<br>########### START OF MAIN ############<br>######################################<br><br># Test the Input<br><br># Looking for exactly one parameter<br>(( $# == 1 )) || usage<br><br># Does the file exist as a regular file?<br>[[ -f $1 ]] || usage<br><br>echo "\nStarting File Processing of each Method\n"<br><br>echo "Method 1:"<br>echo "\nfunction while_read_LINE\n" >> $TIMEFILE<br>echo "function while_read_LINE"<br>time while_read_LINE >> $TIMEFILE<br>echo "\nMethod 2:"<br>echo "\nfunction while_read_LINE_bottom\n" >> $TIMEFILE<br>echo "function while_read_LINE_bottom"<br>time while_read_LINE_bottom >> $TIMEFILE<br>echo "\nMethod 3:"<br>echo "\nfunction while_line_LINE_bottom\n" >> $TIMEFILE<br>echo "function while_line_LINE_bottom"<br>time while_line_LINE_bottom >> $TIMEFILE<br>echo "\nMethod 4:"<br>echo "\nfunction cat_while_LINE_line\n" >> $TIMEFILE<br>echo "function cat_while_LINE_line"<br>time cat_while_LINE_line >> $TIMEFILE<br>echo "\nMethod 5:"<br>echo "\nfunction while_line_LINE\n" >> $TIMEFILE<br>echo "function while_line_LINE"<br>time while_line_LINE >> $TIMEFILE<br>echo "\nMethod 6:"<br>echo "\nfunction while_LINE_line_bottom\n" >> $TIMEFILE<br>echo "function while_LINE_line_bottom"<br>time while_LINE_line_bottom >> $TIMEFILE<br>echo "\nMethod 7:"<br>echo "\nfunction while_LINE_line_cmdsub2\n" >> $TIMEFILE<br>echo "function while_LINE_line_cmdsub2"<br>time while_LINE_line_cmdsub2 >> $TIMEFILE<br>echo "\nMethod 8:"<br>echo "\nfunction while_LINE_line_bottom_cmdsub2\n" >> $TIMEFILE<br>echo "function while_LINE_line_bottom_cmdsub2"<br>time while_LINE_line_bottom_cmdsub2 >> $TIMEFILE<br>echo "\nMethod 9:"<br>echo "\nfunction while_read_LINE_FD\n" >> $TIMEFILE<br>echo "function while_read_LINE_FD"<br>time while_read_LINE_FD >> $TIMEFILE<br>echo "\nMethod 10:"<br>echo "\nfunction while_LINE_line_FD\n" >> $TIMEFILE<br>echo "function while_LINE_line_FD"<br>time while_LINE_line_FD >> $TIMEFILE<br>echo "\nMethod 11:"<br>echo "\nfunction while_LINE_line_cmdsub2_FD\n" >> $TIMEFILE<br>echo "function while_LINE_line_cmdsub2_FD"<br>time while_LINE_line_cmdsub2_FD >> $TIMEFILE<br>echo "\nMethod 12:"<br>echo "\nfunction while_line_LINE_FD\n" >> $TIMEFILE<br>echo "function while_line_LINE_FD"<br>time while_line_LINE_FD >> $TIMEFILE <img src ="http://www.shnenglu.com/prayer/aggbug/123455.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-08-14 19:44 <a href="http://www.shnenglu.com/prayer/archive/2010/08/14/123455.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell的效率不如perl或Python? http://www.shnenglu.com/prayer/archive/2010/06/07/117311.htmlPrayerPrayerMon, 07 Jun 2010 04:25:00 GMThttp://www.shnenglu.com/prayer/archive/2010/06/07/117311.htmlhttp://www.shnenglu.com/prayer/comments/117311.htmlhttp://www.shnenglu.com/prayer/archive/2010/06/07/117311.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/117311.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/117311.html
shell文本处理斚wQ主要是有sedQawkq两把刀?br>
光给你个shell你进行文本处理是做不到的Q当然简单的q可以?br>
perl里有正则表达式引擎,能针Ҏ本进行拆分替换等复杂操作Q关键是Qperl不用fork出新的进E来处理q些事情Q而shell需要用管道等通过q些sedQawk的进E来处理Q至就多出部分q程的开销?br>
而且Q管道这个东西,原本一个工具一ơ能完成的Q务,在需要经qsedQ和awk{多ơ管道。效率可惌知?br>
其实你这个问题问的有点弱智,SHELL是什么?

SHELL只是一个接口,大部分的功能全靠外部E序来完成?br>而Perl是一U语aQ基本上什么事情都能做?/td>


老老实实学好一样东西最实际?br>当你参加面试的时候,如果可以自信的说自己熟练使用Shell或者PerlQ那q当不错了?br>效率之类的云云,具体问题具体分析吧,能解军_作中遇到的问题就行?br>

看出来楼L较关心文本处理,下面q个比方不一定恰当,但已l很接近了:


  • 相比较而言Qawk、sed像L手,而perl和python是个zL手?br>
  • awk和sed专注于文本处理,大部分情冉|率要优于perl{。很单,比如列文Ӟ谁能有cat的效率高Q?br>
  • 如果你是个懒惰的SAQ那qshell吧。如果是geek或者你的工作需要复杂的逻辑Qƈ且还会出现许多无法预知的新要求,那就用perl{好了?br>


没那么多关键字,是打字手误,但是1-10万数量的关键字Q在千万行文本文g中搜索,q个量还是有的?br>
来段sed multiple keywords search in batch的代码,Ƣ迎其他新想法?
  1.                 local num_in_batch=300
  2.                 local batch_counter=0
  3.                 local log_display_in_batch=1000
  4.                 local bbl_counter=0
  5.                 local num_in_bl_counter=0
  6.                 declare -a bbl_arr=()
  7.                 local keyword=
  8.                 local keyword_del

  9.                
  10.                 logMsg $debug_flag null "Starting blacklist checking in batch."

  11.                 for msisdn in `cut -d: -f2 $tmpfile`; do       
  12.                         if [ $batch_counter -eq $num_in_batch ]; then
  13.                                 keyword="${keyword}$msisdn"
  14.                                 bbl_arr=(`sed -rn "/$keyword/p" $filter_file`)
  15.                                 local num=${#bbl_arr[@]}
  16.                                 ((num_in_bl_counter+=num))
  17.                                 if [ $num -gt 1 ]; then
  18.                                         keyword_del=`echo ${bbl_arr[@]} | tr " " "|"`
  19.                                         sed -ri "/${keyword_del}/d" $inputfile
  20.                                 fi
  21.                                 keyword=
  22.                                 batch_counter=0
  23.                         else
  24.                                 keyword="${keyword}$msisdn|"
  25.                         fi
  26.                         if [ $((bbl_counter%log_display_in_batch)) -eq 0 ]; then
  27.                                 logMsg $debug_flag null "$bbl_counter MSISDN processed."
  28.                         fi
  29.                         ((batch_counter++))
  30.                         ((bbl_counter++))
  31.                 done
复制代码


Prayer 2010-06-07 12:25 发表评论
]]>
sort -Thttp://www.shnenglu.com/prayer/archive/2010/05/12/115191.htmlPrayerPrayerWed, 12 May 2010 06:14:00 GMThttp://www.shnenglu.com/prayer/archive/2010/05/12/115191.htmlhttp://www.shnenglu.com/prayer/comments/115191.htmlhttp://www.shnenglu.com/prayer/archive/2010/05/12/115191.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/115191.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/115191.html            Places all temporary files that are created into the directory specified by the Directory parameter.

Prayer 2010-05-12 14:14 发表评论
]]>
sort 指定排序域的两种Ҏhttp://www.shnenglu.com/prayer/archive/2010/05/12/115190.htmlPrayerPrayerWed, 12 May 2010 06:12:00 GMThttp://www.shnenglu.com/prayer/archive/2010/05/12/115190.htmlhttp://www.shnenglu.com/prayer/comments/115190.htmlhttp://www.shnenglu.com/prayer/archive/2010/05/12/115190.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/115190.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/115190.html       The -k KeyDefinition flag uses the following form:

       -k [ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]

       The sort key includes all characters beginning with the field specified by the FStart variable and the column specified by
       the CStart variable and ending with the field specified by the FEnd variable and the column specified by the CEnd variable.
       If Fend is not specified, the last character of the line is assumed. If CEnd is not specified the last character in the
       FEnd field is assumed. Any field or column number in the KeyDefinition variable may be omitted. The default values are:
       FStart
            Beginning of the line
       CStart
            First column in the field

       FEnd
            End of the line
       CEnd
            Last column of the field

       If there is any spaces between the fields, sort considers them as separate fields.

       The value of the Modifier variable can be one or more of the letters b, d, f, i, n, or r. The modifiers apply only to the
       field definition they are attached to and have the same effect as the flag of the same letter. The modifier letter b
       applies only to the end of the field definition to which it is attached. For example:

       -k 3.2b,3r

       specifies a sort key beginning in the second nonblank column of the third field and extending to the end of the third
       field, with the sort on this key to be done in reverse collation order. If the FStart variable and the CStart variable fall
       beyond the end of the line or after the FEnd variable and the CEnd variable, then the sort key is ignored.

       A sort key can also be specified in the following manner:

       [+[FSkip1] [.CSkip1] [Modifier] ] [-[FSkip2] [.CSkip2] [Modifier]]

       The +FSkip1 variable specifies the number of fields skipped to reach the first field of the sort key and the +CSkip
       variable specifies the number of columns skipped within that field to reach the first character in the sort key. The -FSkip
       variable specifies the number of fields skipped to reach the first character after the sort key, and the -CSkip variable
       specifies the number of columns to skip within that field. Any of the field and column skip counts may be omitted. The
       defaults are:

       FSkip1
            Beginning of the line
       CSkip1
            Zero
       FSkip2
            End of the line
       CSkip2
            Zero

       The modifiers specified by the Modifier variable are the same as in the -k flag key sort definition.

       The field and column numbers specified by +FSkip1.CSkip1 variables are generally one less than the field and column number
       of the sort key itself because these variables specify how many fields and columns to skip before reaching the sort key.
       For example:

       +2.1b -3r

       specifies a sort key beginning in the second nonblank column of the third field and extending to the end of the third
       field, with the sort on this key to be done in reverse collation order. The statement +2.1b specifies that two fields are
       skipped and then the leading blanks and one more column are skipped. If the +FSkip1.CSkip1 variables fall beyond the end of

       the line or after the -FSkip2.CSkip2 variables, then the sort key is ignored.

       Note: The maximum number of fields on a line is 10.



Prayer 2010-05-12 14:12 发表评论
]]>
《sort命o的k选项大讨论?linux命o五分钟系列之二十?/title><link>http://www.shnenglu.com/prayer/archive/2010/05/12/115187.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Wed, 12 May 2010 05:34:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/05/12/115187.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/115187.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/05/12/115187.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/115187.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/115187.html</trackback:ping><description><![CDATA[<p><strong>本原创文章属?a target=_blank><u><font color=#e08105>《Linux大棚?/font></u></a>博客Q博客地址?a target=_blank><u><font color=#e08105>http://roclinux.cn</font></u></a>。文章作者ؓrocrocket?/strong></p> <p><span style="COLOR: #000000"></span><span style="COLOR: #000000"><strong>Z防止某些|站的恶性{载,特在每篇文章前加入此信息Q还望读者体谅?/strong></span></p> <p><strong>===</strong></p> <p><span style="COLOR: #993300"><strong>[正文开始]</strong></span><br>有时候学习脚本,你会发现sort命o后面跟了一堆类?k1,2Q或?k1.2 -k3.4的东东,有些匪夷所思。今天,我们来搞定它?k选项Q?/p> <p><strong>1 准备素材</strong></p> <p>$ cat facebook.txt<br>google 110 5000<br>baidu 100 5000<br>guge 50 3000<br>sohu 100 4500</p> <p><span id=more-1472></span></p> <p>W一个域是公司名UͼW二个域是公思h敎ͼW三个域是员工^均工资。(除了公司名称Q其他的别信Q都瞎写的^_^Q?/p> <p><strong>2 我想让这个文件按公司的字母顺序排序,也就是按W一个域q行排序Q(q个facebook.txt文g有三个域Q?/strong></p> <p>$ sort -t ‘ ‘ -k 1 facebook.txt<br>baidu 100 5000<br>google 110 5000<br>guge 50 3000<br>sohu 100 4500</p> <p>看到了吧Q就直接?k 1讑֮可以了。(其实此处q不严格Q稍后你׃知道Q?/p> <p><strong>3 我想让facebook.txt按照公司人数排序</strong></p> <p>$ sort -n -t ‘ ‘ -k 2 facebook.txt<br>guge 50 3000<br>baidu 100 5000<br>sohu 100 4500<br>google 110 5000</p> <p>不用解释Q我怿你能懂?/p> <p>但是Q此处出C问题Q那是baidu和sohu的公思h数相同,都是100人,q个时候怎么办呢Q按照默认规矩,是从W一个域开始进行升序排序,因此baidu排在了sohu前面?/p> <p><strong>4  我想让facebook.txt按照公司人数排序 Qh数相同的按照员工q_工资升序排序Q?/strong></p> <p>$ sort -n -t ‘ ‘ -k 2 -k 3 facebook.txt<br>guge 50 3000<br>sohu 100 4500<br>baidu 100 5000<br>google 110 5000</p> <p>看,我们加了一?k2 -k3p决了问题。对_sort支持q种讑֮Q就是说讑֮域排序的优先U,先以W?个域q行排序Q如果相同,再以W?个域q行排序。(如果你愿意,可以一直这么写下去Q设定很多个排序优先U)</p> <p><strong>5 我想让facebook.txt按照员工工资降序排序Q如果员工h数相同的Q则按照公司人数升序排序Q(q个有点隑ֺ喽)</strong></p> <p>$ sort -n -t ‘ ‘ -k 3r -k 2 facebook.txt<br>baidu 100 5000<br>google 110 5000<br>sohu 100 4500<br>guge 50 3000</p> <p>此处有用了一些小技巧,你仔l看看,?k 3后面偷偷加上了一个小写字母r。你xQ再l合我们<a target=_blank><u><font color=#e08105>上一文?/font></u></a>Q能得到{案么?揭晓Qr?r选项的作用是一LQ就是表C逆序。因为sort默认是按照升序排序的Q所以此处需要加上r表示W三个域Q员工^均工资)是按照降序排序。此处你q可以加上nQ就表示对这个域q行排序Ӟ要按照数值大进行排序,举个例子吧:</p> <p>$ sort -t ‘ ‘ -k 3nr -k 2n facebook.txt<br>baidu 100 5000<br>google 110 5000<br>sohu 100 4500<br>guge 50 3000</p> <p>看,我们L了最前面?n选项Q而是它加入C每一?k选项中了?/p> <p><strong>6 -k选项的具体语法格?/strong></p> <p>要l往下深入的话,׃得不来点理论知识。你需要了?k选项的语法格式,如下Q?/p> <p>[ FStart [ .CStart ] ] [ Modifier ] [ , [ FEnd [ .CEnd ] ][ Modifier ] ]</p> <p>q个语法格式可以被其中的逗号Q?#8220;Q?#8221;Q分Z大部分,Start部分和End部分?/p> <p>先给你灌输一个思想Q那是“如果不设定End部分Q那么就认ؓEnd被设定ؓ行尾”。这个概念很重要的,但往往你不会重视它?/p> <p>Start部分也由三部分组成,其中的Modifier部分是我们之前说过的类似n和r的选项部分。我们重点说说Start部分的FStart和C.Start?/p> <p>C.Start也是可以省略的,省略的话pCZ本域的开头部分开始。之前例子中?k 2?k 3是省略了C.Start的例子喽?/p> <p>FStart.CStartQ其中FStart是表示使用的域Q而CStart则表C在FStart域中从第几个字符开始算“排序首字W?#8221;?/p> <p>同理Q在End部分中,你可以设定FEnd.CEndQ如果你省略.CEndQ则表示l尾?#8220;域尾”Q即本域的最后一个字W。或者,如果你将CEnd讑֮?(?Q也是表C结ֈ“域尾”?/p> <p><strong>7 H发奇想Q从公司英文名称的第二个字母开始进行排序:</strong></p> <p>$ sort -t ‘ ‘ -k 1.2 facebook.txt<br>baidu 100 5000<br>sohu 100 4500<br>google 110 5000<br>guge 50 3000</p> <p>看,我们使用?k 1.2Q这pC对W一个域的第二个字符开始到本域的最后一个字Wؓ止的字符串进行排序。你会发现baidu因ؓW二个字母是a而名列榜首。sohu和googleW二个字W都是oQ但sohu的h在google的o前面Q所以两者分别排在第二和W三。guge只能屈居W四了?/p> <p><strong>8 又突发奇惻IQ只针对公司英文名称的第二个字母q行排序Q如果相同的按照员工工资q行降序排序Q?/strong></p> <p>$ sort -t ‘ ‘ -k 1.2,1.2 -k 3,3nr facebook.txt<br>baidu 100 5000<br>google 110 5000<br>sohu 100 4500<br>guge 50 3000</p> <p>׃只对W二个字母进行排序,所以我们用了-k 1.2,1.2的表C方式,表示我们“?#8221;对第二个字母q行排序。(如果你问“我?k 1.2怎么不行Q?#8221;Q当然不行,因ؓ你省略了End部分Q这意味着你将对从W二个字母v到本域最后一个字Wؓ止的字符串进行排序)。对于员工工资进行排序,我们也用了-k 3,3Q这是最准确的表qͼ表示我们“?#8221;Ҏ域进行排序,因ؓ如果你省略了后面?Q就变成了我?#8220;对第3个域开始到最后一个域位置的内容进行排?#8221;了?/p> <p><strong>9 在modifier部分q可以用到哪些选项Q?/strong></p> <p>可以用到b、d、f、i、n ?r?/p> <p>其中n和r你肯定已l很熟悉了?/p> <p>b表示忽略本域的签到空白符受?/p> <p>d表示Ҏ域按照字兔R序排序(卻I只考虑I白和字母)?/p> <p>f表示Ҏ域忽略大写q行排序?/p> <p>i表示忽略“不可打印字符”Q只针对可打印字W进行排序。(有些ASCII是不可打印字符Q比如\a是报警,\b是退|\n是换行,\r是回车等{)</p> <p><strong>10 思考思考关?k?u联合使用的例子:</strong></p> <p>$ cat facebook.txt<br>google 110 5000<br>baidu 100 5000<br>guge 50 3000<br>sohu 100 4500</p> <p>q是最原始的facebook.txt文g?/p> <p>$ sort -n -k 2 facebook.txt<br>guge 50 3000<br>baidu 100 5000<br>sohu 100 4500<br>google 110 5000</p> <p>$ sort -n -k 2 -u facebook.txt<br>guge 50 3000<br>baidu 100 5000<br>google 110 5000</p> <p>当设定以公司员工域进行数值排序,然后?u后,sohu一行就被删除了Q原?u只识别用-k讑֮的域Q发现相同,将后箋相同的行都删除?/p> <p>$ sort  -k 1 -u facebook.txt<br>baidu 100 5000<br>google 110 5000<br>guge 50 3000<br>sohu 100 4500</p> <p>$ sort  -k 1.1,1.1 -u facebook.txt<br>baidu 100 5000<br>google 110 5000<br>sohu 100 4500</p> <p>q个例子也同理,开头字W是g的guge没有幸免于难?/p> <p>$ sort -n -k 2 -k 3 -u facebook.txt<br>guge 50 3000<br>sohu 100 4500<br>baidu 100 5000<br>google 110 5000</p> <p>咦!q里讄了两层排序优先的情况下Q?u没有删除Q何行。原?u是会权衡所?k选项Q将都相同的才会删除Q只要其中有一U不同都不会L删除?)Q不信,你可以自己加一行sina 100 4500试试看)</p> <p><strong>11 最诡异的排序:</strong></p> <p>$ sort -n -k 2.2,3.1 facebook.txt<br>guge 50 3000<br>baidu 100 5000<br>sohu 100 4500<br>google 110 5000</p> <p>以第二个域的W二个字W开始到W三个域的第一个字W结束的部分q行排序?/p> <p>W一行,会提? 3Q第二行提取00 5Q第三行提取00 4Q第四行提取10 5?/p> <p>又因为sort认ؓ0于00于000于0000….</p> <p>因此0 3肯定是在W一个?0 5肯定是在最后一个。但Z?0 5却在00 4前面呢?Q你可以自己做实验思考一下。)</p> <p>{案揭晓Q原?#8220;跨域的设定是个假?#8221;Qsort只会比较W二个域的第二个字符到第二个域的最后一个字W的部分Q而不会把W三个域的开头字W纳入比较范围。当发现00?0相同Ӟsort׃自动比较W一个域M。当然baidu在sohu前面了。用一个范例即可证实:</p> <p>$ sort -n -k 2.2,3.1 -k 1,1r facebook.txt<br>guge 50 3000<br>sohu 100 4500<br>baidu 100 5000<br>google 110 5000</p> <p><strong>12 有时候在sort命o后会看到+1 -2q些W号Q这是什么东东?</strong></p> <p>关于q种语法Q最新的sort是这么进行解释的Q?/p> <p>On older systems, `sort’ supports an obsolete origin-zero syntax `+POS1 [-POS2]‘ for specifying sort keys.  POSIX 1003.1-2001 (*note Standards conformance::) does not allow this; use `-k’ instead.</p> <p>原来Q这U古老的表示方式已经被淘CQ以后可以理直气壮的鄙视使用q种表示Ҏ的脚本喽Q?/p> <p>Qؓ了防止古老脚本的存在Q在q再说一下这U表C方法,加号表示Start部分Q减可CEnd部分。最最重要的一ҎQ这U方式方法是?开始计数的Q以前所说的W一个域Q在此被表示为第0个域。以前的W?个字W,在此表示为第1个字W。明白?Q?/p> <p><span style="COLOR: #0000ff"><strong>l束语:</strong></span></p> <p><span style="COLOR: #0000ff"><strong>本文是互联网上仅有的比较全的关于sort的k选项的论q文章,如需转蝲请务必注?#8220;转自Linux大棚-Linux主题博客”Q谢谢各?)</strong></span></p> <p><span style="COLOR: #0000ff"><strong>sort?k选项基本是q堆内容了,如果大家有什么补充,qa?) Ƣ迎交流Q?/strong></span></p> <img src ="http://www.shnenglu.com/prayer/aggbug/115187.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-05-12 13:34 <a href="http://www.shnenglu.com/prayer/archive/2010/05/12/115187.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《sort帮你排序?linux命o五分钟系列之二十?-o选项http://www.shnenglu.com/prayer/archive/2010/05/12/115186.htmlPrayerPrayerWed, 12 May 2010 05:32:00 GMThttp://www.shnenglu.com/prayer/archive/2010/05/12/115186.htmlhttp://www.shnenglu.com/prayer/comments/115186.htmlhttp://www.shnenglu.com/prayer/archive/2010/05/12/115186.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/115186.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/115186.htmlsort是在Linux里非常常用的一个命令,排序的Q集中精力,五分钟搞定sortQ现在开始!

1 sort的工作原?/strong>

sort文件的每一行作Z个单位,怺比较Q比较原则是从首字符向后Q依ơ按ASCII码D行比较,最后将他们按升序输出?/p>

[rocrocket@rocrocket programming]$ cat seq.txt
banana
apple
pear
orange
[rocrocket@rocrocket programming]$ sort seq.txt
apple
banana
orange
pear

2 sort?u选项

它的作用很简单,是在输中去除重复行?/p>

[rocrocket@rocrocket programming]$ cat seq.txt
banana
apple
pear
orange
pear
[rocrocket@rocrocket programming]$ sort seq.txt
apple
banana
orange
pear
pear
[rocrocket@rocrocket programming]$ sort -u seq.txt
apple
banana
orange
pear

pear׃重复?u选项无情的删除了?/p>

3 sort?r选项

sort默认的排序方式是升序Q如果想Ҏ降序Q就加个-r搞定了?/p>

[rocrocket@rocrocket programming]$ cat number.txt
1
3
5
2
4
[rocrocket@rocrocket programming]$ sort number.txt
1
2
3
4
5
[rocrocket@rocrocket programming]$ sort -r number.txt
5
4
3
2
1

4 sort?o选项

׃sort默认是把l果输出到标准输出,所以需要用重定向才能将l果写入文gQŞ如sort filename > newfile?/p>

但是Q如果你x排序l果输出到原文g中,用重定向可就不行了?/p>

[rocrocket@rocrocket programming]$ sort -r number.txt > number.txt
[rocrocket@rocrocket programming]$ cat number.txt
[rocrocket@rocrocket programming]$
看,竟然number清空了?/p>

在q个时候,-o选项出现了,它成功的解决了这个问题,让你攑ֿ的将l果写入原文件。这或许也是-o比重定向的唯一优势所在?/strong>

[rocrocket@rocrocket programming]$ cat number.txt
1
3
5
2
4
[rocrocket@rocrocket programming]$ sort -r number.txt -o number.txt
[rocrocket@rocrocket programming]$ cat number.txt
5
4
3
2
1

5 sort?n选项

你有没有遇到q?0?的情况。我反正遇到q。出现这U情冉|׃排序E序这些数字按字符来排序了Q排序程序会先比??Q显?,所以就?0攑֜2前面喽。这也是sort的一贯作风?/p>

我们如果x变这U现Ӟp使用-n选项Q来告诉sortQ?#8220;要以数值来排序”Q?/p>

[rocrocket@rocrocket programming]$ cat number.txt
1
10
19
11
2
5
[rocrocket@rocrocket programming]$ sort number.txt
1
10
11
19
2
5
[rocrocket@rocrocket programming]$ sort -n number.txt
1
2
5
10
11
19

6 sort?t选项?k选项

如果有一个文件的内容是这P

[rocrocket@rocrocket programming]$ cat facebook.txt
banana:30:5.5
apple:10:2.5
pear:90:2.3
orange:20:3.4

q个文g有三列,列与列之间用冒号隔开了,W一列表C水果类型,W二列表C水果数量,W三列表C水果h根{?/p>

那么我想以水果数量来排序Q也是以第二列来排序,如何利用sort实现Q?/p>

q好Qsort提供?t选项Q后面可以设定间隔符。(是不是想起了cut和paste?d选项Q共鸣~~)

指定了间隔符之后Q就可以?k来指定列C?/p>

[rocrocket@rocrocket programming]$ sort -n -k 2 -t : facebook.txt
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3

我们使用冒号作ؓ间隔W,q对第二列来进行数值升序排序,l果很o人满意?/p>

7 其他的sort常用选项

-f会将写字母都{换ؓ大写字母来进行比较,亦即忽略大小?/p>

-c会检查文件是否已排好序,如果乱序Q则输出W一个ؕ序的行的相关信息Q最后返?

-C会检查文件是否已排好序,如果乱序Q不输出内容Q仅q回1

-M会以月䆾来排序,比如JAN于FEB{等

-b会忽略每一行前面的所有空白部分,从第一个可见字W开始比较?/p>

未完待箋Q敬h待:《sort命o的k选项大讨论?/strong>



Prayer 2010-05-12 13:32 发表评论
]]>
shell预定义变?/title><link>http://www.shnenglu.com/prayer/archive/2010/04/12/112366.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 12 Apr 2010 11:11:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/04/12/112366.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/112366.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/04/12/112366.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/112366.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/112366.html</trackback:ping><description><![CDATA[预定义变量和环境变量相类|也是在Shell一开始时定义了的变量。所不同的是Q用户只能根据Shell的定义来使用q些变量Q而不能重定义它。所有预定义变量都是?W和另一个符L成的Q常用的Shell预定义变量有Q?br> $# 位置参数的数量?br> $* 所有位|参数的内容?br> $? 命o执行后返回的状态?br> $$ 当前q程的进E号?br> $! 后台q行的最后一个进E号?br> $0 当前执行的进E名?br> 其中Q??用于查上一个命令执行是否正。(在Linux中,命o退出状态ؓ0表示该命令正执行,M?DC命令出错。)<br> $$变量最常见的用途是用做暂存文g的名字以保证暂存文g不会重复?img src ="http://www.shnenglu.com/prayer/aggbug/112366.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-04-12 19:11 <a href="http://www.shnenglu.com/prayer/archive/2010/04/12/112366.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>怎样向awk中传入shell变量?/title><link>http://www.shnenglu.com/prayer/archive/2010/04/12/112365.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Mon, 12 Apr 2010 11:10:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/04/12/112365.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/112365.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/04/12/112365.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/112365.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/112365.html</trackback:ping><description><![CDATA[<p>假设当前目录下有a.log b.log二个文gQ利用awk打印出其文g名,脚本如下Q?/p> <p>#! /bin/bash<br> for file in *.log<br> do<br>         awk 'BEGIN{var=" ' "$file" ' "}END{print var;}' "$file"<br> done</p> <p><strong><span style="color: #0000ff;">在这里最让hh的是单引号和双引?/span> </strong> </p> <p><br> <strong>我们可以利用bash -x test.sh来执行脚本,其会脚本中的shell变量展开Q便于调?/strong> </p> <p><strong>’单引号中除(’Q都为原本字W,没有Ҏ意义</strong> </p> <p><strong>"双引号中除(</strong> <strong>$`"Q?/strong> <strong>都ؓ原本字符Q没有特D意?/strong> </p> <p><strong>因此Q上q可以展开?</strong> </p> <p><strong>W一ơ:<br> </strong> </p> <p><strong> awk 'BEGIN{var="a.log"}END{print var;}‘ a.log</strong> </p> <p><strong>W二ơ:<br> </strong> </p> <p><strong>awk 'BEGIN{var="b.log"}END{print var;}‘ b.log</strong> </p> <p>如果其写成 awk 'BEGIN{var=' "$file" '}END{print var;}' "$file"Q则展开为:</p> <p><strong>W一ơ:<br> </strong> </p> <p><strong> awk 'BEGIN{var=a.log}END{print var;}‘ a.log</strong> </p> <p><strong>W二ơ:<br> </strong> </p> <p><strong>awk 'BEGIN{var=b.log}END{print var;}‘ b.log</strong> </p> <p><strong>会报错,因ؓawk中字W串要用双引hh</strong> </p> <p><strong><br> </strong> </p> <p><strong>其分解成以下这P便于理解Q?/strong> </p> <p><strong></strong> 'BEGIN{var=" '    "$file"     ' "}END{print var;}'  q三部分为字W串q接关系</p> <img src ="http://www.shnenglu.com/prayer/aggbug/112365.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-04-12 19:10 <a href="http://www.shnenglu.com/prayer/archive/2010/04/12/112365.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>eval用法三例,与大家分?很有用o ! 对字W串两次解析?/title><link>http://www.shnenglu.com/prayer/archive/2010/03/28/110744.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Sun, 28 Mar 2010 09:49:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/03/28/110744.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/110744.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/03/28/110744.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/110744.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/110744.html</trackback:ping><description><![CDATA[<p> </p> <p>## eval用法三例 ##<br>## shell:/bin/sh ##<br>## yhc 2002.09.26 18:00 ##</p> <p>#例一:<br>#L合符条g的变量名,然后该变量的D予另一变量<br>v1=aaa<br>v2=bbb<br>c=1<br>if [ $c -eq 1 ]<br>then<br>  vname=v$c   #扑ֈW合条g的变量名为v1<br>  eval vvv="$"$vname ; echo vvv: $vvv    #变量v1的D予vvv,?使vvv=aaa<br>  eval vvv='$'$vname ; echo vvv: $vvv    #变量v1的D予vvv,?使vvv=aaa<br> #eval vvv=$$vname   ; echo vvv: $vvv    #错误用法<br>fi</p> <p><br>#例二:#以变量v1的值aaa作ؓ变量?变量vaaa的D予这一新定义的变量aaa<br>v1=aaa ; vaaa="This is aaa"<br>#eval $v1=$vaaa    ; echo aaa: $aaa    #错误用法<br>#eval $v1="$vaaa"  ; echo aaa: $aaa    #错误用法<br>eval $v1='$vaaa'   ; echo aaa: $aaa</p> <p><br>#例三:<br>#以变量v1的值aaa作ؓ变量?q将变量名字串作为D予自w?br>v1=aaa ; vaaa="This is aaa"<br>eval $v1=$v1   ; echo aaa: $aaa    #与例二的错误用法不同,q一用法是正的<br>eval $v1="$v1" ; echo aaa: $aaa    #与例二的错误用法不同,q一用法是正的<br>eval $v1='$v1' ; echo aaa: $aaa</p> <p>该文章{载自[E序人生]Q?lt;a href="<a target="_blank">http://www.procedurelife.com/technical/SHELL/content/lbkjf.html</a><br><br><br><br></p> <img src ="http://www.shnenglu.com/prayer/aggbug/110744.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-03-28 17:49 <a href="http://www.shnenglu.com/prayer/archive/2010/03/28/110744.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shell中trap捕捉CL处理http://www.shnenglu.com/prayer/archive/2010/03/28/110743.htmlPrayerPrayerSun, 28 Mar 2010 09:42:00 GMThttp://www.shnenglu.com/prayer/archive/2010/03/28/110743.htmlhttp://www.shnenglu.com/prayer/comments/110743.htmlhttp://www.shnenglu.com/prayer/archive/2010/03/28/110743.html#Feedback0http://www.shnenglu.com/prayer/comments/commentRss/110743.htmlhttp://www.shnenglu.com/prayer/services/trackbacks/110743.html一. trap捕捉C号之后,可以有三U反应方式:

  (1)执行一D늨序来处理q一信号

  (2)接受信号的默认操?br>
  (3)忽视q一信号

  ? trap对上面三U方式提供了三种基本形式Q?br>
  W一UŞ式的trap命o在shell接收到signal list清单中数值相同的信号Ӟ执行双

  引号中的命o丌Ӏ?br>
  trap 'commands' signal-list

  trap "commands" signal-list

  Z恢复信号的默认操作,使用W二UŞ式的trap命oQ?br>
  trap signal-list

  W三UŞ式的trap命o允许忽视信号

  trap " " signal-list

 

 

注意Q?br>
  (1) 对信?1(D违?不能捕捉Q因为shell本n需要捕捉该信号去进行内存的转储?br>
  (2) 在trap中可以定义对信号0的处?实际上没有这个信?Q?shellE序在其l止(?br>
  执行exit语句)时发信号?br>
  (3) 在捕捉到signal-list中指定的信号q执行完相应的命令之后, 如果q些命o没有

  shellE序l止的话QshellE序l执行收Ch所执行的命令后面的命oQ这样将

  很容易导致shellE序无法l止?br>
  另外Q在trap语句中,单引号和双引h不同的,当shellE序W一ơ碰到trap语句Ӟ

  把commands中的命o扫描一遍。此时若commands是用单引hh的话Q那么shell不会

  对commands中的变量和命令进行替换, 否则commands中的变量和命令将用当时具体的值来

 

kill -l可以列出pȝ的信?/p>

通常我们需要忽略的信号有四个,卻IHUP, INT, QUIT, TSTPQ也是信号1, 2, 3, 24
使用q样的语句可以ɘq些中断信号被忽略:
trap "" 1 2 3 24 ?trap "" HUP INT QUIT TSTP
?trap Q? 2 3 24 ?trap HUP INT QUIT TSTP使其回复默认倹{?
用stty -a可以列出中断信号与键盘的对应Q分别执行上面的命o后,q行
tail -f /etc/passwd, 然后试用键盘中断,试试两种情况Q默认和忽略Q下有何不同?
更方便的是我们可以用在shell中用trap定义我们自己的信号处理程?/p>

 

 

 

 

#!/bin/bash
#scriptname: trapping
#can use the singnal numbers of bash abbreviations seen
#below. Cannot use SIGINT ,SIGOUIT ,etc
trap 'echo Control-c will not terminate $0. ' INT
trap 'echo Control-\ will not terminate $0. ' QUIT
trap 'echo Control-Z will not terminate $0. ' TSTP
echo "Enter any string after the prompt. When you are ready to exit ,type \"stop\"."
while true
do
echo -n "Go ahead ...>"
read
if [[ $reply==[sS]top ]]
then
   break
fi
done


Prayer 2010-03-28 17:42 发表评论
]]>
awk内置字符串函数详?/title><link>http://www.shnenglu.com/prayer/archive/2010/03/28/110742.html</link><dc:creator>Prayer</dc:creator><author>Prayer</author><pubDate>Sun, 28 Mar 2010 09:14:00 GMT</pubDate><guid>http://www.shnenglu.com/prayer/archive/2010/03/28/110742.html</guid><wfw:comment>http://www.shnenglu.com/prayer/comments/110742.html</wfw:comment><comments>http://www.shnenglu.com/prayer/archive/2010/03/28/110742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/prayer/comments/commentRss/110742.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/prayer/services/trackbacks/110742.html</trackback:ping><description><![CDATA[<div>awk提供了许多强大的字符串函敎ͼ见下表:<br>awk内置字符串函?br> <table class=zeroBorder id=c-nq style="WIDTH: 458px; HEIGHT: 298px" cellSpacing=0 cellPadding=3 border=0> <tbody> <tr> </tr> <tr> <td width="50%">gsub(r,s)</td> <td width="50%">在整?0中用s替代r </td> </tr> <tr> <td width="50%">gsub(r,s,t)</td> <td width="50%">在整个t中用s替代r</td> </tr> <tr> <td width="50%">index(s,t)<span></span><br></td> <td width="50%">q回s中字W串t的第一位置</td> </tr> <tr> <td width="50%">length(s)<br></td> <td width="50%">q回s长度</td> </tr> <tr> <td width="50%">match(s,r)<br></td> <td width="50%">试s是否包含匚wr的字W串</td> </tr> <tr> <td width="50%">split(s,a,fs)</td> <td width="50%"><span></span>在fs上将s分成序列a </td> </tr> <tr> <td width="50%">sprint(fmt,exp)</td> <td width="50%">q回lfmt格式化后的exp </td> </tr> <tr> <td width="50%">sub(r,s)<br></td> <td width="50%">?0中最左边最长的子串代替s </td> </tr> <tr> <td width="50%">substr(s,p)</td> <td width="50%">q回字符串s中从p开始的后缀部分</td> </tr> <tr> <td width="50%">substr(s,p,n)<br></td> <td width="50%">q回字符串s中从p开始长度ؓn的后~部分</td> </tr> </tbody> </table> 详细说明一下各个函数的使用Ҏ?br><span id="m44gmgg" class=fullpost><br>gsub函数有点cM于sed查找和替换。它允许替换一个字W串或字Wؓ另一个字W串或字W,q以正则表达式的形式执行。第一个函C用于记录$0Q第二个gsub函数允许指定目标Q然而,如果未指定目标,~省?0?br>index(s,t)函数q回目标字符串s中查询字W串t的首位置。length函数q回字符串s字符<br>长度。match函数试字符串s是否包含一个正则表辑ּr定义的匹配。split使用域分隔符fs?br>字符串s划分为指定序列a。sprint函数cM于printf函数(以后涉及)Q返回基本输出格式fmt?br>l果字符串exp。sub(r,s)函数用s替代$0中最左边最长的子串Q该子串?r)匚w?br>sub(s,p)q回字符串s在位|p后的后缀。substr(s,p,n)同上Qƈ指定子串长度为n?br>现在看一看awk中这些字W串函数的功能?br><br><span style="FONT-WEIGHT: bold">1.gsub</span><br>要在整个记录中替换一个字W串为另一个,使用正则表达式格式,/目标模式/Q替换模?br>/。例如改变学生序?842?899Q?br><br>$ awk 'gsub('4842/, 4899) {print $0}' grade.txt<br>J.Troll 07/99 4899 Brown-3 12 26 26<br><br><span style="FONT-WEIGHT: bold">2.index</span><br>查询字符串s中t出现的第一位置。必ȝ双引号将字符串括h。例如返回目标字W串<br>Bunny中ny出现的第一位置Q即字符个数?br><br>$ awk 'BEGIN {print index("Bunny", "ny")} grade.txt<br>4<br><br><span style="FONT-WEIGHT: bold">3.length</span><br>q回所需字符串长度,例如验字W串J.Trollq回名字及其长度Q即人名构成的字W个<br>数?br><br>$ awk '$1=="J.Troll" {print length($1) " "$1}' grade.txt<br>7 J.Troll<br><br>q有一U方法,q里字符串加双引受?br><br>$ awk 'BEGIN {print length("A FEW GOOD MEN")}'<br>14<br><br><span style="FONT-WEIGHT: bold">4.match</span><br>match试目标字符串是否包含查扑֭W的一部分。可以对查找部分使用正则表达?q?br>回gؓ成功出现的字W排列数。如果未扑ֈ,q回0,W一个例子在ANCD中查找d。因其不<br>存在,所以返?。第二个例子在ANCD中查找D。因其存?所以返回ANCD中D出现的首?br>|字W数。第三个例子在学生J.Lulu中查找u?br><br>$ awk '{BEGIN {print match("ANCD", /d/)}'<br>0<br>$ awk '{BEGIN {print match("ANCD", /C/)}'<br>3<br>$ awk '$1=="J.Lulu" {print match($1, "u")} grade.txt<br>4<br><br><span style="FONT-WEIGHT: bold">5.split</span><br>使用splitq回字符串数l元素个数。工作方式如下:如果有一字符?包含一指定分隔<br>W?,例如AD2-KP9-JU2-LP-1,之划分成一个数l。用split,指定分隔W及数组名。此<br>例中,命o格式?"AD2-KP9-JU2-LP-1",parts_array,"-"),split然后q回数组下标?q?br>里结果ؓ4?br>q有一个例子用不同的分隔W?br><br>$ awk '{BEGIN {print split("123#456#678", myarray, "#")}'<br>3<br><br>q个例子?splitq回数组myarray的下标数。数lmyarray取值如下:<br><br>Myarray[1]="123"<br>Myarray[2]="456"<br>Myarray[3]="789"<br><br><span style="FONT-WEIGHT: bold">6.sub</span><br>使用sub发现q替换模式的W一ơ出C|。字W串STR包含‘popedpopopill’,执行?br>列sub命osub(/op/,"op",STR)。模式opW一ơ出现时,q行替换操作,q回l果如下Q?br>‘pOPedpopepill’?br>假如grade.txt文g?学生J.Troll的记录有两个g?“目前U别?#8221;?#8220;最高别分”。只<br>改变W一个ؓ29,W二个仍?4不动,操作命o为sub(/26/,"29",$0),只替换第一个出?br>24的位|?br><br>$ awk '$1=="J.Troll" sub(/26/, "29", $0)' grade.txt<br>L.Troll 07/99 4842 Brown-3 12 29 26<br>L.Transley 05/99 4712 Brown-2 12 30 28<br><br><span style="FONT-WEIGHT: bold">7.substr</span><br>substr是一个很有用的函数。它按照起始位置及长度返回字W串的一部分。例子如下:<br><br>$ awk '$1=="L.Transley" {print substr($1, 1,5)}' grade.txt<br>L.Tan<br>上面例子?指定在域1的第一个字W开?q回其前?个字W?br>如果l定长度D大于字符串长度, awk从起始位置q回所有字W,要抽取L.Tansley的姓,只需从第3个字W开始返回长度ؓ7。可以输入长?9,awkq回l果相同?br><br>$ awk '{$1=="L.Transley" {print substr($1, 3,99)}' grade.txt<br>Transley<br><br>substr的另一UŞ式是q回字符串后~或指定位|后面字W。这里需要给出指定字W串及其q回字串的v始位|。例?从文本文件中抽取姓氏,需操作?,q从W三个字W开始:<br><br>$ awk '{print substr($1, 3)}' grade.txt<br>Troll<br>Transley<br><br>q有一个例?在BEGIN部分定义字符?在END部分q回从第t个字W开始抽取的子串?br><br>$ awk '{BEGIN STR="A FEW GOOD MEN"} END {print substr(STR,7)) grade.txt<br>GOOD MEN<br><br><span style="FONT-WEIGHT: bold">8.从shell中向awk传入字符?</span><br>awk脚本大多只有一?其中很少是字W串表示?q一炚w过变量传入awk命o行会变得很容易。现其基本原理讲述一些例子?br>使用道字W串stand-by传入awk,q回光度?br><br>$ echo "Stand-by" | awk '{print length($0)}'<br>8<br><br>讄文g名ؓ一变量,道输出到awk,q回不带扩展名的文g名?br><br>$ STR="mydoc.txt"<br>$ echo $STR | awk '{print subst($STR, 1, 5)}'<br>mydoc<br><br>讄文g名ؓ一变量,道输出到awk,只返回其扩展名?br>$ STR="mydoc.txt"<br>$ echo $STR | awk '{print substr($STR, 7)}'<br>txt<br></span></div> <img src ="http://www.shnenglu.com/prayer/aggbug/110742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/prayer/" target="_blank">Prayer</a> 2010-03-28 17:14 <a href="http://www.shnenglu.com/prayer/archive/2010/03/28/110742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.mir802.cn" target="_blank">69Ʒþþþùۿ</a>| <a href="http://www.ruan8.cn" target="_blank">һaƬþëƬ</a>| <a href="http://www.tspride.cn" target="_blank">һձȾþۺ</a>| <a href="http://www.dafa888da.cn" target="_blank">þþþseɫ͵͵޾Ʒav</a>| <a href="http://www.yufuzhi.cn" target="_blank">޹Ʒþ</a>| <a href="http://www.symedia.com.cn" target="_blank">ƷþþþùA</a>| <a href="http://www.chixuelang.cn" target="_blank">Ļ뾫ƷԴþ </a>| <a href="http://www.caifu580.cn" target="_blank">ɫþþ99Ʒ91</a>| <a href="http://www.kongqueyuhn.cn" target="_blank">ҹþþþüŮӰԺ</a>| <a href="http://www.dittoit800.cn" target="_blank">պAvĻþþ޸ </a>| <a href="http://www.828wlp.cn" target="_blank">þҹҹݺ2022</a>| <a href="http://www.djdnx.cn" target="_blank">պŷۺϾþӰԺDs</a>| <a href="http://www.fanglan-tech.cn" target="_blank">ݲݾþþר</a>| <a href="http://www.elecline.com.cn" target="_blank">þþþžоƷ</a>| <a href="http://www.jsbcly.cn" target="_blank">þѹƷһ</a>| <a href="http://www.t2tt.cn" target="_blank">91Ըߺþþþ</a>| <a href="http://www.lzcoxag.cn" target="_blank">þþþþavѿƬ </a>| <a href="http://www.sh-yd.com.cn" target="_blank">þþþþþþþѾƷ</a>| <a href="http://www.ixibai.cn" target="_blank">Ʒþùһ㽶</a>| <a href="http://www.xinmeige.com.cn" target="_blank">þŮվ</a>| <a href="http://www.leaow.cn" target="_blank">þþþùɫAVѹۿ</a>| <a href="http://www.summernote.cn" target="_blank">ഺþ</a>| <a href="http://www.33759gov.cn" target="_blank">ۺ޾þһƷ</a>| <a href="http://www.stargaze3s.cn" target="_blank">þþþùƷŮӰԺ</a>| <a href="http://www.fanerge.cn" target="_blank">þþþ뾫Ʒ</a>| <a href="http://www.ttprinting.cn" target="_blank">þõӰһ</a>| <a href="http://www.84448.com.cn" target="_blank">þþ91뾫ƷHD</a>| <a href="http://www.vlbx.cn" target="_blank">þùŷպƷ</a>| <a href="http://www.xqt007.cn" target="_blank">޾Ʒþһ</a>| <a href="http://www.jisuvpn.cn" target="_blank">þþþþúݺݶ</a>| <a href="http://www.taoke140.cn" target="_blank">þþþһëþþ</a>| <a href="http://www.www9785.cn" target="_blank">þѾDzݲƷ</a>| <a href="http://www.ssc695.cn" target="_blank">99þþƷëƬѲ</a>| <a href="http://www.604664.cn" target="_blank">Ʒþˬ</a>| <a href="http://www.hlpswa.cn" target="_blank">鶹þþ9ԴƬ</a>| <a href="http://www.ihkd.cn" target="_blank">þseƷһƷ</a>| <a href="http://www.17450.cn" target="_blank">ձɫվWWWþ </a>| <a href="http://www.vxe49.cn" target="_blank">þ91ᆱƷ2020</a>| <a href="http://www.kuhaoma.cn" target="_blank">ŷ޾Ʒ˾þԻӰƬ</a>| <a href="http://www.hrbsyyy.cn" target="_blank">þþƷa޹v岻 </a>| <a href="http://www.hjj9588.cn" target="_blank">ٸۺϾþĻ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>