colorful console output by unix pipe filter - linux

while examining the console output and logging messages of different software it is sometimes difficult to keep the overview. It would be much easier to make the output colorful and highlight the text phrases which are currently important.
Is there a program for Linux/UNIX shell which could be used as a filter by utilizing unix pipes to make the console output colorful according to predefined patterns and colors?
p.ex.
pattern definition:
INFO=green
WARN=yellow
ERROR=red
\d+=lightgreen
to highlight the severity of the message and also numbers.
usage:
$ chatty_software | color_filter
11:41:21.000 [green:INFO] runtime.busevents - SensorA state updated to [lightgreen:17]
11:41:21.004 [green:INFO] runtime.busevents - SensorB state updated to [lightgreen:20]
original output:
11:41:21.000 INFO runtime.busevents - SensorA state updated to 17
11:41:21.004 INFO runtime.busevents - SensorB state updated to 20

we use a sed script along these lines:
s/.* error .*/^[[31m&^[[0m/
t done
s/.* warning .*/^[[33m&^[[0m/
t done
:done
and invoke it by
sed -f log_color.sed
I guess you could do something similar?

Related

using awk and bash for monitoring exec output to log

I am looking for some help with awk and bash commands,
my project have an embedded (so very limited) hardware,
i need to run a specific command called "digitalio show"
the command output is:
Input=0x50ff <-- last char only change
Output=0x7f
OR
Input=0x50fd <-- last char only change
Output=0x7f
i need to extract the input parameter and convert it into either Active or Passive and log them to a file with timestamp.
the log file should look like this:
YYMMDDhhmmss;Active
YYMMDDhhmmss;Passive
YYMMDDhhmmss;Active
YYMMDDhhmmss;Passive
while logging only changes
The command "digitalio show" is an embedded specific command that give the I/O state at the time of the execution, so i basically need to log every change in the I/O into a file using a minimal tools i have in the embedded H/W.
i can run the command for every 500msec, but if i will log all the outputs i can finish the flash very quickly, so i need only log changes.
in the end this will run as a background deamon.
Thanks !
Rotem.
As far as I understand, a single run of digitalio show command outputs two lines in the following format:
Input=HEX_NUMBER
Output=0x7f
where HEX_NUMBER is either 0x50ff, or 0x50fd. Suppose, the former stands for "Active", the latter for "Passive".
Running the command once per 500 milliseconds requires keeping the state. The most obvious implementation is a loop with a sleep.
However, sleep implementations vary. Some of them support a floating point argument (fractional seconds), and some don't. For example, the GNU implementation accepts arbitrary floating point numbers, but the standard UNIX implementation guarantees to suspend execution for at least the integral number of seconds. There are many alternatives, though. For instance, usleep from killproc accepts microseconds. Alternatively, you can write your own utility.
Let's pick the usleep command. Then the Bash script may look like the following:
#!/bin/bash -
last_state=
while true ; do
i=$(digitalio show | awk -F= '/Input=0x[a-zA-Z0-9]+/ {print $2}')
if test "$i" = "0x50ff" ; then
state="Active"
else
state="Passive"
fi
if test "$state" != "$last_state" ; then
printf '%s;%s\n' $(date '+%Y%m%d%H%M%S') "$state"
fi
last_state="$state"
usleep 500000
done
Sample output
20161019103534;Active
20161019103555;Passive
The script launches digitalio show command in an infinite loop, then extracts the hex part from Input lines with awk.
The $state variable is assigned to whether "Active", or "Passive" depending on the value of hex string.
The $last_state variable keeps the value of $state in the last iteration. If $state is not equal to $last_state, then the state is printed to the standard output in the specific format.

ftrace: Only print output of trace_printk()

Is it possible to dump only trace_printk() outputs in trace file? I mean filter out all functions in function tracer (or any other tracer).
In general, you can switch options off inside of the options directory, /sys/kernel/debug/tracing/options. Use ls to display all toggle-able options.
# ls
annotate context-info funcgraph-abstime funcgraph-overhead func_stack_trace hex overwrite record-cmd sym-offset trace_printk
bin disable_on_free funcgraph-cpu funcgraph-overrun function-fork irq-info printk-msg-only sleep-time sym-userobj userstacktrace
blk_classic display-graph funcgraph-duration funcgraph-proc function-trace latency-format print-parent stacktrace test_nop_accept verbose
block event-fork funcgraph-irqs
Toggle options via echo, i.e.,
echo -n "1" > /sys/kernel/debug/tracing/options/trace_printk
If you are trying to filter out any output that was not produced by trace_printk(), you would likely need to ensure that trace_printk() is the only option set.
It's always good to check out the kernel documentation when in doubt. There's also a great lwn article that helped me out when I was first learning ftrace called Secrets of the Ftrace function tracer, which includes some sections about filtering in general.
For some who cannot get more informations from this thread...
Actually, if you use nop as an current_tracer, you will ignore all the trace except trace_printk.
do
echo nop > current_tracer
and run your kernel module or anything else which includes your trace_printk.

Is there a printk-style log parser?

The journald of systemd supports kernel-style logging. So, the service can write on stderr the messages starting with "<6>", and they'll be parsed like info, "<4>" - warning.
But while developing the service it's launched outside of systemd. Is there any ready-to-use utilities to convert these numbers into readable colored strings? (it would be nice if that doesn't complicate the gdb workflow)
Don't want to roll my own.
There is no tool to convert the output but a simple sed run would do the magic.
As you said journal would strip off <x> token from the beginning of your log message and convert this to log level. What I would do is check for some env. variable in the code. For ex:
if (COLOR_OUTPUT_SET)
printf ("[ WARNING ] - Oh, snap\n");
else
printf ("<4> Oh, snap\n");

After OutPut displayed in linux , how can i see from begining

This is my Output using system.out.println which i got in Linux Server .
The problem is taht , i am not able to view the output from serial number 1 , as the below occupies the entire screen .
please tell me how can i scroll to top ??
341:allitems: tq_relation
342:allitems: tr_num
343:allitems: trader_id
344:allitems: tradetick
345:allitems: trading_state
346:allitems: treas_shrs
347:allitems: treas_stk
348:allitems: treasury_yield
349:allitems: trend
350:allitems: uask_quote
351:allitems: uask_quote_date
352:allitems: ubid_quote
353:allitems: ubid_quote_date
354:allitems: under_cusip
355:allitems: undersymbol
356:allitems: unique_symbol
357:allitems: unit_measure
358:allitems: unpriced
359:allitems: unsolicited
360:allitems: valoren
361:allitems: value_pr_shortinterest
362:allitems: value_shortinterest
363:allitems: vega
364:allitems: vl
365:allitems: vol
366:allitems: volatility12
367:allitems: vwap
368:allitems: wanted_off_bid_ind
369:allitems: wk52hi
370:allitems: wk52hidate
371:allitems: wk52hidate_t
372:allitems: wk52lo
373:allitems: wk52lodate
374:allitems: wk52lodate_t
375:allitems: wkinprog
376:allitems: xchg
377:allitems: xdate
378:allitems: xday
379:allitems: xmonth
380:allitems: xyear
381:allitems: yield
The Power of Linux is that you are able to redirect in and output of certain commands to other commands as using the output of program 1 as input for program 2. This can be archived by using:
program1|program2
and more specific you could use a pager like less or more. With the less command you are able to scroll back and forth and search specific patterns in the output. The more command displays the output page wise. The next page of the file is displayed by hitting the enter key. For your purpose all you have to to is using.
myprogram|less
or
myprogram|more
whatever you prefer. The other approach is to redirect the output to a file. You can do this with the Redirection Operator > and
myprogramm > log.txt
will log the output log.txt.
There is even a third method using the script command. When you are typing
script log.txt
at least in bash this would open a subshell and every input and output that is will get logged to log.txt. You can close and afterwards access the logile by using exit. In the last two cases you can view the log.txt file with you favorite text editor or pager.
You can redirect the output of you program to a text file:
$ ./myprogram >output.txt
Then view the text file with any text editor you like.
You can also pipe the result to the more command:
$ ./myprogram | more

Getting specific fields from ID3 tags using command line tool?

I'm looking for a way that would let me get specific fields from ID3 tags from mp3 files.
All tools I have so far found return all fields, and they also format them for "easier reading". I need just some fields, and formatted differently (artist\talbum\ttitle\n) for reporting purposes.
Is there any such tool? I would love tool that would let me output separately values from ID3v1 and ID3v2.
id3v2 -R sounds like it does what you want. Debian package name is id3v2, upstream is http://id3v2.sourceforge.net/
From the manpage:
-R, --list-rfc822
Lists using an rfc822-style format for output
Example:
$ id3v2 -R 365-Days-Project-04-26-sprinkle-leland-w-the-great-stalacpipe-organ.mp3
Filename: 365-Days-Project-04-26-sprinkle-leland-w-the-great-stalacpipe-organ.mp3
TALB: Released independently through Luray Caverns
TPE1: Leland W. Sprinkle
TIT2: The Great Stalacpipe Organ
COMM: ()[eng]: � 2004, Copyright resides with the artist, The 365 Days Project, and UbuWeb (http://ubu.com) / PennSound (http://www.writing.upenn.edu/pennsound/). All materials at UbuWeb / PennSound are available for free exchange for noncommerical purposes.
365-Days-Project-04-26-sprinkle-leland-w-the-great-stalacpipe-organ.mp3: No ID3v1 tag
The easiest way is creating a bash script.
grep the fields returned by your tool so you get just the ones you want. Then you use awk (if you know how to use it), or cut, etc.
If you give us the format used by one of the tools you found, we can help you to write it. The more simple the format is, the more simple the script will be.

Resources