Filtering Text By Line / Remove All Lines Except Ones That Contain A String

Usually I need to find and replace strings with other strings, which is easy to do using your standard “Find and Replace” function in your text editor. But, oftentimes I also need to find certain lines of text and remove everything EXCEPT them.  Trying to write a regex to search and replace everything BUT a particular string is complicated. An easier way is to just use the unix command “grep”. For example, if you want to extract all lines in a file called report.txt that contain the word “time” in them, you can do

$ grep ‘time’ report.txt

If you want to output the result of this filtering to a file, you can run

$ grep ‘time’ report.txt > filtered-report.txt

If you want to extract certain pieces of text in each line of a file called report.txt where those pieces of text are offset from the beginning of each line based on certain delimiters, e.g. commas, tabs, spaces, then you can use the awk command.