« blib/lib/App/RecordStream/Operation/fromre.pm
↑ Summary
blib/lib/App/RecordStream/Manual/Examples.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package App::RecordStream; | ||
| 5 | use Module::Pluggable::Object; | ||
| 6 | |||
| 7 | our $VERSION = "4.0.23"; | ||
| 8 | |||
| 9 | sub operation_packages { | ||
| 10 | sort { $a cmp $b } | ||
| 11 | Module::Pluggable::Object->new( | ||
| 12 | search_path => "App::RecordStream::Operation", | ||
| 13 | max_depth => 4, | ||
| 14 | )->plugins | ||
| 15 | } | ||
| 16 | |||
| 17 | =encoding utf-8 | ||
| 18 | |||
| 19 | =for markdown | ||
| 20 | <img src="https://cdn.rawgit.com/benbernard/RecordStream/master/logos/logo-small.svg" align="right"> | ||
| 21 | |||
| 22 | =for markdown | ||
| 23 | [](https://metacpan.org/release/App-RecordStream) | ||
| 24 | [](https://travis-ci.org/benbernard/RecordStream) | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | App::RecordStream - recs - A system for command-line analysis of data | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | A set of programs for creating, manipulating, and outputting a stream of | ||
| 33 | Records, or JSON hashes. Inspired by Monad. | ||
| 34 | |||
| 35 | =head1 INSTALLATION | ||
| 36 | |||
| 37 | =head2 Quick, standalone bundle | ||
| 38 | |||
| 39 | The quickest way to start using recs is via the minimal, standalone bundle: | ||
| 40 | |||
| 41 | curl -fsSL https://recs.pl > recs | ||
| 42 | chmod +x recs | ||
| 43 | ./recs --help | ||
| 44 | |||
| 45 | This is also known as the "fatpacked" recs. | ||
| 46 | |||
| 47 | =head2 From CPAN | ||
| 48 | |||
| 49 | You can also install recs from L<CPAN|http://cpan.org> as App::RecordStream: | ||
| 50 | |||
| 51 | cpanm --interactive App::RecordStream | ||
| 52 | |||
| 53 | Using L<cpanm> in interactive mode will prompt you for optional feature | ||
| 54 | support. Other CPAN clients such as L<cpan> and L<cpanp> also work fine, but | ||
| 55 | you can't opt to use any optional features (just like cpanm in non-interactive | ||
| 56 | mode). A kitchen-sink install of App::RecordStream looks like: | ||
| 57 | |||
| 58 | cpanm --with-recommends --with-all-features App::RecordStream | ||
| 59 | |||
| 60 | If you don't have L<cpanm> itself, you can install it easily with: | ||
| 61 | |||
| 62 | curl -fsSL https://cpanmin.us | perl - App::cpanminus | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | The recs system consists of three basic sets of commands: | ||
| 67 | |||
| 68 | =over 4 | ||
| 69 | |||
| 70 | =item * I<Input> commands responsible for generating streams of record objects | ||
| 71 | |||
| 72 | =item * I<Manipulation> commands responsible for analyzing, selecting, and manipulating records | ||
| 73 | |||
| 74 | =item * I<Output> commands responsible for taking record streams and producing output for humans or other programs | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | These commands can interface with other systems to retrieve data, parse existing | ||
| 79 | files, or just regex out some values from a text stream. | ||
| 80 | |||
| 81 | Commands are run using C<recs command [options and arguments]>. If you're using | ||
| 82 | a CPAN-based install, you may also run commands directly as C<recs-command>, | ||
| 83 | though this is no longer recommended for forwards compatibility. Both | ||
| 84 | L<installation methods|/INSTALLATION> provide a top-level C<recs> executable | ||
| 85 | which dispatches to commands, so this is the preferred invocation style. | ||
| 86 | |||
| 87 | The core recs commands are briefly summarized below, and you can list all | ||
| 88 | available commands by running C<recs --list>. | ||
| 89 | |||
| 90 | To read more about each command, run C<recs command --help>. Longer | ||
| 91 | documentation is available as C<recs command --help-all> or C<perldoc recs-command>. | ||
| 92 | For example, to read more about L</fromcsv>, you might run any of the | ||
| 93 | following: | ||
| 94 | |||
| 95 | recs fromcsv --help | ||
| 96 | recs fromcsv --help-all | ||
| 97 | perldoc recs-fromcsv | ||
| 98 | |||
| 99 | =head1 COMMANDS | ||
| 100 | |||
| 101 | =head2 Input Generation | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item fromcsv | ||
| 106 | |||
| 107 | Produces records from a csv file/stream | ||
| 108 | |||
| 109 | =item fromdb | ||
| 110 | |||
| 111 | Produces records for a db table, or from a SELECT statement into a db. | ||
| 112 | |||
| 113 | =item fromre | ||
| 114 | |||
| 115 | Matches input streams against a regex, puts capture groups into hashes | ||
| 116 | |||
| 117 | =item frommongo | ||
| 118 | |||
| 119 | Generate a record stream from a MongoDB query. | ||
| 120 | |||
| 121 | =item frommultire | ||
| 122 | |||
| 123 | Matches input streams against several regexes, puts capture groups into the record | ||
| 124 | |||
| 125 | =item fromsplit | ||
| 126 | |||
| 127 | Splits input stream on a delimiter | ||
| 128 | |||
| 129 | =item fromps | ||
| 130 | |||
| 131 | Generate records from the process tree | ||
| 132 | |||
| 133 | =item fromatomfeed | ||
| 134 | |||
| 135 | Produces records for an optionally paginated atom feed. | ||
| 136 | |||
| 137 | =item fromxml | ||
| 138 | |||
| 139 | Produces records for an XML document. | ||
| 140 | |||
| 141 | =item fromkv | ||
| 142 | |||
| 143 | Produces records from input streams containing loosely formed key/value pairs | ||
| 144 | |||
| 145 | =item fromtcpdump | ||
| 146 | |||
| 147 | Produces records from packet capture files (.pcap) as made by tcpdump | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head2 Stream Manipulation | ||
| 152 | |||
| 153 | =over 4 | ||
| 154 | |||
| 155 | =item annotate | ||
| 156 | |||
| 157 | Annotate records with common fields, will memoize additions to speed up common | ||
| 158 | annotations | ||
| 159 | |||
| 160 | =item collate | ||
| 161 | |||
| 162 | Perforce aggregation operations on records. Group by a field, get an average, | ||
| 163 | sum, correlation, etc. Very powerful | ||
| 164 | |||
| 165 | =item delta | ||
| 166 | |||
| 167 | Transform values into deltas between adjacent records | ||
| 168 | |||
| 169 | =item eval | ||
| 170 | |||
| 171 | Eval a string of Perl against each record | ||
| 172 | |||
| 173 | =item flatten | ||
| 174 | |||
| 175 | Flatten records of input to one level | ||
| 176 | |||
| 177 | =item grep | ||
| 178 | |||
| 179 | Select records for which a string of Perl evaluates to true. | ||
| 180 | |||
| 181 | =item multiplex | ||
| 182 | |||
| 183 | Take records, grouped by keys, and run a separate recs command for each group. | ||
| 184 | |||
| 185 | =item normalizetime | ||
| 186 | |||
| 187 | Based on a time field, tag records with a normalized time, i.e. every 5 minute buckets | ||
| 188 | |||
| 189 | =item join | ||
| 190 | |||
| 191 | Perform an inner join of two record streams. Associate records in one stream | ||
| 192 | with another stream. | ||
| 193 | |||
| 194 | =item substream | ||
| 195 | |||
| 196 | Filter to a range of matching records with paired Perl snippets C<--start> and C<--end>. | ||
| 197 | |||
| 198 | =item sort | ||
| 199 | |||
| 200 | Sort records based on keys, may specify multiple levels of sorting, as well as | ||
| 201 | numerical or lexical sort ordering | ||
| 202 | |||
| 203 | =item topn | ||
| 204 | |||
| 205 | Outputs the top I<n> records. You may segment the input based on a list of keys | ||
| 206 | such that unique values of keys are treated as distinct input streams. This | ||
| 207 | enables top I<n> listings per value groupings. | ||
| 208 | |||
| 209 | =item xform | ||
| 210 | |||
| 211 | Perform a block of Perl on each record, which may modify the record, Record is | ||
| 212 | then output | ||
| 213 | |||
| 214 | =item generate | ||
| 215 | |||
| 216 | Perform a block of Perl on each record to generate a record stream, which is | ||
| 217 | then output with a chain link back to the original record. | ||
| 218 | |||
| 219 | =back | ||
| 220 | |||
| 221 | =head2 Output Generation | ||
| 222 | |||
| 223 | =over 4 | ||
| 224 | |||
| 225 | =item todb | ||
| 226 | |||
| 227 | Inserts records into a DBI supported SQL database. Will create a local SQLite | ||
| 228 | database by default | ||
| 229 | |||
| 230 | =item tocsv | ||
| 231 | |||
| 232 | Generates correctly quoted CSV files from record streams. | ||
| 233 | |||
| 234 | =item tognuplot | ||
| 235 | |||
| 236 | Create a graph of field values in a record using GNU Plot. | ||
| 237 | |||
| 238 | =item totable | ||
| 239 | |||
| 240 | Pretty prints a table of results. | ||
| 241 | |||
| 242 | =item tohtml | ||
| 243 | |||
| 244 | Prints out an HTML table of the record stream | ||
| 245 | |||
| 246 | =item toprettyprint | ||
| 247 | |||
| 248 | Prettily prints records, one key to a line, great for making sense of very large records | ||
| 249 | |||
| 250 | =item toptable | ||
| 251 | |||
| 252 | Prints a multi-dimensional (pivot) table of values. Very powerful. | ||
| 253 | |||
| 254 | =back | ||
| 255 | |||
| 256 | =head1 KEY SPECS | ||
| 257 | |||
| 258 | Many of the commands above take key arguments to specify or assign to a key in a | ||
| 259 | record. Almost all of the places where you can specify a key (which normally | ||
| 260 | means a first level key in the record), you can instead specify a key spec. | ||
| 261 | |||
| 262 | A key spec may be nested, and may index into arrays. Use a C</> to nest into a | ||
| 263 | hash and a C<#NUM> to index into an array (i.e. C<#2>) | ||
| 264 | |||
| 265 | An example is in order, take a record like this: | ||
| 266 | |||
| 267 | {"biz":["a","b","c"],"foo":{"bar 1":1},"zap":"blah1"} | ||
| 268 | {"biz":["a","b","c"],"foo":{"bar 1":2},"zap":"blah2"} | ||
| 269 | {"biz":["a","b","c"],"foo":{"bar 1":3},"zap":"blah3"} | ||
| 270 | |||
| 271 | In this case a key spec of C<foo/bar 1> would have the values 1, 2, and 3 | ||
| 272 | respectively. | ||
| 273 | |||
| 274 | Similarly, C<biz/#0> would have the value of C<a> for all 3 records | ||
| 275 | |||
| 276 | =head2 Fuzzy matching | ||
| 277 | |||
| 278 | You can also prefix key specs with C<@> to engage the fuzzy matching logic. | ||
| 279 | Matching is tried like this, in order, with the first key to match winning: | ||
| 280 | |||
| 281 | =over 4 | ||
| 282 | |||
| 283 | =item 1. Exact match (C<eq>) | ||
| 284 | |||
| 285 | =item 2. Prefix match (C<m/^/>) | ||
| 286 | |||
| 287 | =item 3. Match anywhere in the key (C<m//>) | ||
| 288 | |||
| 289 | =back | ||
| 290 | |||
| 291 | Given the above example data and the fuzzy key spec C<@b/#2>, the C<b> portion | ||
| 292 | would expand to C<biz> and C<2> would be the index into the array, so all | ||
| 293 | records would have the value of C<c>. | ||
| 294 | |||
| 295 | Simiarly, C<@f/b> would have values 1, 2, and 3. | ||
| 296 | |||
| 297 | =head1 WRITING YOUR OWN COMMANDS | ||
| 298 | |||
| 299 | The data stream format of the recs commands is JSON hashes separated by new | ||
| 300 | lines. If you wish to write your own recs command in your own language, just | ||
| 301 | get a JSON parser and you should be good to go. The recs commands use | ||
| 302 | L<JSON::MaybeXS>. | ||
| 303 | |||
| 304 | If you name your command as C<recs-mycommand> and put it somewhere in your | ||
| 305 | C<PATH> environment variable, the C<recs> command will dispatch to it when | ||
| 306 | called as C<recs mycommand>. It will also be included in C<recs --list> | ||
| 307 | output. | ||
| 308 | |||
| 309 | If you want to write your new command in Perl, you can use the same Perl API | ||
| 310 | that the standard recs toolkit uses. See the various | ||
| 311 | L<App::RecordStream::Operation> subclasses. Once your new operation class is | ||
| 312 | installed in perl's library paths, C<recs> will find it automatically without | ||
| 313 | the need for any executable command shim. | ||
| 314 | |||
| 315 | =head1 EXAMPLES | ||
| 316 | |||
| 317 | # look in the custom access log for all accesses with greater than 5 seconds, | ||
| 318 | # display in a table | ||
| 319 | cat access.log \ | ||
| 320 | | recs fromre --fields ip,time '^(\d+).*TIME: (\d+)' \ | ||
| 321 | | recs grep '$r->{time} > 5' \ | ||
| 322 | | recs totable | ||
| 323 | |||
| 324 | =head1 SEE ALSO | ||
| 325 | |||
| 326 | Each of the commands discussed have a C<--help> mode available to print out | ||
| 327 | usage and examples for the particular command. See that documentation for | ||
| 328 | detailed information on the operation of each of the commands. Also see some | ||
| 329 | other man pages: | ||
| 330 | |||
| 331 | =over | ||
| 332 | |||
| 333 | =item * Run C<recs examples> or see L<App::RecordStream::Manual::Examples> for a set of simple recs examples | ||
| 334 | |||
| 335 | =item * Run C<recs story> or see L<App::RecordStream::Manual::Story> for a humorous introduction to RecordStream | ||
| 336 | |||
| 337 | =back | ||
| 338 | |||
| 339 | =head1 AUTHORS | ||
| 340 | |||
| 341 | Benjamin Bernard <perlhacker@benjaminbernard.com> | ||
| 342 | |||
| 343 | Keith Amling <keith.amling@gmail.com> | ||
| 344 | |||
| 345 | Thomas Sibley <tsibley@cpan.org> | ||
| 346 | |||
| 347 | =head1 COPYRIGHT AND LICENSE | ||
| 348 | |||
| 349 | Copyright 2007–2017 by the AUTHORS | ||
| 350 | |||
| 351 | This software is released under the MIT and Artistic 1.0 licenses. | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | 1; |