Thursday, August 13, 2020

Useful Linux commands to quickly analyze a log

 1)   Limit the output to 'n' characters per line in a search .

In the above example, it is limited to 400 bytes.

cat sample_file.log | cut -b 1-400 | less


2) In a large file, count the total lines starting with ^1 in the first column

 sed '1 d' FILE.csv | cut -d',' -f1 | sort | grep '^1*' | wc -l

sed '1 d' - delete the first line i.e. the header generally in a CSV

FILE.csv - File in question to analyze

cut -d',' -f1 -  Split the file using the ',' as a separator ('d','), and then retain the first column (f1)

sort - order the output in ascending manner

wc -l - total number of lines in the file

Tuesday, January 7, 2020

Python debugging

Some tips for debugging in python :

1) Always use an editor to modify code (VS code in my case). Python is very strict about indentation, and if you mess it up, it will complain and complain!

2) Two golden lines to help you debug :

import pdb
pdb.set_trace()

Add this into each file, at whatever position you want it to stop in the pdb interactive debugger.

3) Commands for debugging
l - prints a snippet of the lines of code before and after the current line.
n - to step over next function
s - to step into next function
c - to continue execution
enter key - to execute previous command
pp - to pretty print the object
p - to print
where - to print the stack trace during execution (so very useful)

Happy debugging!