大家好,又见面了,我是全栈君。
sed博大精深,本文仅持续更新在工作中频繁使用的方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
【SED】
```删除注释 #和空行:
sed '/ *#/d; /^ *$/d'
```统计行数:
sed -n '$=' log /query .log
2546
```删掉空格:
sed 's/\ //g'
```删掉]:
sed 's/]//g'
```匹配行前(i,后a)增加一行:
sed -i '/-A INPUT -j REJECT --reject-with icmp-host-prohibited/i\-A INPUT -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT' rc.firewall.txt
sed -i '/-A INPUT -p icmp -j ACCEPT /a\-A INPUT -p vrrp -j ACCEPT' rc.firewall.txt
OK,现在我们要增加N行,怎么处理呢?
--------------------------
【实例1:调整防火墙,增加NFS相关的端口】
[root@ test ~] # sed -i.backup '/-A INPUT -i lo -j ACCEPT/a\## NFS related \
-A INPUT -p tcp -m state --state NEW -m tcp --dport 111 -j ACCEPT \
-A INPUT -p udp -m state --state NEW -m udp --dport 111 -j ACCEPT \
-A INPUT -p tcp -m state --state NEW -m tcp --dport 662 -j ACCEPT \
-A INPUT -p udp -m state --state NEW -m udp --dport 662 -j ACCEPT \
-A INPUT -p tcp -m state --state NEW -m tcp --dport 875 -j ACCEPT \
-A INPUT -p udp -m state --state NEW -m udp --dport 875 -j ACCEPT \
-A INPUT -p tcp -m state --state NEW -m tcp --dport 892 -j ACCEPT \
-A INPUT -p udp -m state --state NEW -m udp --dport 892 -j ACCEPT \
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2049 -j ACCEPT \
-A INPUT -p udp -m state --state NEW -m udp --dport 32769 -j ACCEPT \
-A INPUT -p tcp -m state --state NEW -m tcp --dport 32803 -j ACCEPT \
# \
' /etc/sysconfig/iptables
[root@ test ~] # service iptables reload
--------------------------
【实例2:调整ntp,增加服务器列表】
先更新一次:
# /usr/sbin/ntpdate stdtime.gov.hk
# sed -i.backup '/server 0.centos.pool.ntp.org iburst/i\## NTP related \
server stdtime.gov.hk iburst \
server 0.asia.pool.ntp.org iburst \
server 1.asia.pool.ntp.org iburst \
server 2.asia.pool.ntp.org iburst \
\
' /etc/ntp .conf
启动ntpd服务:
# service ntpd start
# chkconfig ntpd on
--------------------------
```删掉指定字符
sed 's/insert ignore//g' 1.txt | sed 's/insert//g' | sed 's/\ //g' | sort | uniq -c | sort -nr | more
```正则匹配截取指定字符(例如,截取“down”)
echo '<state>down</state>' | sed -nr 's/<state>(.*)<\/state>/\1/p' | sed 's/\ //g'
```截取内容,根据时间段:
sed -n '/2013:14:00:00/,/2013:14:40:00/p' 0305.log > 1400.1440.log
zcat 2014-06-17-0000-2330_msg.ecqun.com.cn.log.gz | sed -n '/2014:08:00:00/,/2014:10:00:00/p' | more
```替换:
sed '/^SELINUX=/c\SELINUX=disabled' /etc/sysconfig/selinux
echo "http://192.168.0.154:10010/?from=PEK&to=SHA&date=2013-12-02&enddate=2013-12-02&type=text" | sed -r "s/([0-9]{4}-[0-9]{2}-[0-9]{2})/$(date +%F)/g"
echo "20140625232336" | sed -r 's/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5:\6/'
2014-06-25 23:23:36
```替换指定行的内容:
sed '15s/abc/def/'
```打印指定行:
sed -n '2p' 1630.1650.log_7_1
```删掉指定行:
sed 1,8d WAN.IP
```两行变一行:
sed 'N;s/\n//g'
三行变一行:
sed 'N;N;s/\n//g'
注意,下列用法和上面的区别:
sed ':a;N;s/\n//;ta'
sed ':a;...;ta' 的作用:
:a表示:建立一个标签a;ta表示:如果执行成功,则跳转到标签a处继续执行
t label
If a s /// has done a successful substitution since the last input line was read and since the last t or T command , then branch to label; if label is omitted, branch to end of script.
补充一下,得是前面的s /// 替换语句执行成功了,才跳转到标签
```替换内容并备份文件
# cd /etc/yum/pluginconf.d/
# sed -i.backup 's/^enabled=1/enabled=0/' fastestmirror.conf
# ls
fastestmirror.conf fastestmirror.conf.backup security.conf
# diff fastestmirror.conf fastestmirror.conf.backup
2c2
< enabled=0
---
> enabled=1
```说明
“p” command prints the buffer (remember to use -n option with “p”)
“d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle.
P:Print up to the first embedded newline of the current pattern space.
D:If pattern space contains no newline, start a normal new cycle as if the d command was issued. Otherwise, delete text in the pattern space up to the first newline, and restart cycle with the resultant pattern space, without reading a new line of input.
N:Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed exits without processing any more commands.
增加一个换行符,并读入下一行,一起保存到 pattern space 中来处理。(回头再去理解下三行变一行,也就是读取第1行时,同时读取了2,3行,一起给后边的s来替换处理,处理过的这几行后续就跳过了。)
```在输出时,增加一行内容的示例:
1)在第1行前插入一段字符
~] # ls |sed 'N;2iFILENAME'
FILENAME
set_color.sh
t.sh
www
xxx
yyy
zzz
2)在每2行前插入一段字符
~] # ls |sed 'N;iFILENAME'
FILENAME
set_color.sh
t.sh
FILENAME
www
xxx
FILENAME
yyy
zzz
注:上述例子中,结合 'N;' 带来的用法比较特别,不妨仔细琢磨下。
3)在每1行前插入一段字符
~] # ls |sed 'iFILENAME'
FILENAME
set_color.sh
FILENAME
t.sh
FILENAME
www
FILENAME
xxx
FILENAME
yyy
FILENAME
zzz
4)在第3行前插入一段字符
~] # ls |sed '3iFILENAME'
set_color.sh
t.sh
FILENAME
www
xxx
yyy
zzz
|
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107874.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...