Grep Tips

Earlier this year I drank the proverbial kool-aid and switch completely over to linux. As a result of that I've been trying to be more intentional with improving my linux tools knowledge. Here's some notes about grep! These tools are dense, so if you have any tips or tricks, send me a message!

  grep bananas foo.txt

  -i # case insensitive
  -o # print only matching part, not whole line
  -A 3 # 3 lines after match
  -B 3 # 3 lines before match
  -BA 3 # 3 lines before and after match
  -E # enables regex
  -v # invert the match
  -l # only show filenames (useful when greping dir)
  -r # recursive search all files in dir

  grep bananas fruit/ -lr

To use with watch, you'll need to enclose the piped command in quotes..

  watch -n 2 'kubectl get pod | grep podname'

Grep and xargs make good friends, especially when you're trying to do something to a bunch of files

  ls | grep test | xargs rm

There's also ripgrep, which is a (faster) alternative to grep written in rust (so it must be better)! Most of the stuff above works the same with rg.