Linux文本处理三剑客之sed

sed

sed - stream editor for filtering and transforming text
sed是一款面向字符流的非交互式编辑器,其命令参数说明如下:
sed [选项] [动作]

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

sed命令的动作

除了上述的命令参数外,sed还提供了一系列命令动作进行编辑。

a :新增, a 的后面可以接字串,当前行的下一行插入;
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行;注意这里是取代整行,这是和s的区别
d :删除,如'3d', sed  '2,5d'
i :插入, i 的后面可以接字串,当前行的上一行插入;
p :打印,通常 p 会与参数 sed -n 一起运行, 如 sed -n '5,7p';
s :替换,通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g

所有命令中使用-i选项会修改原始文件,特别注意。
所有的操作都可以限定到某行,某个行区间,符合某个pattern的行等

删除操作 d

删除第三行的内容,sed '3d' filename
删除以good开头的行,sed '/^good/d' filename
删除以数字开头的行,sed '/^[0-9]/d' filename
删除第三行到最后一行的内容,sed '3,$d' filename

添加操作 a/i

在第三行后增加一句话,sed '3a hello world' filename
在第三行前增加一句话,sed '3i hello world' filename
增加多行内容则使用\隔开不同行的内容

$ sed '2a Drink tea or ......\
> drink beer ?' filename

显示操作 p

显示部分行sed '3,5p',配合-n选项和|命令可以选择某些内容进行操作,如sed -n '3,5p' | grep xxx

关键词匹配及操作 /pattern/

显示匹配关键词的行,sed '/pattern/p' filename,这个命令首先输出文件内容,如果找到匹配的行,输出匹配行的内容。如果只想输出匹配行的话,加上-n选项即可。

删除匹配关键词的行,sed '/pattern/d' filename

数据搜索及替换sed 's/oldstring/newstring/g' filename这里的g会替换文本行中所有的匹配关键词
将文件中所有以’.‘结尾的标点符号替换成’!’,sed -i 's/\.$/\!/g' filename这里的g会替换文本行中所有的匹配关键词

数据搜索及命令操作,执行后面花括号中的一组命令,每个命令之间用分号分隔,q是退出的意思。
sed '/pattern/{s/oldstring/newstring/g; q}' filename

sed命令级联

可以用-e选项级联sed命令
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'

sed正则元字符

前面说过sed命令可以使用地址范围、pattern等对某些符合条件的行进行相应的操作。
sed中的正则语法和其他命令的正则语法有些许不同,下面列一下sed里常用的正则
元字符。

$ 行尾
^ 行首
[a-z], [0-9] 字符范围限定
[^] 除了字符集中的字符以外的字符
很多符号使用的时候都需要转义字符`\`
. 任意字符
* 零个或多个
\+ 一次或多次
\? 零次或一次
\| 或语法

你可能感兴趣的:(Linux使用)