我希望所有记录直到文件末尾,这些记录大于unix中的给定日期

时间:2015-12-16 12:08:52

标签: unix

我希望所有记录直到日志文件的末尾,这些记录大于给定日期... 假设给定日期是2015年12月14日22:00:03那么我希望日志文件中的所有行直到第一次出现大于此日期时结束。

ex:awk'{if($ 0>“Tue Dec 15 08:00:00 2015”)print}'file.out

  • 只打印大于日期的行,但我希望匹配后的所有行直到文件结尾..

请注意 1.我不能使用正则表达式,因为我不知道该日期的日志文件中是否存在条目,即H:M:S所以我必须使用大于日期的功能。

  1. 日期不存在于lg文件的每一行。它介于两者之间,但在新行
  2. 请帮助

    示例日志文件:::

    Mon Dec 14 02:00:00 2015
    Clearing Resource Manager plan via parameter
    Mon Dec 14 07:02:57 2015
    
    
    ***********************************************************************
    
    Fatal NI connect error 12504, connecting to:
     (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=)(CID=(PROGRAM=oracle)(HOST=ltest8)(USER=oracle)))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.115)(PORT=1521)))
    
      VERSION INFORMATION:
        TNS for Linux: Version 11.1.0.6.0 - Production
        TCP/IP NT Protocol Adapter for Linux: Version 11.1.0.6.0 - Production
      Time: 14-DEC-2015 07:02:57
      Tracing not turned on.
      Tns error struct:
        ns main err code: 12564
    
    TNS-12564: TNS:connection refused
        ns secondary err code: 0
        nt main err code: 0
        nt secondary err code: 0
        nt OS err code: 0
    Mon Dec 14 08:01:37 2015
    
    
    ***********************************************************************
    
    Fatal NI connect error 12504, connecting to:
     (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=)(CID=(PROGRAM=oracle)(HOST=ltest8)(USER=oracle)))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.115)(PORT=1521)))
    
      VERSION INFORMATION:
        TNS for Linux: Version 11.1.0.6.0 - Production
        TCP/IP NT Protocol Adapter for Linux: Version 11.1.0.6.0 - Production
      Time: 14-DEC-2015 08:01:37
      Tracing not turned on.
      Tns error struct:
        ns main err code: 12564
    
    TNS-12564: TNS:connection refused
        ns secondary err code: 0
        nt main err code: 0
        nt secondary err code: 0
        nt OS err code: 0
    Mon Dec 14 08:54:33 2015
    
    
    ***********************************************************************
    
    Fatal NI connect error 12504, connecting to:
     (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=)(CID=(PROGRAM=oracle)(HOST=ltest8)(USER=oracle)))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.115)(PORT=1521)))
    
      VERSION INFORMATION:
        TNS for Linux: Version 11.1.0.6.0 - Production
        TCP/IP NT Protocol Adapter for Linux: Version 11.1.0.6.0 - Production
      Time: 14-DEC-2015 08:54:33
      Tracing not turned on.
      Tns error struct:
        ns main err code: 12564
    
    TNS-12564: TNS:connection refused
        ns secondary err code: 0
        nt main err code: 0
        nt secondary err code: 0
        nt OS err code: 0
    Mon Dec 14 08:57:18 2015
    Thread 1 advanced to log sequence 232
      Current log# 2 seq# 232 mem# 0: /u04/app/oracle/oradata/kcom/redo02.log
    Mon Dec 14 08:57:19 2015
    Errors in file /u01/app/oracle/diag/rdbms/kcom/Rialto/trace/Rialto_arc3_3953.trc:
    ORA-19815: WARNING: db_recovery_file_dest_size of 268435456 bytes is 100.00% used, and has 0 remaining bytes available.
    ************************************************************************
    You have following choices to free up space from flash recovery area:
    1. Consider changing RMAN RETENTION POLICY. If you are using Data Guard,
       then consider changing RMAN ARCHIVELOG DELETION POLICY.
    2. Back up files to tertiary device such as tape using RMAN
       BACKUP RECOVERY AREA command.
    3. Add disk space and increase db_recovery_file_dest_size parameter to
       reflect the new space.
    4. Delete unnecessary files using RMAN DELETE command. If an operating
       system command was used to delete files, then use RMAN CROSSCHECK and
       DELETE EXPIRED commands.
    ************************************************************************
    Mon Dec 14 09:17:45 2015
    
    
    ***********************************************************************
    
    Fatal NI connect error 12504, connecting to:
     (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=)(CID=(PROGRAM=oracle)(HOST=ltest8)(USER=oracle)))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.115)(PORT=1521)))
    
      VERSION INFORMATION:
        TNS for Linux: Version 11.1.0.6.0 - Production
        TCP/IP NT Protocol Adapter for Linux: Version 11.1.0.6.0 - Production
      Time: 14-DEC-2015 09:17:45
      Tracing not turned on.
      Tns error struct:
        ns main err code: 12564
    
    TNS-12564: TNS:connection refused
        ns secondary err code: 0
        nt main err code: 0
        nt secondary err code: 0
        nt OS err code: 0
    Mon Dec 14 10:25:24 2015
    QKSRC: ViewText[ecode=942] = SELECT /*+ result_cache */ ID, 'PLUGIN_'||NAME AS NAME, STANDARD_ATTRIBUTES, SQL_MIN_COLUMN_COUNT, NVL(SQL_MAX_COLUMN_COUNT, 999) AS SQL_MAX_COLUMN_COUNT, SQL_EXAMPLES FROM WWV_FLOW_PLUGINS WHERE FLOW_ID = :B2 AND PLUGIN_TYPE = :B1 
    Mon Dec 14 14:31:14 2015
    Thread 1 advanced to log sequence 233
      Current log# 3 seq# 233 mem# 0: /u04/app/oracle/oradata/kcom/redo03.log
    Mon Dec 14 14:31:15 2015
    Errors in file /u01/app/oracle/diag/rdbms/kcom/Rialto/trace/Rialto_arc0_3947.trc:
    ORA-19815: WARNING: db_recovery_file_dest_size of 268435456 bytes is 100.00% used, and has 0 remaining bytes available.
    ************************************************************************
    You have following choices to free up space from flash recovery area:
    1. Consider changing RMAN RETENTION POLICY. If you are using Data Guard,
       then consider changing RMAN ARCHIVELOG DELETION POLICY.
    2. Back up files to tertiary device such as tape using RMAN
       BACKUP RECOVERY AREA command.
    3. Add disk space and increase db_recovery_file_dest_size parameter to
       reflect the new space.
    4. Delete unnecessary files using RMAN DELETE command. If an operating
       system command was used to delete files, then use RMAN CROSSCHECK and
       DELETE EXPIRED commands.
    ************************************************************************
    Mon Dec 14 20:28:23 2015
    Thread 1 advanced to log sequence 234
      Current log# 4 seq# 234 mem# 0: /u04/app/oracle/oradata/kcom/redo04.log
    Mon Dec 14 20:28:24 2015
    Errors in file /u01/app/oracle/diag/rdbms/kcom/Rialto/trace/Rialto_arc1_3949.trc:
    ORA-19815: WARNING: db_recovery_file_dest_size of 268435456 bytes is 100.00% used, and has 0 remaining bytes available.
    ************************************************************************
    You have following choices to free up space from flash recovery area:
    1. Consider changing RMAN RETENTION POLICY. If you are using Data Guard,
       then consider changing RMAN ARCHIVELOG DELETION POLICY.
    2. Back up files to tertiary device such as tape using RMAN
       BACKUP RECOVERY AREA command.
    3. Add disk space and increase db_recovery_file_dest_size parameter to
       reflect the new space.
    4. Delete unnecessary files using RMAN DELETE command. If an operating
       system command was used to delete files, then use RMAN CROSSCHECK and
       DELETE EXPIRED commands.
    ************************************************************************
    Mon Dec 14 22:00:00 2015
    Setting Resource Manager plan SCHEDULER[0x2C09]:DEFAULT_MAINTENANCE_PLAN via scheduler window
    Setting Resource Manager plan DEFAULT_MAINTENANCE_PLAN via parameter
    Mon Dec 14 22:00:03 2015
    Mon Dec 14 22:00:03 2015
    Logminer Bld: Lockdown Complete.  DB_TXN_SCN is   UnwindToSCN (LockdownSCN) is 18957974
    Tue Dec 15 02:00:00 2015
    Clearing Resource Manager plan via parameter
    Tue Dec 15 02:00:02 2015
    

1 个答案:

答案 0 :(得分:1)

带有硬编码日期时间和输入文件的简单python 2解析器。使用您给定的日志进行测试。它有足够的优化空间,但我认为这是一个开始。

#/usr/bin/env python

import re
from datetime import datetime

# year, month, day, hour, minute
filter_from = datetime(2015, 12, 13, 23, 40)

with open("tmp.log") as log:
    match = False
    for line in log:
        if (match):
            print line
        else:
            candidate = re.match(r'[A-Z][a-z][a-z] [A-Z][a-z][a-z] \d\d \d\d:\d\d:\d\d \d\d\d\d', line)
            if candidate:
                parsed_date = datetime.strptime(candidate.group(0), "%a %b %d %X %Y")
                if parsed_date > filter_from:
                     match = True
                     print line