sed
sed
sed(stream editer) 是流数据编辑器 ,每次读取一行数据到模式空间处理过滤
基本语法:sed [OPTION]... {script-only-if-no-other-script} [input-file]...
【基本语法】
【OPTION】
-n, --quiet, --silent 静默模式
sed -n '/root/p' /etc/passwd
-f script-file, --file=script-file 添加文件给sed运行
[root@allinone ~]# cat 3
/root/p
[root@allinone ~]# sed -n -f 3 /etc/passwd
-r, --regexp-extended 支持扩展正则
-i 直接修改文件
-e script, --expression=script 添加脚本给sed执行
匹配参数
s/regexp/replacement/
| 参数 | 语法 | 释义 |
|---|---|---|
| x | sed -n 3p /etc/passwd |
匹配第x行 |
| x,y | sed -n 1,3p /etc/passwd |
匹配x到y行 |
| x,y! | 不匹配x到y行 | |
| /pattern/ | sed -n /^root/p /etc/passwd |
匹配条件 |
| /pattern1/,/pattern2/ | printf "%s\n" {a..z}|sed -n '/a/,/c/p' |
匹配两个模式中间的行 |
| /pattern/,y | printf "%s\n" {a..z}|sed -n '/X/,26p' |
匹配从条件到第y行 |
| x,/pattern/ | printf "%s\n" {a..z}|sed -n '20,/x/p' |
匹配从第y行到匹配位置 |
动作参数
| 参数 | 语法 | 释义 |
|---|---|---|
| s (substitute) | sed '[/address/] s/source/dest/flags filename |
内容替换 |
sed 's/root/newroot/g' /etc/passwd |
全局替换 | |
sed 's/root/newroot/w netpasswd' /etc/passwd |
写入到文件 | |
sed 's/root/newroot/2' /etc/passwd |
替换改行的第二个匹配位 | |
| y(transform) | sed '[address] y/source/dest/ filename |
|
| d(deltete) | sed '2,$d' filename |
|
| i(insert) | sed '$ i\# this is a test line ' /etc/passwd |
插入行 |
| a(add) | sed '$ a\# this is a test line ' /etc/passwd |
追加行 |
| c(chang) | sed '$ c\# this is a test line ' /etc/passwd |
替换行 |
| w(write) | sed '$ w netpasswd' /etc/passwd |
写入到新文件 |
| r(read) | sed '1r netpasswd' /etc/passwd |
从文件读入 |
| n(next) | sed -n '/^root/{n;p}' /etc/passwd |
处理匹配的下一行 |
| = | sed '{=}' /etc/passwd |
打印行号 |
| N(next) | 将数据流的下一行加进来,创建一个多行数组 | |
| D(delete) | 删除数组的第一行 | |
| P(print) | 打印数组的第一行 |
举例:
sed -n -e '/^root/p;/nologin$/p' /etc/passwd
# 一个 sed 替换的事例
for n in seq `1 80`
do
sed -i $ns/$n/$(($n+20))/2 showresources|sed -i $ns/$n/$(($n+20))/3|sed -i $ns/$n/$(($n+20))/5
done
#在文件所有行结尾添加";"
sed 's/$/;/g' /etc/hosts
sed -r -i -e '/^nohup/{p;:a;N;$!ba;d}' /opt/src/dubbo-monitor/dubbo-monitor-simple/bin/start.sh && \
sed -r -i -e "s%^nohup(.*)%exec \1%g" /opt/src/dubbo-monitor/dubbo-monitor-simple/bin/start.sh
# 打印
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -n '/A/p'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -n '/^A/p'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -n '/^A/{p}'
# 删除
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed '/^A/{d}'
# 替换
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed '{s/AAAA/1111/1}'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -e 's/AAAA/1111/g;s/BBBB/2222/g'
# 模式空间
n N
p P
d D
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -n '/^A/{n;p}'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -n '/^A/{N;p}'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed -n '/^A/{N;P}'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed '/^A/{N;d}'
echo -e "AAAA\nBBBB\nCCCC\nDDDA"|sed '/^A/{N;D}'
# 保持空间
默认文本一行行加载在模式空间中 patter space,可以使用一下命令转换模式空间和保持空间的内容
h 模式空间--保持空间,清空保持空间的内容
H 模式空间--保持空间,追加到保持空间
g 保持空间---模式空间,清空模式空间的内容
G 保持空间---模式空间,追加到模式空间
x 保持空间和模式空间的内容互换
实现tac的文件倒叙功能
方法一:
sed -n '{1!G;h;$p}'

通过上面过程发现4.1 执行h命令有些多余,因此把命令修改为 echo -e "AAAA\n\BBBB\n\CCCC\n\DDDD"|sed -n '{1!G;h;$p}'
sed '{1!G;h;$!d}'

通过上面过程发现4.1 执行h命令有些多余,因此把命令修改为 echo -e "AAAA\n\BBBB\n\CCCC\n\DDDD"|sed -n '{1!G;$h;$p}'
浙公网安备 33010602011771号