zoukankan      html  css  js  c++  java
  • sed 小结

     语法格式1:

    sed option command file

    注意区分 option command,

    option 以-开始

    command 以单?引号包围    ———— 这完全是误会, 我测试好多后发现, 引起很多时候完全可以去掉,只有在当可能引起歧义的时候, 才需要引号

     command 里面的表达式默认格式为/re1/re2/

    不同于tr的基于字符,它是基于字符串的,如

    sed -i s'/vw/in/' temp.txt    就是说vw在作为一个元素来匹配的,然后整体替换为in

    通过-i可以将替换写入temp.txt

    sed  s'/^in.*/va/' temp.txt     表示以in开头,而不是以i或者n开头。

    如果 * 前面不跟一个“.”,则表示对前面的字母的重复。

    sed  s'/in*/va/' temp.txt 

    从文件读入:r命令

    • $ sed '/test/r file' example-----file里的内容被读进来,显示在与test匹配的行后面,如果匹配多行,则file的内容将显示在所有匹配行的下面。

    写入文件:w命令

    • $ sed -n '/test/w file' example-----在example中所有包含test的行都被写入file里。

    追加命令:a命令

    • $ sed '/^test/a\--->this is a example' example<-----'this is a example'被追加到以test开头的行后面,sed要求命令a后面有一个反斜杠。

    command 命令r w a 还是有些不懂

    特别是有些command可以放在re1前面,又可以re2后面。

    总结:

    对应command,格式应该是

    [scope]紧接operation+[pattern]     scope可选,如果没有scope则表示遍历所有行    紧接是说,不能有任何其他字符,包括空格(引号除外)    pattern可选,对某些操作是必须的

    scope如何表示?

    用行号表示:

    1,3 第一到第三行

    2,%第二到文尾

    用正则表达式regx表示:

    /hello/  所有包含hello的行

    /^hello/   所有以hello开头的行

    。。。

    operation 有哪些:  

    s 替换 格式固定为    s/reg1/reg2/

    d删除  

    a 追加 格式为 a sthnew 或者 ’a sthnew’

    c 整行替换

    w  

    r  

    i插入

    = 列出行号

    什么时候需要引号?

    这个时候:

    sed /temp/a sthnew sedtest.txt   不需要

    如果sed /temp/’a sthnew‘ sedtest.txt  则需要, 因为a 变成了a

    sed /temp/'w aa.txt' sedtest.txt  因为w aa.txt之间有空格  对于r参数也一样

    参见

    http://www.cnblogs.com/erichhuang/archive/2012/03/13/2394108.html

    http://www.cnblogs.com/lovemo1314/archive/2011/09/30/2196841.html

  • 相关阅读:
    Eclipse安装Hadoop插件
    (转)Ubuntu14.0.4中hadoop2.4.0伪分布模式配置
    Hadoop--DataNode无法启动
    启动与关闭hadoop
    hadoop中执行命令时发生错误
    strings命令
    Deriving data from ElasticSearch Engine
    elasticsearch data importing
    reading words in your computer and changing to female voice, linux festival text2wave saving wav files
    DDNS client on a Linux machine
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/3643646.html
Copyright © 2011-2022 走看看