当前位置: 技术问答>linux和unix
shell文本删除问题求助
来源: 互联网 发布时间:2017-01-01
本文导语: 请问如何使用shell脚本删除samba的配置文件中的一个用户子项啊? samba的配置文件信息如下所示: [global] workgroup = MYGROUP security = user [user1] path = /home/user1 public = n...
请问如何使用shell脚本删除samba的配置文件中的一个用户子项啊? samba的配置文件信息如下所示:
[global]
workgroup = MYGROUP
security = user
[user1]
path = /home/user1
public = no
writeable = yes
browseable = yes
valid users = user1
[user2]
path = /home/user2
public = no
writeable = yes
browseable = yes
valid users = user2
[user3]
path = /home/user3
public = no
writeable = yes
browseable = yes
valid users = user3
现在需要写一个shell脚本userdel.sh, 传递一个参数, 譬如参数为user2, 执行sh userdel user2之后, 将从上面的samba配置文件中把user2的信息删除, 删除后的内容变为:
[global]
workgroup = MYGROUP
security = user
[user1]
path = /home/user1
public = no
writeable = yes
browseable = yes
valid users = user1
[user3]
path = /home/user3
public = no
writeable = yes
browseable = yes
valid users = user3
请问这个shell脚本该怎么写啊? 哪位高手可以帮帮忙? 万分感激!!
[global]
workgroup = MYGROUP
security = user
[user1]
path = /home/user1
public = no
writeable = yes
browseable = yes
valid users = user1
[user2]
path = /home/user2
public = no
writeable = yes
browseable = yes
valid users = user2
[user3]
path = /home/user3
public = no
writeable = yes
browseable = yes
valid users = user3
现在需要写一个shell脚本userdel.sh, 传递一个参数, 譬如参数为user2, 执行sh userdel user2之后, 将从上面的samba配置文件中把user2的信息删除, 删除后的内容变为:
[global]
workgroup = MYGROUP
security = user
[user1]
path = /home/user1
public = no
writeable = yes
browseable = yes
valid users = user1
[user3]
path = /home/user3
public = no
writeable = yes
browseable = yes
valid users = user3
请问这个shell脚本该怎么写啊? 哪位高手可以帮帮忙? 万分感激!!
|
grep -n userN,获取所在的行号,假如获取的是10行,再用删除行sed -n '10,15p' 配置文件.ini
|
错了,删除是sed -n '10,15d'
|
这样用:
./test.sh samba.old user2 删除user2这部分,新的配置在samba.new中
./test.sh samba.old user2 删除user2这部分,新的配置在samba.new中
#!/bin/sh
beg=`grep "$2" -n $1 | cut -d":" -f1 | head -1`
((end=beg+5))
sed "${beg},${end}d" $1 > samba.new
|
试了下,这样都可以
(1) sed ''$beg','$end'd' $1 > samba.new 对变量$var再加一对单引号,sed最外层是一对单引号
(2) sed "${beg},${end}d" $1 > samba.new 双引号会解释${var},sed最外层是一对双引号
|
awk -v value=$para 'BEGIN{RS="";ORS="nn"}{if($1!~value)print }' file > tmp
mv tmp file
重定向一下
RS和ORS分别是输入和输出的记录分隔符,简单说以前是按n分隔记录的,RS=""以后,相当于把空行作为了分隔符,然后把匹配value的段落给屏蔽输出
这个需要先学习下awk,否则不好理解
mv tmp file
重定向一下
RS和ORS分别是输入和输出的记录分隔符,简单说以前是按n分隔记录的,RS=""以后,相当于把空行作为了分隔符,然后把匹配value的段落给屏蔽输出
这个需要先学习下awk,否则不好理解