一、工作中遇到sed添加及修改在nginx末尾添加太麻烦了

需求:随意查找添加一条以前不存在的内容加到"}"前一行

实现:

#!/usr/bin/env python
# coding=utf-8
import sys,os
from optparse import OptionParser parse = OptionParser()
parse.add_option("-c", "--context", help="add one line contexts",default="xxxhtxxxtps_status")
parse.add_option("-f", "--file", help="modify file",default="/006_eleallproject/002_surveypro/001_nginxdevops/002_camel-agent-deploy/camel-agent-deploy2.0/nginx.conf") (options, args) = parse.parse_args()
content = options.context
file = os.path.realpath(options.file)
#content="x$https_status"
#file='/006_eleallproject/002_surveypro/001_nginxdevops/002_camel-agent-deploy/camel-agent-deploy2.0/nginx.conf'
print options _list_content=[]
m = 0
count = 0
#获取"}"的位置
try:
fh = open(file, 'rb')
for n in fh.readlines():
m += 1
if (n.find('}') >= 0):
global count
count = m
_list_content.append(n)
else:
_list_content.append(n)
finally:
fh.close() #在"}"位置前添加内容
n = 0
ngconf = open(file, 'rb')
for s in ngconf.readlines():
n += 1
if s.find('}') >= 0 and n >= count-1:
#在以前的列表中有任何可以找到的内容都不会添加
result = any([str(line.strip()).find(content) >=0 for line in _list_content])
if not result:
_list_content.insert(count-1, content+"\n")
ngconf.close() #写入文件
end_nginx_f = open(file, 'wb')
end_nginx_f.writelines(_list_content)
end_nginx_f.close()

  

最新文章

  1. 高通vuforia+Unity3D 制作ar app
  2. Cookie实现商品浏览记录--方式一:Java实现
  3. 【Android】Android应用安装失败及无法打开
  4. PHP 实现 一致性哈希 算法(转的)
  5. acdream 1409 Musical 状压DP
  6. IT版孔乙己(转)
  7. ServletContext对象
  8. poj 1274 The Perfect Stall【匈牙利算法模板题】
  9. <object>元素+svg 绘制图片
  10. 使用SQLQuery 在Hibernate中使用sql语句
  11. Shim 与 Polyfill
  12. 网络协议 15 - P2P 协议:小种子大学问
  13. 45)django-分页实现
  14. Linux下修改环境变量,不小心改错,找不到命令解决办法
  15. HDU 2586 How far away(dfs+邻接表)
  16. 刘志梅2017710101152.《面向对象程序设计(java)》第一周学习总结
  17. Java内存泄漏定位
  18. sublime3 常用快捷键
  19. JSON中JObject和JArray的修改
  20. hibernate Validator 6.X 的学习,bean的约束(主要包括的是容器元素的验证)

热门文章

  1. 4:Python的while循环
  2. K8S 容器的资源需求、资源限制
  3. 基于Aspectj 注解实现 spring AOP
  4. iOS--------获取当前连接的WiFi以及IP地址
  5. iOS------获取当前时间和当前时间戳
  6. <解决方法>Centos安装使用Chromedriver
  7. spring Boot 出现:org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
  8. Fiddler做代理服务器时添加X-Forwarder-For转发真实客户端ip
  9. java后台打开浏览器代码
  10. ros中自定义消息 报错 ImportError: No module named em