forked from DidierStevens/DidierStevensSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headtail.py
115 lines (95 loc) · 2.82 KB
/
headtail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
__description__ = 'Output head and tail of input'
__author__ = 'Didier Stevens'
__version__ = '0.0.1'
__date__ = '2015/07/12'
"""
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2015/07/12: start
Todo:
"""
import optparse
import glob
import sys
import collections
def File2Strings(filename):
try:
f = open(filename, 'r')
except:
return None
try:
return map(lambda line:line.rstrip('\n'), f.readlines())
except:
return None
finally:
f.close()
def ProcessAt(argument):
if argument.startswith('@'):
strings = File2Strings(argument[1:])
if strings == None:
raise Exception('Error reading %s' % argument)
else:
return strings
else:
return [argument]
def ExpandFilenameArguments(filenames):
return list(collections.OrderedDict.fromkeys(sum(map(glob.glob, sum(map(ProcessAt, filenames), [])), [])))
class cOutput():
def __init__(self, filename=None):
self.filename = filename
if self.filename and self.filename != '':
self.f = open(self.filename, 'w')
else:
self.f = None
def Line(self, line):
if self.f:
self.f.write(line + '\n')
else:
print(line)
def Close(self):
if self.f:
self.f.close()
self.f = None
def HeadTail(args, options):
lines = []
for file in args:
if file == '':
fIn = sys.stdin
else:
fIn = open(file, 'r')
for line in [line.strip('\n') for line in fIn.readlines()]:
lines.append(line)
if file != '':
fIn.close()
oOutput = cOutput(options.output)
if len(lines) <= options.number * 2 + 1:
for line in lines:
oOutput.Line(line)
else:
for line in lines[0:options.number]:
oOutput.Line(line)
oOutput.Line('...')
for line in lines[-options.number:]:
oOutput.Line(line)
oOutput.Close()
def Main():
moredesc = '''
Arguments:
@file: process each file listed in the text file specified
wildcards are supported
Source code put in the public domain by Didier Stevens, no Copyright
Use at your own risk
https://DidierStevens.com'''
oParser = optparse.OptionParser(usage='usage: %prog [options] [files]\n' + __description__ + moredesc, version='%prog ' + __version__)
oParser.add_option('-n', '--number', type=int, default=10, help='Number of lines')
oParser.add_option('-o', '--output', default='', help='Output file')
(options, args) = oParser.parse_args()
if len(args) == 0:
HeadTail([''], options)
else:
HeadTail(ExpandFilenameArguments(args), options)
if __name__ == '__main__':
Main()