How to color text in log files in linux - linux

I want to add colors to the text in Log files. For e.g. I would want lines that contain text 'ERROR', to be red colored. So that when I view that file, these I should be able to easily find those lines with 'ERROR'. I tried looking for the answer to question but couldn't find anything helpful.
Thanks in advnace.

So that when I view that file, these I should be able to easily find those lines with 'ERROR'.
Coloring those lines would be one way but there's a much simpler and more idiomatic way:
$ grep ERROR /path/to/logfile | less
will show you every line containing ERROR from /path/to/logfile in less.

Someprogrammerdude suggested to use ability of viewers to colorize output. It is called 'syntax highlighting' in vim ecosystem but not only there.
The simplest thing you can do in vim is:
:sy match my_error /.*ERROR.*/
:hi my_error ctermfg=red guifg=red
You can add these lines to your .vimrc or may better is to create a special syntax file for your log files where you can define more rules...

I wrote a utility for coloring log files called TxtStyle. It can color log files based on regex patterns defined in a config file ~/.txts.conf:
[Style="example"]
!red: regex("error")
green: regex("\d{4}-\d\d-\d\d")
# .. snip ..
To try it out, run (requires Python):
sudo pip install TxtStyle
wget -q https://raw.githubusercontent.com/armandino/TxtStyle/master/example.log
txts -n example example.log

You could use ccze to color the files:
e.g.:
docker logs -f <container> | ccze -m ansi

Related

Vim code formating with astyle (formatprg in vimrc)

I'm trying to use astyle as my code formatter in Vim. However, I can't seem to find how to tell vim which options to use for astyle.
The following is in my vimrc:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\
If given no options in command line mode, astyle will try to find an options file named .astyle. However this does not seem to work here. (Ie: when I format in vim: gqG the result is totaly different than if I had called astyle from the command line !)
This is my .astyle options file:
--style=allman
--mode=c
--attach-classes # -xc
--attach-closing-while # -xV
--indent-classes # -C
--keep-one-line-blocks # -O
--keep-one-line-statements # -o
--align-pointer=name # -k3
So I tried to simply specify the options in my vimrc like in this answer:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ --style=allman --mode=c -xc -xV -C -O -o -k3
Saved, sourced, reloaded vim entierly: no change. The options seem to not take effect. When I format with gggqG the result is still not what my options ask for... Can anyone see why this is not working ?
(Note: I want to be able to format using gq and I don't mind reformatting the whole file entierly each time, unlike in this question)
EDIT:
After a few stupid mistakes I've escaped all the spaces as suggested in #romainl comment. However there seems to be an error comming from vim which I cannot interpret:
/bin/bash: -c: line 1: syntax error: unexpected end of file
shell returned 1
E485: Can't read file /tmp/vHXZmnp/3
Since no one is answering, I'll share what I managed to come up with.
Following the advice given in the comments I escaped all the spaces in the sequence of options passed to astyle. However, this leads to bash trying to interpret the options, failing and kindly telling us so: see question edit.
The solution that seems to work is to create a system wide astyle options file and pass the path to that using command line arguments instead of giving it the options directly.
(This can probably also be done with a project options file but requires having an options file at the root directory of every project)
Taking my options file .astylerc in my home directory this gives:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ --options="/home/myusername/.astylerc"\
Note that a relative path did not work, but it is possible to write the path to the options file using a 'pseudo relative' path using the $HOME environment variable as mentioned in the astyle documentation

linux vi mess up after set prompt and ls --color

I have set my ls color and less like
alias ls='ls --color'
LS_COLORS='no=00:fi=00:di=00;94:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:'
export LS_COLORS
alias less='less -R'`, and my `prompt` like `
export PS1="\n\[\e[31m\]\u\[\e[33m\]#\[\e[34;1m\]\h \[\e[36m\]\W \[\e[35m\]^\$ \[\e[0m\]"
.
Which I think is better to see on my monitor, but when I do something like ls > OUTPUTFILE or even cat SOMEFILEthen I use vi to see the output file, it has messed up like
^[[00mNC_000954.fna^[[00m
^[[00mNC_000955.fna^[[00m
^[[00mNC_000956.fna^[[00m
^[[00mNC_000957.fna^[[00m
^[[00mNC_000958.fna^[[00m
^[[00mNC_000959.fna^[[00m
^[[00mNC_000961.fna^[[00m
It should be like
NC_000117.fna
NC_000853.fna
NC_000854.fna
NC_000868.fna
NC_000907.fna
NC_000908.fna
NC_000909.fna
without those special symbols.
What's wrong with that? I don't know which color setting has changed this. BTW, I've set my vimrc color as colorscheme delek
My 2 cent worth :
Well, the colour setting is handled with escape sequences in the terminal window. If you pipe stdout to a file, and have requested colours, you should get those escape sequences in there.
It seem as though vim does not appreciate those escape sequences in your present configuration. By default it makes them visible.
ls: note that ls --color=auto it will not send to file only when stdout is terminal.
maybe you have --color=always on. Make sure you have auto and the escapes goes away.
If that is your intent - to actually get ls colours in vim, then check this link for more discussion. You may coax vim to actually treat them as escapes and filter them away - or colourize.

compare two files and save the difference in linux

I like to compare two text files and save the difference under linux.
I know there are tools like kdiff, diff vimdiff etc. but my expectation are as follows.
Output should be in a separate file
The difference should be quoted with colours, ex: delete line in red and added line in green something like that
It should ignore space differences
It should be an opensource tool
use tkdiff4 -w file-name1 file-name2
It fulfills all your requirements. Specific color might be an issue.
try colordiff and man diff for options for ignoring whitespace etc
Like,
#!/bin/bash
wdiff -w "\e[31m" -x "\e[0m" -y "\e[32m" -z "\e[0m" "$#";
replace \e by, well, the ASCII character with value 0x1A. Put the two commands into some file, and run it using redirection.
Save the changes to a file:
diff -Nur originalfile newfile > patchfile
Use the difference file to change the origin file:
patch originfile patchfile
I think this is the easiest way to save the changes and reload the changes.
By the way, you can use this command the create an update-package.

help - change diff symbol "<", "|" or ">" to a desired one?

diff -w command is used to create a side by side comparison diff file (instead of parallel)
i then view them using vi via ssh terminal
the changes are indicated by either "<" or "|" or ">"
Since the file i am viewing is a source code, navigating to changes alone
using above symbols is difficult since they are also in C source code.
How can i change these default symbols to desired ones ?
Kindly help. Thanks.
Instead of viewing the output of diff -w in vim, you can use vim's built-in diff:
vim -d file1 file2
This opens vim in a vertical split with both files open, and diff markings in the code. This is what it looks like:
And it works in a terminal too:
You can find a short tutorial here
According to my version of diff (2.8.1 from the GNU diffutils by the FSF) -w is used to change the width of the output; The -y parameter outputs side by side comparison. In combination, the two show no further effect than the -y parameter used alone, which means you may have an alias in your terminal profile or in the global terminal profile that aliases diff to diff -y.
I say all this because all options to change the symbols ("<", "|", and ">") conflict with the -y option. If you can live without side-by-side, you have the option of two other included output styles or defining your own. The two output styles are -c (context) and -u (unified). (For more information on what they do see the diff Wikipedia page. For more information on the options see the diff man page.)
A more in depth fix would be to use the following options:
diff --old-group-format="(deleted)---" \
--new-group-format="(added)---" \
--changed-group-format="(updated)---" \
--unchanged-group-format="(nodiff)---" \
old_file.c new_file.c
Now the old file's lines that are not present in the new file are represented by (deleted)---
The new file's lines that are not present in the old file are represented by (added)---
Lines that have been changed are represented by (updated)---
Lines common to both files are represented by (nodiff)---
Since you seem to do this often enough, you have the option of making it an alias in your terminal profile or writing a small shell script to handle it. For more options, see the manual's section on options and specifically see the section on line group formats for information on what you can put between the quotes in the format definitions.
Of course, if you must have side-by-side, try Nathan Fellman's idea above. Otherwise, there's the option of using a dedicated GUI tool for it such as Kompare.

vi, find line and output/output to separate file

I'm looking for a command in VI/VIM to search for particular text in the file and grab the whole line and output in either on the screen or separate file. e.g.
This is some line with this _word_ and some other text.
This is some line with this some other text and some other text.
so this would output only the first line..
:redir > output.txt
:g/_word_/p
:redir END
The line will be output to the screen and to output.txt. See :h :redir.
EDIT: I agree with others who suggest using plain old *nix grep if you can. Situations where you might not be able to use grep:
You're searching buffer text that doesn't exist in a file on the filesystem.
You're using Vim-specific regex extensions, like cursor location, column number, marks, etc.
You want do this in a cross-platform way, and grep might not exist on the system you're using.
redir can be useful in these situations.
use the g (global) command:
:g/_word_/y
will yank all lines containing _word_
having mentioned the DOS find command, you probably want to use grep:
grep -h '_word_' * > results
in vi, from command mode
search for _word_
/_word_
yank the line
yy
paste the line
p

Resources