Working with log files that contains variable length messages wrapped between open and close tags it may be useful to filter the messages upon a keyword. This works fine with GNU sed version 4.2 or higher, so pay attention to some unix distros (solaris, hp-ux, etc.). Linux should be ok. Show Sample Output
Thanks to knoppix5 for the idea :-)
Print selected lines from a file or the output of a command.
Usage:
every NTH MAX [FILE]
Print every NTH line (from the first MAX lines) of FILE.
If FILE is omitted, stdin is used.
The command simply passes the input to a sed script:
sed -n -e "${2}q" -e "0~${1}p" ${3:-/dev/stdin}
print no output
sed -n
quit after this many lines (controlled by the second parameter)
-e "${2}q"
print every NTH line (controlled by the first parameter)
-e "0~${1}p"
take input from $3 (if it exists) otherwise use /dev/stdin
{3:-/dev/stdin}
Show Sample Output
This line does not include your closing tag in the output. Show Sample Output
Show's per IP of how many requests they did to the Apache webserver
The first sort is necessary for ips in a list to be actually unique.
Sometimes commands give you too much feedback.
Perhaps 1/100th might be enough. If so, every() is for you.
my_verbose_command | every 100
will print every 100th line of output.
Specifically, it will print lines 100, 200, 300, etc
If you use a negative argument it will print the *first* of a block,
my_verbose_command | every -100
It will print lines 1, 101, 201, 301, etc
The function wraps up this useful sed snippet:
... | sed -n '0~100p'
don't print anything by default
sed -n
starting at line 0, then every hundred lines ( ~100 ) print.
'0~100p'
There's also some bash magic to test if the number is negative:
we want character 0, length 1, of variable N.
${N:0:1}
If it *is* negative, strip off the first character ${N:1} is character 1 onwards (second actual character).
Show Sample Output
Applying filter rules is what makes this a really useful command. It's usually a pain to figure out how to sync ONLY files matching a particular pattern, and often one reverts to goofy stuff like find .. -exec rsync .. The filter hides all folders from the transfer, so that only the matching folders that store the filename are left for the sync.
Commandline perl filter for, using a production.log from a rails app, display on realtime the count of requests grouped by "seconds to complete" (gross round, but fair enough for an oneliner) :) Show Sample Output
avoiding UUOC! cut can handle files as well. No neet for a cat.
by default, will output the whole line on which 'word' has been found
Removes all lines between the lines containing "" and "", including these lines itself Backdrop: Sometimes when working with XML files without an graphical editor, large comment-/annotation-blocks taper the readability to walk through the file. I like to create a copy of such documents without these annotations. As the documentation itself is in documentation tags inside the annotation tags an therefore graphical editors tend to put the annotation tags in their own lines, this command removes all documentations within annotation-tags. Show Sample Output
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: