Useful cli tools - grep and tee
Last month I was working on a big project, that consists of multiple pipelines, complicated envirinment settings, and I found that I use several tools all the time.
grep
I often use it with env, for example I need to check whether current devcontainer has access to azure, I need to check if i have access to AZURE__DATABASE__CONNECTION_STRING.
I could easily find out it with:
env | grep AZURE__DATABASE__CONNECTION_STRING
envshows all environment variables|is a pipe, it takes output of theenvand puts it as input to the next command -grepgrepfilters the input, and only shows lines that containAZURE__DATABASE__CONNECTION_STRING
It is still a bit combursome, so I use a simplified version usually:
env | grep -i ^azure
-imakes it case-insensitive^matches the start of the line So, it will show all environment variables that start withazure(case-insensitive). Pretty cool and saves a lot of time!
tee
Because of complicated pipelines and AI result, I often put logs, but analizing it directly in terminal is not that comfartable so I use tee. It looks like this:
python -m src.application.main | tee ./_tmp/1818_v0.log
python -m src.application.mainruns the application code. It produces tons of logs, some of which I need to analyze.|is a pipe, it takes output of the previous command and puts it as input to the next command -teeteeduplicates the input, puts it to the console, and also writes it to the file. So I see my logs as usual in terminal but at the same time they are saved to the file!./_tmp/1818_v0.logis the file where logs are saved.
I can modify command with:
python -m src.application.main 2>&1 | tee -a ./_tmp/1818_v0.log
2>&1redirects stderr to stdout, so it puts all logs, not only stdout-aappends to the file, so I can rerun the same command multiple times and it will append new logs to the file, instead of overwriting the file.