예제 #1
0
파일: grep_test.py 프로젝트: dktim/py-text
 def test_grep(self):
     
     log_list = text_test.read_log()
     
     linux_syslog_head = '(\S+\s+\d+)\s+(\d+:\d+:\d+)\s+(\S+)\s+'
     group_data = grep.grep(log_list, linux_syslog_head + '(login|ssh|su|sshd|passwd)\[(\d+)\].*')
     self.assertEqual(len(group_data), 11)
     
     group_data = grep.grep(log_list, '[1,4]', False, 'n')
     self.assertEqual(len(group_data), 4)
     
     group_data = grep.grep(log_list, '[1,4]', False, 'n')
     self.assertEqual(len(group_data), 4)
     
     group_data = grep.grep(log_list, 'pam', False, 's')
     self.assertEqual(len(group_data), 6)
     
     group_data = grep.grep(log_list, 'pam', True, 's')
     self.assertEqual(len(group_data), 6)
     self.assertTrue(string_utils.startswith(group_data[0], '1'))
     self.assertTrue(string_utils.startswith(group_data[1], '2'))
     self.assertTrue(string_utils.startswith(group_data[4], '12'))
     self.assertTrue(string_utils.startswith(group_data[5], '19'))
     
     group_data = grep.grep(log_list, None, True, 'e')
     self.assertEqual(len(group_data), 19)
     
     group_data = grep.grep(log_list, grep_action, True, 'a')
     self.assertEqual(len(group_data), 3)
     
     group_data = grep.grep(None, None)
     self.assertEqual(group_data, None)
예제 #2
0
def awk(target, pattern, separator, action):
    '''
    awk : pattern scanning and text processing language
    
    @param target: string list or text file name
    @param pattern: regex pattern
    @param separator: line separator
    @param action: column index list or function: str[]=action(str[])
    
    @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male']
              print awk.awk(list, '', ':', [1])
              output: ['huiyugeng', 'zhuzhu']
    
    '''

    text = grep.grep(target, pattern)
    if not text:
        return None

    if str_utils.is_blank(separator):
        separator = ' '

    result = []

    for line in text:
        if line != None and separator in line:
            split_text = str_utils.split(line, separator)
            if not action:
                result.append(split_text)
            elif type(action) == types.ListType:
                temp_line = []
                for column in action:
                    if str_utils.is_numeric(column):
                        temp_line.append(split_text[column])
                result.append(temp_line)
            elif type(action) == types.FunctionType:
                temp_line = action(split_text)
                if temp_line != None and type(temp_line) == types.ListType:
                    result.append(temp_line)

    return result
예제 #3
0
파일: awk.py 프로젝트: PinaeOS/py-text
def awk(target, pattern, separator, action):
    '''
    awk : pattern scanning and text processing language
    
    @param target: string list or text file name
    @param pattern: regex pattern
    @param separator: line separator
    @param action: column index list or function: str[]=action(str[])
    
    @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male']
              print awk.awk(list, '', ':', [1])
              output: ['huiyugeng', 'zhuzhu']
    
    '''
    
    text = grep.grep(target, pattern)
    if not text:
        return None
    
    if str_utils.is_blank(separator):
        separator = ' '
    
    result = []
        
    for line in text:
        if line != None and separator in line:
            split_text = str_utils.split(line, separator)
            if not action:
                result.append(split_text)
            elif type(action) == types.ListType:
                temp_line = []
                for column in action:
                    if str_utils.is_numeric(column):
                        temp_line.append(split_text[column])
                result.append(temp_line)
            elif type(action) == types.FunctionType:
                temp_line = action(split_text)
                if temp_line != None and type(temp_line) == types.ListType:
                    result.append(temp_line)
                
    return result
예제 #4
0
파일: sed_test.py 프로젝트: PinaeOS/py-text
 def test_sed(self):
     result = sed.sed('test_file/sshd_config', '#ListenAddress 0.0.0.0', 'e', 'ListenAddress 192.168.228.123', 's', 'rl')
     self.assertEqual(len(grep.grep(result, '.*(192.168.228.123).*')), 1)
     
     self.assertEqual(len(grep.grep(result, 'PrintMotd no')), 1)
     result = sed.sed('test_file/sshd_config', 'PrintMotd no', 'e', '', 'd', 'rl')
     self.assertEqual(len(grep.grep(result, 'PrintMotd no')), 0)
      
     self.assertEqual(len(grep.grep(result, 'UseLogin yes')), 0)
     result = sed.sed('test_file/sshd_config', 'TCPKeepAlive yes', 'e', 'UseLogin yes', 'a', 'rl')
     self.assertEqual(len(grep.grep(result, 'UseLogin yes')), 1)
     
     self.assertEqual(len(grep.grep(result, 'Banner /opt/ssh_banner')), 0)
     result = sed.sed('test_file/sshd_config', '#Banner /etc/issue.net', 'e', 'Banner /opt/ssh_banner', 'i', 'rl')
     self.assertEqual(len(grep.grep(result, 'Banner /opt/ssh_banner')), 1)
     
     result = sed.sed('test_file/sshd_config', None, 'e', 'Something', 'i', 'rl')
     
     result = sed.sed('test_file/sshd_config', '#Banner /etc/issue.net', 'e', None, 'i', 'rl')