I’m far away from being a Linux guru and honestly I like it that way. In order to be effective as QA you need to have minimal knowledge how to do certain things under Linux. This post is devoted on working with logs.
Chaining commandsLinux offers a possibility to combine several commands by chaining them. In current post I will just one of them, the PIPE (I) operator. By using it output of one command is used as input for other.
I would strongly recommend to read following post if you are interested in chaining Linux commands: 10 Useful Chaining Operators in Linux with Practical Examples.
Useful commandsCommands bellow are one I use on daily basis when working with logs. I will show basic usage, if you need more detail on certain command then you can type: man <command> e.g. man cat in Linux console and it will display you more information.
grepIt is used to search in text files or print lines matching some pattern. Usage is: grep text filename.log . If text contains spaces it should be wrapped around single quote (‘) or double quote (“). If text contains single quote, then you have to wrap it around with double quote and vice versa.
catPrint file content on standard out put. Usage is: cat filename.log . You can concatenate several files: cat file1.log file2.log . Drawback using this command is when you have large files. It combines very well with grep to search output of the file: cat filename.log | grep text .
zcatPrint content of zipped file. Usage: zcat filename.gz. Combines with grep: zcat filename.gz | grep text .
tailPrints last 10 lines from a file. Usage: tail filename.log . Most valuable tail usage is with -f option: tail -f filename.log . This monitor file in real time and outputs all new text appended to the file. You can also monitor several files: tail -f file1.log file2.log .
lessUsed for paging through a file. It shows one page and with arrow key up and down you can scroll into the file. Usage: less filename.txt . In order to exit just type q . Valuable with this command is that you can type a search term /text and then with n go to next appearance and with N go to previous.
Colours Commands above are nice, but using colours aid for much better perception of information in the files. In order to use colours perl -pe command will be used as chained command to colour the output of commands described above. Syntax is: perl -pe ‘s/^.*INFO.*$/\e[0;36;40m$&\e[0m/g’ . It is quite complex expression and I will try to explain it in details.First is ^.*INFO.*$ which is a regular expression what needs to be matched. Character ^ means from beginning of the string or line, character $ means to end of string or line. Group .* matches any character. So this regular expression means inspect every string or line and match those that contain INFO .
Second comes the colouring part \e[0;36;40m . 0 is value for ANSI escape code. Possible values for escape code are: Code Colour 0 Reset / Normal 1 Bold or increased intensity 2 Faint (decreased intensity) 3 Italic: on 4 Underline: Single 5 Blink: Slow 6 Blink: Rapid 7 Image: Negative 8 Conceal 9 Crossed-outMore codes can be found in ANSI escape code wiki .
36 from \e[0;36;40m is colour code of text. Colour depends and is different based on escape code. Possible combinations of escape and colour codes are: Code Colour Code Colour 0;30 Black 1;30 Dark Grey 0;31 Red 1;31 Light Red 0;32 Green 1;32 Lime 0;33 Dark Yellow 1;33 Yellow 0;34 Blue 1;34 Light Blue 0;35 Purple 1;35 Magenta 0;36 Dark Cyan 1;36 Cyan 0;37 Light Grey 1;37 White One possible colour scheme I like is: cat application.log |perl -pe ‘s/^.*FATAL.*$/\e[1;37;41m$&\e[0m/g; s/^.*ERROR.*$/\e[1;31;40m$&\e[0m/g; s/^.*WARN.*$/\e[0;33;40m$&\e[0m/g; s/^.*INFO.*$/\e[0;36;40m$&\e[0m/g; s/^.*DEBUG.*$/\e[0;37;40m$&\e[0m/g’ which will produce following output:
Conclusion
Having coloured logs makes it much easier to investigate logs. Linux provides tooling for better visualisation so it is good if we take advantage of those.
If you find this post useful, please share it to reach more people. Sharing is caring!

