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
  • env shows all environment variables
  • | is a pipe, it takes output of the env and puts it as input to the next command - grep
  • grep filters the input, and only shows lines that contain AZURE__DATABASE__CONNECTION_STRING

It is still a bit combursome, so I use a simplified version usually:

env | grep -i ^azure
  • -i makes it case-insensitive
  • ^ matches the start of the line So, it will show all environment variables that start with azure (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.main runs 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 - tee
  • tee duplicates 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.log is 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>&1 redirects stderr to stdout, so it puts all logs, not only stdout
  • -a appends 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.