Chinaunix首页 | 论坛 | 博客
  • 博客访问: 248066
  • 博文数量: 30
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 411
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-24 09:18
文章分类
文章存档

2015年(22)

2014年(8)

分类: 系统运维

2015-04-18 15:21:04

整理sed实战修改多行配置技巧,以下部分内容转自老男孩老师博客和!

1、在指定行前插入两行内容,分别为oldboy和oldgirl。
提示:被修改的文件内容必须要大于等于2行

  1
.1 sed -i '2 ioldboy\noldgirl'sshd_config
  1.2 在每一行最前面加点东西:
$ sed 's/^/#/g' pets.txt
#This is my cat
#  my cat's name is betty
#This is my dog
#  my dog's name is frank
#This is my fish
#  my fish's name is george
#This is my goat
#  my goat's name is adam
 1.3 在每一行最后面加点东西:
$sed 's/$/ --- /g' pets.txt
This is mycat---
  mycat's name is betty ---
This is my dog ---
  my dog's name is frank ---
This is my fish ---
  my fish's name is george ---
This is my goat ---
  my goat's name is adam ---

顺手介绍一下正则表达式的一些最基本的东西:

  • ^ 表示一行的开头。如:/^#/ 以#开头的匹配。
  • $ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
  • \< 表示词首。 如 \
  • \> 表示词尾。 如 abc\> 表示以 abc 結尾的詞。
  • . 表示任何单个字符。
  • * 表示某个字符出现了0次或多次。
  • [ ] 字符集合。 如:[abc]表示匹配a或b或c,还有[a-zA-Z]表示匹配所有的26个字符。如果其中有^表示反,如[^a]表示非a的字符

正规则表达式是一些很牛的事,比如我们要去掉某html中的tags:

html.txt
1
This is what I meant. Understand?

看看我们的sed命令

1
2
3
4
5
6
7
8
# 如果你这样搞的话,就会有问题
$sed's/<.*>//g'html.txt
 Understand?
 
# 要解决上面的那个问题,就得像下面这样。
# 其中的'[^>]' 指定了除了>的字符重复0次或多次。
$sed 's/<[^>]*>//g' html.txt
This is what I meant. Understand?

我们再来看看指定需要替换的内容:

1
2
3
4
5
6
7
8
9
$sed "3s/my/your/g" pets.txt
This is mycat
  mycat's name is betty
This is your dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam

   1.4 下面的命令只替换第3到第6行的文本。

1
2
3
4
5
6
7
8
9
$sed "3,6s/my/your/g" pets.txt
This is mycat
  mycat's name is betty
This is your dog
  your dog's name is frank
This is your fish
  your fish's name is george
This is my goat
  my goat's name is adam

 

$cat my.txt
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

  1.5只替换每一行的第一个s:

1
2
3
4
5
$sed s/s/S/1' my.txt
ThiS is mycat, mycat's name is betty
ThiS is my dog, my dog's name is frank
ThiS is my fish, my fish's name is george
ThiS is my goat, my goat's name is adam

  1.6只替换每一行的第二个s:

1
2
3
4
5
$sed 's/s/S/2' my.txt
This iS mycat, mycat's name is betty
This iS my dog, my dog's name is frank
This iS my fish, my fish's name is george
This iS my goat, my goat's name is adam

  1.7只替换第一行的第3个以后的s:

1
2
3
4
5
$sed 's/s/S/3g' my.txt
This is mycat, mycat'S name iS betty
This is my dog, my dog'S name iS frank
This is my fiSh, my fiSh'S name iS george
This is my goat, my goat'S name iS adam

  1.8 多个匹配

如果我们需要一次替换多个模式,可参看下面的示例:(第一个模式把第一行到第三行的my替换成your,第二个则把第3行以后的This替换成了That)

1
2
3
4
5
$sed  '1,3s/my/your/g; 3,$s/This/That/g' my.txt
This is yourcat, yourcat's name is betty
This is your dog, your dog's name is frank
That is your fish, your fish's name is george
That is my goat, my goat's name is adam

上面的命令等价于:(注:下面使用的是sed的-e命令行参数)

1
sed -e '1,3s/my/your/g'-e'3,$s/This/That/g' my.txt

我们可以使用&来当做被匹配的变量,然后可以在基本左右加点东西。如下所示:

1
2
3
4
5
$sed 's/my/[&]/g' my.txt
This is [my]cat, [my]cat's name is betty
This is [my] dog, [my] dog's name is frank
This is [my] fish, [my] fish's name is george
This is [my] goat, [my] goat's name is adam

  1.9圆括号匹配

使用圆括号匹配的示例:(圆括号括起来的正则表达式所匹配的字符串会可以当成变量来使用,sed中使用的是\1,\2…)

1
2
3
4
5
$sed 's/This is my \([^,]*\),.*is \(.*\)/\1:\2/g' my.txt
cat:betty
dog:frank
fish:george
goat:adam

上面这个例子中的正则表达式有点复杂,解开如下(去掉转义字符):

正则为:This is my ([^,]*),.*is (.*)
匹配为:This is my (cat),……….is (betty)

然后:\1就是cat,\2就是betty


a命令和i命令

a命令就是append, i命令就是insert,它们是用来添加行的。如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 其中的1i表明,其要在第1行前插入一行(insert)
$sed "1 i This is my monkey, my monkey's name is wukong" my.txt
This is my monkey, my monkey's name is wukong
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
# 其中的1a表明,其要在最后一行后追加一行(append)
$sed "$ a This is my monkey, my monkey's name is wukong" my.txt
This is mycat, mycat's name is betty
This is my monkey, my monkey's name is wukong
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

我们可以运用匹配来添加文本:

1
2
3
4
5
6
7
# 注意其中的/fish/a,这意思是匹配到/fish/后就追加一行
$sed "/fish/a This is my monkey, my monkey's name is wukong" my.txt
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam

下面这个例子是对每一行都挺插入:

1
2
3
4
5
6
7
8
9
$sed "/my/a ----" my.txt
This is mycat, mycat's name is betty
----
This is my dog, my dog's name is frank
----
This is my fish, my fish's name is george
----
This is my goat, my goat's name is adam
----
c命令

c 命令是替换匹配行

1
2
3
4
5
6
7
8
9
10
11
$sed "2 c This is my monkey, my monkey's name is wukong" my.txt
This is mycat, mycat's name is betty
This is my monkey, my monkey's name is wukong
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
$sed "/fish/c This is my monkey, my monkey's name is wukong" my.txt
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam
d命令

删除匹配行

1
2
3
4
5
6
7
8
9
10
11
12
$sed '/fish/d' my.txt
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my goat, my goat's name is adam
 
$sed '2d' my.txt
This is mycat, mycat's name is betty
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
$sed '2,$d' my.txt
This is mycat, mycat's name is betty
p命令

打印命令

你可以把这个命令当成grep式的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 匹配fish并输出,可以看到fish的那一行被打了两遍,
# 这是因为sed处理时会把处理的信息输出
$sed '/fish/p' my.txt
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
 
# 使用n参数就好了
$sed -n '/fish/p' my.txt
This is my fish, my fish's name is george
 
# 从一个模式到另一个模式
$sed -n '/dog/,/fish/p' my.txt
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
 
#从第一行打印到匹配fish成功的那一行
$sed -n '1,/fish/p' my.txt
This is mycat, mycat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
命令打包

第二个是cmd可以是多个,它们可以用分号分开,可以用大括号括起来作为嵌套命令。下面是几个例子:

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
$cat pets.txt
This is mycat
  mycat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam
 
# 对3行到第6行,执行命令/This/d
$sed '3,6 {/This/d}' pets.txt
This is mycat
  mycat's name is betty
  my dog's name is frank
  my fish's name is george
This is my goat
  my goat's name is adam
 
# 对3行到第6行,匹配/This/成功后,再匹配/fish/,成功后执行d命令
$sed '3,6 {/This/{/fish/d}}' pets.txt
This is mycat
  mycat's name is betty
This is my dog
  my dog's name is frank
  my fish's name is george
This is my goat
  my goat's name is adam
 
# 从第一行到最后一行,如果匹配到This,则删除之;如果前面有空格,则去除空格
 $sed '1,${/This/d;s/^ *//g}' pets.txt
my cat's name is betty
my dog's name is frank
my fish's name is george
my goat's name is adam

2、企业实战例子:快速更改SSH配置(一键完成增加若干参数)

2.1 在文件sshd_config 中的第13行前增加或插入内容:


1 [root@oldboyssh]# sed -i '13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no' sshd_config

查看:

1
2
3
4
5
6
 [root@oldboyssh]# sed -n '13,17p' sshd_config
 Port 52113
 PermitRootLogin no
 PermitEmptyPasswords no
 UseDNS no
 GSSAPIAuthentication no

2.2 在文件ifcfg-eth0中的最后一行之后增加或插入内容:

[root@c6-6moban ~]# cat -n /etc/sysconfig/network-scripts/ifcfg-eth0

1 DEVICE=eth0

2 HWADDR=00:0C:29:E1:47:FC

3 TYPE=Ethernet

4 UUID=89886144-a7a8-4c97-8045-808f5c671464

5 ONBOOT=yes

6 NM_CONTROLLED=yes

7 BOOTPROTO=static

[root@c6-6moban ~]#sed -i '$a IPADDR=192.168.0.166\nNETMASK=255.255.255.0\nGATEWAY=192.168.0.1\nDNS1=202.100.64.68\nDNS2=61.178.0.93' /etc/sysconfig/network-scripts/ifcfg-eth0

[root@c6-6moban ~]# cat -n/etc/sysconfig/network-scripts/ifcfg-eth0

1 DEVICE=eth0

2 HWADDR=00:0C:29:E1:47:FC

3 TYPE=Ethernet

4 UUID=89886144-a7a8-4c97-8045-808f5c671464

5 ONBOOT=yes

6 NM_CONTROLLED=yes

7 BOOTPROTO=static

8 IPADDR=192.168.0.166

9  NETMASK=255.255.255.0

10 GATEWAY=192.168.0.1

11 DNS1=202.100.64.68

12 DNS2=61.178.0.93

3、如果是快速修改参数可以用如下方法(企业实战例子)

2
3
4
5
6
7
8
echo"#--------sshConfig修改ssh默认登录端口,禁止root登录----------------------------#"
\cp /etc/ssh/sshd_config/etc/ssh/sshd_config.$(date+"%F"-$RANDOM)
sed -i 's%#Port 22%Port 52113%'/etc/ssh/sshd_config
sed -i 's%#PermitRootLogin yes%PermitRootLogin no%'/etc/ssh/sshd_config
sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%'/etc/ssh/sshd_config
sed -i 's%#UseDNS yes%UseDNS no%'/etc/ssh/sshd_config
sed -i 's%GSSAPIAuthentication yes%GSSAPIAuthentication no%'/etc/ssh/sshd_config
egrep "UseDNS|52113|RootLogin|EmptyPass|GSSAPIAuthentication"/etc/ssh/sshd_config


提示:如果是指定行修改还可以是:

sed -i '2735s/admin_tenant_name=service/admin_tenant_name=admin/' nova.conf
阅读(9913) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~