grep Without cat, and Other Tips
grep is a command commonly used with pipes. This is not the only way it can be used though. One particular practice that appears common, is combining it with cat. This works, but...
If you do this:
$ cat games.txt | grep "chess"
You can probably do this:
$ grep "chess" games.txt
The first way:
faster to type
uses only one program
uses fewer resources / completes faster
Other Tips:
Inverted Matching
The "-v" flag will return everything except lines with the included search string.
-v, --invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
$ grep -v "something" file.log
Performing multiple rm commands through xargs
$ ls | grep -v ".log" | xargs rm
xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.
The above command will remove all files in the current directory except the ones with .log in their filename.
Counting the number of lines with a search term
$ grep -c "something" file.log
Finding only the filenames with the search term
he -l flag will search filenames rather than their contents.
$ grep -l "something" ./*
Finding filenames that don't contain the search term
The -L flag is the inverted matching for filename search.
$ grep -L "something" ./*
Case insensitive search
Use the -i flag to ignore case.
$ grep -i "something" crash.log