striplog 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python
  2. #
  3. # striplog -- strip leading lines from logs
  4. #
  5. # striplog -j strips JSON leader sentences.
  6. # striplog with no option strips all leading lines beginning with #
  7. #
  8. # This file is Copyright (c) 2010-2019 by the GPSD project
  9. # BSD terms apply: see the file COPYING in the distribution root for details.
  10. #
  11. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  12. # Preserve this property!
  13. from __future__ import absolute_import, print_function, division
  14. import getopt
  15. import sys
  16. secondline = firstline = stripjson = False
  17. stripval = 0
  18. (options, arguments) = getopt.getopt(sys.argv[1:], "12n:j")
  19. for (switch, val) in options:
  20. if (switch == '-1'):
  21. firstline = True
  22. if (switch == '-2'):
  23. secondline = True
  24. if (switch == '-n'):
  25. stripval = int(val)
  26. if (switch == '-j'):
  27. stripjson = True
  28. try:
  29. if firstline:
  30. sys.stdin.readline()
  31. elif secondline:
  32. sys.stdin.readline()
  33. sys.stdin.readline()
  34. elif stripval:
  35. for _dummy in range(stripval):
  36. sys.stdin.readline()
  37. elif stripjson:
  38. while True:
  39. line = sys.stdin.readline()
  40. if ((line.startswith('{"class":"VERSION"') or
  41. line.startswith('{"class":"DEVICE"') or
  42. line.startswith('{"class":"DEVICES"') or
  43. line.startswith('{"class":"WATCH"'))):
  44. continue
  45. else:
  46. break
  47. sys.stdout.write(line)
  48. else:
  49. while True:
  50. line = sys.stdin.readline()
  51. if line[0] != '#':
  52. break
  53. sys.stdout.write(line)
  54. sys.stdout.write(sys.stdin.read())
  55. except KeyboardInterrupt:
  56. pass