免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4057 | 回复: 12
打印 上一主题 下一主题

关于SED命令的一些埋解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-27 23:16 |只看该作者 |倒序浏览
本帖最后由 goride 于 2010-02-28 23:13 编辑

不管是新手老手,都可以看看zlei1写的  sed总结

看了他的我下面的东西就显得凌乱了。

有1点要指明 关于地址范围:    1,3  和 1~3 是不同的,而zlei1的文章他们之间用了或,给人感觉是可以替换

                             1,3 从1行到3行 包括 1 2 3
                             1~3  从第1行开始,每隔3行后的行,包括1 4 7 .....



今天学sed,看了man,也在看info。有了新的认识,写出来跟大家一起交流


sed  OPTIONS  [SCRIPT]  [INPUTFILE]


SED 掌管着2个数据缓冲区,active_pattern_space(就叫pattern space吧),和auxiliary_hold_space(hold space).初始状态下他们没放东西,是空的。
    这两个区在sed的 g,G,h,H,x命令操作的时候要用到。现在我还没懂



sed 运行大概步骤:
     首先从输入读取一行数据,把行尾的换行符(newline)去掉,然后就把它(是整行,我开始理解以为只有正则式部分)放到pattern space里面,再执行sed 命令(模式匹配处理),处理完后(全部SCRIPT),如果sed没有-n参数,就会加回换行符直接打印出pattern space 里的所有内容。
    接下来再读第二行输入,继续处理。

论坛徽章:
0
2 [报告]
发表于 2010-02-27 23:32 |只看该作者
对于SCRIPT,相当于主程序。一般都是这样的格式 地址+操作命令(“+”实际上没有,这里是方便看清楚)。

这个和awk有点像吧。

地址可以是 行号,如第2行打印 ,就是‘2p'
    也可以是“步进”,如第2行和后面每隔5行后的行打印,‘2~5p’
    也可以是正则表达式 /regexp/

操作比较多,简单的有p打印,d删除行,还有1楼说的g,G,h,H,x等

可以有几组 地址+操作  之间用;分隔开。也可以用 -e '地址操作'  -e '地址操作'

论坛徽章:
0
3 [报告]
发表于 2010-02-27 23:38 |只看该作者
请教一下楼主,

怎样把”首先从输入读取一行数据“ 的这一部分,完全作为文本处理?

比如这个命令
sed -i "2s,.*,$line2," ./test.txt

$line2里面含有 & 或者 \ 就无法作为文本处理,即使加了\

论坛徽章:
0
4 [报告]
发表于 2010-02-27 23:54 |只看该作者
不好意思,sed我刚在学,上面只是自己看其他一些教材感觉模糊,看了info理解,所以记录下来。

其实我很多操作还不懂呢。还请各位大人多多指点

论坛徽章:
0
5 [报告]
发表于 2010-02-28 07:17 |只看该作者
回复 3# 洋芋环


    可以这样~
    #!/bin/bash
    #1.sh
    sed 's/'"$1"'/love/'  yourfile


   sh  1.sh  "\\\abc"
  多加几个\\\就行了

论坛徽章:
0
6 [报告]
发表于 2010-02-28 08:17 |只看该作者
关于 h,H,g,G,x

h  replace the contents of the hold space with the contents of pattern space
    用pattern space内容置换hold space中的内容,就是 copy  "pattern space"  to "hold space"

H  Append a newline to the contents of the hold space ,the append the contents of the
    pattern space to that of hold space
    先加一“换行符”到hold space 后面,再把pattern space 加到hold space 后。
  
g  copy "hold space" to "pattern space"  ,和h反了下

G  先加一“换行符”到pattern space 后面,再把hold space 加到pattern  space 后

x  Exchange the contents of the hold and pattern spaces
    互换"hold spaces"  和"pattern space "内容。

不知道上面的翻译准确,请各位指点。。

论坛徽章:
0
7 [报告]
发表于 2010-02-28 08:19 |只看该作者
不错~LZ英语不错啊

论坛徽章:
0
8 [报告]
发表于 2010-02-28 08:32 |只看该作者
本帖最后由 goride 于 2010-02-28 10:38 编辑

现在有1 问
  1. sed '1!G;h;$!d'  inputfile
复制代码
这个怎么理解。

找个实际例子,inputfile 有2行
line 1
line 2

   命令分步拆解:

            pattern space    hold space     执行命令        pattern space       hold space      output (屏幕输出)
  读 第一行       line 1                     空     1!G  (不匹配)     line 1                       空             还有命令,继续执行
                                                            h                    line 1                      line 1                 同上
                                                            $!d   (匹配)                                      line 1                 无输出

读 第二行     line 2                   line1         1!G                  line 2\n line1        line 1               继续执行      
                                                           h                    line 2\n line 1         line 2\n line 1       继续执行
                                                       $!d                 line2\n line1            line 2\n line 1           line 2\n line 1

所以最终结果 输出一次 :    line 2
                                          line 1                                   


明白了。

论坛徽章:
0
9 [报告]
发表于 2010-02-28 09:40 |只看该作者
本帖最后由 goride 于 2010-02-28 10:53 编辑

是不是 1!G;h;$!d 全部执行完,才做一次输出呢??


上面猜测是正确的,info sed 里How `sed' Works有说明,只是我当时没理解。

原文:
`sed' operates by performing the following cycle on each lines of
input: first, `sed' reads one line from the input stream, removes any
trailing newline, and places it in the pattern space.  Then commands
are executed; each command can have an address associated to it:
addresses are a kind of condition code, and a command is only executed
if the condition is verified before the command is to be executed.

   When the end of the script is reached, unless the `-n' option is in
use, the contents of pattern space are printed out to the output
stream, adding back the trailing newline if it was removed.(1) Then the
next cycle starts for the next input line.

论坛徽章:
0
10 [报告]
发表于 2010-02-28 09:42 |只看该作者
好好看看那个$!d。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP