sed 基础用法

#sed [-nefr] 动作[n1,[n2]]function
#sed [options] "script" FILE1 .......

选项:
-n #silent,只有经过sed特殊处理的那一行(或动作)才会被列出来。

-e #直接在指令模式上进行sed编辑 Editing instruction follows.
#add the script to the commands to be executed

-f #直接将sed的动作写在一个档案内,
-f filename则可以执行filename的sed动作。

-i #直接编辑文件
example: sed -i 's/127.0.0.1/0.0.0.0/' my.ini

-r # 支持扩展正则表达式


sed 地址定界
start_line, end_line example 1,10 sed '1,10action'
start_line, /pattern/ sed '3,/^#/
/pattern/

编辑动作说明:

.a \text 新增,符合条件内容的后面
example: sed '/^#/ a \The commend line.\nThe second line.' /etc/fstab
.i \text 新增,符合条件内容的前面。
.r /path/to/somefile: 在符合条件的位置读入指定的文件
example: sed '/^proc/r /etc/issue' /etc/fstab

.w /path/to/somefile: 将符合条件的行保存至指定的文件中

.c \text 取代,c的后面可以接字符串
example: sed '/^#/ c \comment' /etc/fstab

.d 删除
example sed '/^#/!d' /etc/fstab
.p 打印
example: sed -n '5,7p'

.s 取代 s@要查找的内容@替换为的内容@:

example: which ls | grep -v "^alias" | sed 's@^[[:space:]]*@@g'
which ls | sed '/^alias/d;s@^[[:space:]]*@@g'
d;s 表示先删除alias再替换

which ls | grep -v "^alias" | sed 's/^[[:space:]]*//g'

= 显示匹配到的行的行号


你可能感兴趣的:(linux,sed,基础用法)