【问题标题】:using python, how do we delete the auth_user column from the proxy log file?使用 python,我们如何从代理日志文件中删除 auth_user 列?
【发布时间】:2015-06-09 20:41:11
【问题描述】:

我在一个文件夹中有数百个代理日志文件,我想从所有日志文件中删除auth_user 列并将它们输出到另一个文件夹。

auth_user 列用双引号括起来。最大的问题是我不能使用空格字符作为文本分隔符,因为有些日志文件在timestampauth_user 之间没有空格。我尝试使用双引号作为文本分隔符,但这会导致一些奇怪的结果,因为有时双引号对之间没有任何内容。

到目前为止我所拥有的:

for src_name in glob.glob(os.path.join(source_dir, '*.log')):
    base = os.path.basename(src_name)
    dest_name = os.path.join(dest_dir,base)
    with open(src_name, 'rb') as infile:
        with open(dest_name, 'w') as outfile:
             reader = csv.reader(infile, delimiter='"')
             writer = csv.writer(outfile, delimiter='"')
             for row in reader:
                 row[1] = ''
                 writer.writerow(row)

日志文件如下(time_stamp"auth_user"src_ip):

[21/Apr/2013:00:00:00 -0300]"cn=john smith,ou=central,ou=microsoft,o=com" 192.168.2.5
[21/Apr/2013:00:00:01 -0400]"jsmith" 192.168.4.5
[21/Apr/2013:00:00:01 -0400]"" 192.168.15.5
[22/Apr/2013:00:00:01 -0400]"" 192.168.4.5
[22/Apr/2013:00:00:01 -0400]"jkenndy" 192.168.14.5

我想改成这个(time_stampsrc_ip):

[21/Apr/2013:00:00:00 -0300] 192.168.2.5
[21/Apr/2013:00:00:01 -0400] 192.168.4.5
[21/Apr/2013:00:00:01 -0400] 192.168.15.5
[22/Apr/2013:00:00:01 -0400] 192.168.4.5
[22/Apr/2013:00:00:01 -0400] 192.168.14.5

【问题讨论】:

  • auth_user 行是否开始时没有时间戳? auth_user 之后的行中的空引号怎么办?您是否只想让数据从username 行开始?如果这两行位于文件的开头,则可以在读取文件时从第三行开始忽略它们。
  • 嗨,欢迎来到 StackOverflow。 请不要用粗体写问题,因为这感觉像是在对我们大喊大叫。 :)
  • #time_stamp "auth_user"
  • 在下面试试我的答案。我认为它可以满足您的需求。
  • 一个典型的代理日志有 15 列,而 stevieb 的解决方案就像一个魅力。谢谢!

标签: python text delimiter


【解决方案1】:

假设每个文件都有结构:

#[some timestamp here]"auth_user"
#[21/Apr/2013:00:00:00 -0300]""
#[21/Apr/2013:00:00:00 -0300]"username"
#[21/Apr/2013:00:00:00 -0300]"machine$"
#[21/Apr/2013:00:00:00 -0300]"cn=john smith,ou=central,ou=microsoft,o=com"
#[21/Apr/2013:00:00:01 -0400]"jsmith"
#[21/Apr/2013:00:00:01 -0400]""
#[21/Apr/2013:00:00:01 -0400]""

假设前两行需要跳过:

#!/usr/bin/env python3
# coding: utf-8

with open('file.log') as f:
    for line_number, line in enumerate(f):
        # line_number starts at zero, skip both lines at beginning of file
        if line_number > 1:
            # process file here, replace print statement with appropriate code
            print(line)

【讨论】:

    【解决方案2】:

    除了使用 CSV,您可以正常打开文件并使用正则表达式吗?以下将删除 auth_user 列,无论时间戳后是否有空格,或者引号内是否有任何内容:

    import re
    
    with open('in.txt', 'r') as fh:
        for line in fh:
            line = re.sub(r'(?:(?<=\d{4}])|(?<=#time_stamp))\s*".*?"', '', line)
            print(line)
    

    输入:

    #time_stamp "auth_user" src_ip 
    [21/Apr/2013:00:00:00 -0300]"cn=johnsmith,ou=central,ou=microsoft,o=com" 192.168.2.5
    [21/Apr/2013:00:00:01 -0400]"jsmith" 192.168.4.5
    [21/Apr/2013:00:00:01 -0400]"" 192.168.15.5
    [22/Apr/2013:00:00:01 -0400]"" 192.168.4.5
    [22/Apr/2013:00:00:01 -0400]"jkenndy" 192.168.14.5
    

    输出:

    #time_stamp src_ip
    [21/Apr/2013:00:00:00 -0300] 192.168.2.5
    [21/Apr/2013:00:00:01 -0400] 192.168.4.5
    [21/Apr/2013:00:00:01 -0400] 192.168.15.5
    [22/Apr/2013:00:00:01 -0400] 192.168.4.5
    [22/Apr/2013:00:00:01 -0400] 192.168.14.5
    

    【讨论】:

      【解决方案3】:

      我会使用re正则表达式模块将日志文件的每一行分成三组,然后将第一组和第三组写入输出文件:

      import glob
      import os
      import re
      
      pattern = re.compile(r'''(\[.+\])(".*")( .+)''')
      
      for src_name in glob.glob(os.path.join(source_dir, '*.log')):
          base = os.path.basename(src_name)
          dest_name = os.path.join(dest_dir, base)
          with open(src_name, 'rt') as infile, open(dest_name, 'wt') as outfile:
              for line in infile:
                  groups = pattern.search(line).groups()
                  outfile.write(groups[0]+groups[2]+'\n')
      

      【讨论】:

        猜你喜欢
        • 2013-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-06
        • 2019-08-25
        • 1970-01-01
        相关资源
        最近更新 更多