Created
June 7, 2013 19:04
-
-
Save lpar/5731584 to your computer and use it in GitHub Desktop.
Report the first 10 lines of all files on the command line, transparently decompressing .xz compressed files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: UTF-8 | |
# The audit team wanted to know that we were doing logging as required. As | |
# evidence, they asked for a regular report consisting of the first 10 lines | |
# of each daily log file. I wrote this script to automate the process. | |
# If you want the last 10 lines instead, I suggest the Ruby Gem called Elif, | |
# which wraps any IO object to read line by line backwards. | |
LINES_OF_LOG = 10 | |
XZCAT = '/usr/bin/xzcat --decompress --stdout' | |
def firstlines(filename, numlines) | |
stream = nil | |
pathname = File.realpath(filename) | |
result = "### #{pathname} ###\n\n" | |
if filename.match('.xz') | |
stream = IO.popen("#{XZCAT} #{filename}") | |
else | |
stream = File.open(filename, "r") | |
end | |
gotone = false | |
for i in 1..numlines | |
line = stream.gets | |
if line | |
gotone = true | |
result << line | |
end | |
end | |
if !gotone | |
result << "[No text in log file #{pathname}]\n" | |
end | |
return result << "\n" | |
end | |
if ARGV.length == 0 | |
puts "#{$0} [FILE] ..." | |
puts " Outputs a report listing the first #{LINES_OF_LOG} lines from each listed file." | |
puts " Transparently decompresses files compressed with xz." | |
end | |
for file in ARGV | |
puts firstlines(file, LINES_OF_LOG) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment