免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2279 | 回复: 6
打印 上一主题 下一主题

求一正则表达式或其他方法来处理此文本~谢谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-09 17:09 |只看该作者 |倒序浏览
将一文件中的
ca::ctrlaltdel:/sbin/shutdown -r -t 4 now
变成
#ca::ctrlaltdel:/sbin/shutdown -r -t 4 now
也就是此行前面加#号

我现在用的正则,直接将此行进行替换,测试一些没有写出来 请帮助我一下~谢谢
  1. import sys,os
  2. import re
  3. import shutil,time
  4. def SEC_SUSE_PWD_07():
  5.     #获得当前时间
  6.     t = time.localtime()
  7.     today = time.strftime('%Y-%m-%d', t)
  8.     sshd_config_newname = ('/etc/inittab'+'.'+today)
  9.     shutil.copyfile('/etc/inittab',sshd_config_newname)
  10.     f_sshd_config = open('/etc/inittab','r')
  11.     file_ssh_read = f_sshd_config.read()
  12.     sub = re.sub(r'这里的正则表达式','#ca::ctrlaltdel:/sbin/shutdown -r -t 4 now',file_ssh_read)
  13.     f_sshd_config.close()
  14.     file_sshd_ok = open('/etc/inittab','w')
  15.     file_sshd_ok.write(sub)
  16.     file_sshd_ok.close()
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-11-10 11:05 |只看该作者
re.sub('^ca:[^#]ca::', '#ca::', file_ssh_read, re.S)

论坛徽章:
0
3 [报告]
发表于 2010-11-10 11:06 |只看该作者
  1. re.sub('^ca::|[^#]ca::', '#ca::', file_ssh_read, re.S)
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-11-10 13:24 |只看该作者
  1. import os
  2. os.popen(r'sed -i "s/^\(ca::ctrlaltdel:\)/#\1/" /etc/inittab').close()
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-11-10 20:19 |只看该作者
感谢2位大牛~

论坛徽章:
0
6 [报告]
发表于 2010-11-10 21:34 |只看该作者
我也来现下丑,看到前面那些精短的回答,我汗颜
下面的代码功能是:
      可以注释指定目录及其子目录中所有中的‘ca::ctrlaltdel:/sbin/shutdown -r -t 4 now’
      或指定文件中的‘ca::ctrlaltdel:/sbin/shutdown -r -t 4 now’
稍加修改,可以进行批量文件处理、处理指定目录or文件。。。。
  1. #!/usr/bin/env python
  2. #Filename: comment.py

  3. import os, sys

  4. #-------------------------------------------------------------
  5. def usage():
  6.     print u'''
  7.     help: comment.py <filename | dirname>

  8.     [dirname]: Option, select a directory to operate
  9.     [filename]: Option, select a file to operate

  10.     Example: python comment.py /home/saylor/test
  11.     '''
  12. #--------------------------------------------------------------
  13. def commentFile(src, fileList):
  14.     '''
  15.     description: comment files
  16.     param src: Operate file name
  17.     '''
  18.     #if file exist?
  19.     if not os.path.exists(src):
  20.         print 'Error: file - %s doesn\'t exist.'% src
  21.         return False
  22.     if os.path.islink(src):
  23.         print 'Error: file - %s is just a link, will not handle it.'
  24.         return False
  25.     filetype = (os.path.splitext(src))[1]
  26.     if not filetype in ['.c','.h']:
  27.         return False
  28.     try:
  29.         if not os.access(src, os.W_OK):
  30.             os.chmod(src, 0664)
  31.     except:
  32.         print 'Error: you can not chang %s\'s mode.'% src
  33.     try:
  34.         inputf = open(src, 'r')
  35.         outputfilename = src + '.tmp'
  36.         outputf = open(outputfilename, 'w')
  37.         str = 'ca::ctrlaltdel:/sbin/shutdown -r -t 4 now'
  38.         for eachline in inputf:
  39.             if eachline.find('::ctrlaltdel:/sbin/shutdown -r -t 4 now') > 0:
  40.                 print 'run here'
  41.                 eachline = eachline.replace('ca::ctrlaltdel:/sbin/shutdown -r -t 4 now','#ca::ctrlaltdel:/sbin/shutdown -r -t 4 now')
  42.             outputf.write(eachline)
  43.         inputf.close()
  44.         outputf.close()
  45.         os.rename(outputfilename, src)
  46.         fileList.append(src)
  47.     except:
  48.         print 'Error: unexcept error.'
  49.         inputf.close()
  50.         outputf.close()
  51.     return True

  52. #--------------------------------------------------------------
  53. def commentDir(src, fileList):
  54.     '''
  55.     description:
  56.          comment files in src(dir)
  57.     param src:
  58.          operate files in src(dir)
  59.     '''
  60.     #if dir exist?
  61.     if not os.path.exists(src):
  62.         print 'Error: dir - %s is not exist.'%s (src)
  63.         return False
  64.     filelists = os.listdir(src)
  65.     for eachfile in filelists:
  66.         eachfile = src + '/' +eachfile
  67.         if os.path.isdir(eachfile):
  68.             commentDir(eachfile, fileList)
  69.         elif os.path.isfile(eachfile):
  70.             commentFile(eachfile, fileList)
  71.     return True

  72. #--------------------------------------------------------------
  73. def main():
  74.     if len(sys.argv) < 2:
  75.         usage()
  76.         sys.exit(1)
  77.     src = sys.argv[1]
  78.     if os.path.isdir(src):
  79.         dire = os.path.abspath(src)
  80.         dirFlag = True
  81.     elif os.path.isfile(src):
  82.         fl = os.path.abspath(src)
  83.         dirFlag = False
  84.     else:
  85.         print 'Error'
  86.     fileList = []
  87.     if dirFlag:
  88.         commentDir(dire, fileList)
  89.     else:
  90.         commentFile(fl, fileList)
  91.     if fileList:
  92.         print 'Successful handle file: ...'
  93.         for eachfile in fileList:
  94.             print eachfile
  95.     print 'Done'
  96.     return True

  97. #--------------------------------------------------------------
  98. if __name__ == '__main__':
  99.     main()
复制代码

论坛徽章:
0
7 [报告]
发表于 2010-11-11 11:31 |只看该作者
回复 6# 吴秦


    我看懂你写的了~回去测试下~十分感谢~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP