mò 8T§Cc@sdZdZdZdZdZdZdZdkZdkZdk Z hZ dei d„Z eid „Zd „Zdd d „Zd „Zd„Zd„Zedjody eƒWq ej oeiidƒq XqdkZeiddƒZeiddƒndS(s5This program generates a histogram of the input data. This is intended to replace a complex pipeline like the following awk -F" '{print $6}' /var/log/httpd-access.log | \ sort | uniq -c | sort -nr | head -20 ...with a single process, like so: histogram.py -F" -f 6 -n 20 < /var/log/httpd-access.log sChuck Swiger s!Copyright (c) 2005 Charles Swigers7$Id: histogram.py,v 1.4 2005/12/18 22:58:14 chuck Exp $spThis software is licenced under the same terms as Python. (See the PSF license at www.python.org/license.html).s1.0s$Revision: 1.4 $NcCs@|otiid|ƒntiidƒti|ƒdS(s4Displays program usage message and help, then exits.s ERROR: %s süUsage: histogram.py [options] data_file [ data_file2... ] Where [options] may include any of the following flags: -b N --byte Start after byte offset N. "-b 0" means the whole line. -B N --byte-field Start field after N bytes. -c N --count Only display lines which appeared at least N times. -f N --field Use field #N only, default ("-f 0") means whole line. Also [ -f N1,N2,N3 ] supports a comma-seperated list to output multiple fields. -F FS --field-separator Use FS as the field seperator, rather than whitespace. Note that $FS can also be set in the environment (cf awk). -h --help Display this help message. -l --lower Lower-cases the input to match case-insensitively. -n N --number Generate N lines of output and then exit. -o file --output Write data to file specified, rather than stdout. -p PAT --pattern Consider only lines which match string PAT. (no regex yet) -P PAT --pattern-field Consider only lines if selected field matches PAT. -q STR --quote Quote the intput fields using STR in the generated output. -r --reverse Generate the output sorted from least to most common. -s N --size Limit the size of the line (or field) to N characters. -v --verbose Enable verbosity and display frequency list. -V --version Display the version of histogram.py and exit. -x PAT --exclude Exclude lines which match string PAT. (no regex yet) -X PAT --exclude-field Exclude fields which match string PAT. If no filename is specified the program will read stdin, and will write to stdout unless -o is used. '-' can be used as an alias for stdin or stdout. This program generates a histogram of the input data. This is intended to replace a complex pipeline like the following: awk -F\" '{print $6}' /var/log/httpd-access.log | \ sort | uniq -c | sort -nr | head -20 ...with a single process, like so: histogram.py -F\" -f 6 -n 20 < /var/log/httpd-access.log N(tmsgtsyststderrtwritetexittexitCode(RR((t histogram.pytusages *cCs+tiidttfƒti|ƒdS(s*Displays program version info, then exits.shistogram.py version %s %s N(RRRt versionNumbert versionInfoRR(R((RtversionNscCsFd}||g}x-|D]%}|d|jo|d}qqW|S(s;Counts the number of times an option or long-alias appears.iiN(tcountt shortNametlongNametxtoLto(RR R R RR((RtcountOptOccurrencesVs icCs]|i|ƒo||}n#|i|ƒo||}n|S|djo|Sn|SdS(sÀ Scans oD for options with name shortName or longName. If found, returns the value. If not found, returns defaultValue. If found, but the value is '', returns the setValue. tN(toDthas_keyR t possibleValueR t defaultValuetsetValue(RR R RRR((RtcheckOpt_s c Cs2h}y\titiddddddddd d d d d dddddddgƒ\} }Wn&tiƒ\}}}t |ƒnXx| D]\}}|||•st%s!FATAL: Format not supported yet. tFSs-Fs--field-separatortfss-ls--lowers-ns--numbertnumbers-ps --patternRtpatterns-Ps--pattern-fieldt patternfields-qs--quotetquotes-rs --reversetreverses-ss--sizetsizes-vs --verboseRs-xs --excludetexcludes-Xs--exclude-fieldt excludefields-os--outputtoutputtws2FATAL: histogram.py couldn't write to the output! N((toptDicttgetoptRtargvt optionListtargstexc_infotexc_typet exc_valuet exc_tracebackRtktvtlentlisttconfigRtNonetostEX_OKR R$tfindtsplittfltmapRRRtEX_UNAVAILABLEtenvironRt fs_defaulttFalsetTrueRtstdouttopentIOErrort EX_CANTCREAT( R9R3R:R7RFR<R;R=RJR6((Rt parseOptionsssfI   +cs |djo ti}nQyt|ddƒ}Wn7tj o+tiid|ƒtit i ƒnXt d}t d}t d} t d}t d }t d }t d } t d d }tt d ƒ} t d d}t dodt dt df}nd}t d‰t d‰ˆo*ˆo‡‡d†}qn‡d†}n!ˆo‡d†}n d„}t d‰t d‰ˆo*ˆo‡‡d†}qÓ‡d†}n!ˆo‡d†}n d„}x|D]}| d7} t|ƒd|jo,|d jo||d!}qU|d }n)|otiid||fƒqÚqÚ||ƒoqÚn|d joÒ|i t dƒ}t|ƒ| jo0|o"tiid |d|fƒqÚqÚn||}|d joMt|ƒ|jo||}q0|otiid!||fƒqÚqÚn||ƒoqÚqHn|d7}x&|D]}||||i#ƒ7}qYW| o!t|ƒ| jo|| }nt d"o|i$ƒ}n|i%|ƒo||d||REtwordlistRYtstripRRtclose(R\RReRhRbRiRWR`RTRaRUR[RdRZRjR RgRXR^RRYRf((RWR[RZRXRt scan_file¹s                     "       c CstƒhtdtnfRRRRRBt EX_DATAERRR?tsettvaluestfreqlisttsorttstrRct fieldwidthtformatRTtvalueRltiterkeystkey( RzRxR`RwRsRvRoRnRT((Rtmain.sX       *'"& /    "  "t__main__s' ...Control-C seen, quitting program. tbiasf2.0000000000000002e-05smain()s main.profile(t__doc__t __author__t __copyright__t __version__t __license__RR RBRR4R@RAtEX_USAGERRCR RRRQRmR{t__name__tKeyboardInterruptRRtprofiletProfiletprtrun(RˆRR RR4R{R@R†RmR‚RRRRR R€RRQRB((Rt? s03  F u E