sed & awk --SED 入门

sed & awk

1.syntax :
 You invoke sed and awk in much the same way. The command-line syntax is:

   command [options] script filename

2.The syntax for invoking sed has two forms:

    sed [-n][-e] `command' file(s)
    sed [-n] -f scriptfile file(s)

    The following options are recognized:

-n :Only print lines specified with the p command or the p flag of the s command.
-e cmd  :Next argument is an editing command. Useful if multiple scripts are specified.
-f file :Next argument is a file containing editing commands.

If the first line of the script is "#n", sed behaves as if -n had been specified.
Frequently used sed scripts are usually invoked from a shell script.

3.You can specify simple editing commands on the command line.

    sed [-e] 'instruction' file
   
    The -e option is necessary only when you supply more than one instruction on the command line.
    It tells sed to interpret the next argument as an instruction. When there is a single instruction, sed is able to make that determination on its own. Let's look at some examples.
 
 NOTE:
 Enclosing the instruction in single quotes is not required in all cases but you should get in the habit of always doing it. The enclosing single quotes prevent the shell from interpreting special characters or spaces found in the editing instruction. (The shell uses spaces to determine individual arguments submitted to a program; characters that are special to the shell are expanded before the command is invoked.)
 (养成用''关闭instruction的习惯,即使当instruction 中没有空格时,''并不是必需的。但是如果有空格一定要用'',以防止shell把特殊符号和空格等当成instruction 来翻译。;)

4. How to separate multiple command ?
There are three ways to specify multiple instructions on the command line:

   1.)Separate instructions with a semicolon.

          sed 's/ MA/, Massachusetts/; s/ PA/, Pennsylvania/' list ==〉simpling : sed 's/ MA/Massachusetts/; s/ PA/Pennsylvania/' list

   2.)Precede each instruction by -e.

          sed -e 's/ MA/, Massachusetts/' -e 's/ PA/, Pennsylvania/' list ==> simpling : sed -e 's/ MA/Massachusetts/' -e 's/ PA/Pennsylvania/' list


   3.)Use the multiline entry capability of the Bourne shell.[1] Press RETURN after entering a single quote and a secondary prompt (>) will be displayed for multiline input.

          [1] These days there are many shells that are compatible with the Bourne shell, and work as described here: ksh, bash, pdksh, and zsh, to name a few.

          $ sed '
          > s/ MA/, Massachusetts/
          > s/ PA/, Pennsylvania/
          > s/ CA/, California/' list
          John Daggett, 341 King Road, Plymouth, Massachusetts
          Alice Ford, 22 East Broadway, Richmond VA
          Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
          Terry Kalkas, 402 Lans Road, Beaver Falls, Pennsylvania
          Eric Adams, 20 Post Road, Sudbury, Massachusetts
          Hubert Sims, 328A Brook Road, Roanoke VA
          Amy Wilde, 334 Bayshore Pkwy, Mountain View, California
          Sal Carpenter, 73 6th Street, Boston, Massachusetts

5.Script Files
  Syntax :sed -f scriptfile file

All the editing commands that we want executed are placed in a file. We follow a convention of creating temporary script files named sedscr.

    $ cat sedscr
    s/ MA/, Massachusetts/
    s/ PA/, Pennsylvania/
    s/ CA/, California/
    s/ VA/, Virginia/
    s/ OK/, Oklahoma/

The following command reads all of the substitution commands in sedscr and applies them to each line in the input file list:

    $ sed -f sedscr list
    John Daggett, 341 King Road, Plymouth, Massachusetts
    Alice Ford, 22 East Broadway, Richmond, Virginia
    Orville Thomas, 11345 Oak Bridge Road, Tulsa, Oklahoma
    Terry Kalkas, 402 Lans Road, Beaver Falls, Pennsylvania
    Eric Adams, 20 Post Road, Sudbury, Massachusetts
    Hubert Sims, 328A Brook Road, Roanoke, Virginia
    Amy Wilde, 334 Bayshore Pkwy, Mountain View, California
    Sal Carpenter, 73 6th Street, Boston, Massachusetts
  
 6.Saving output

   Unless you are redirecting the output of sed to another program, you will want to capture the output in a file.
   This is done by specifying one of the shell's I/O redirection symbols followed by the name of a file:

    $ sed -f sedscr list > newlist

    NOTE:
    Do not redirect the output to the file you are editing or you will clobber it.
    (The ">" redirection operator truncates the file before the shell does anything else.)
    If you want the output file to replace the input file, you can do that as a separate step, using the mv command.
    But first make very sure your editing script has worked properly!
    (不能企图把输出重定向你正在编辑的文件,否则会把原来的文件完全冲掉。如果你实在想这么做,可以分不执行,用mv 命令)
  
  7。Suppressing automatic display of input lines(限制输入文件的显示,即不想显示整个文件,只想显示一部分,用-n 和p 选项,只显示受影响的行)

     The default operation of sed is to output every input line. The -n option suppresses the automatic output.
     When specifying this option, each instruction intended to produce output must contain a print command, p. Look at the following example.

    $ sed -n -e 's/MA/Massachusetts/p' list

    John Daggett, 341 King Road, Plymouth Massachusetts
    Eric Adams, 20 Post Road, Sudbury Massachusetts
    Sal Carpenter, 73 6th Street, Boston Massachusetts

    Compare this output to the first example in this section.
    Here, only the lines that were affected by the command were printed.
 
 8.Summarizes the sed command-line options.
  -e    Editing instruction follows.
  -f    Filename of script follows.
  -n    Suppress automatic output of input lines.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值