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...
$ cat games.txt | grep "chess"
$ grep "chess" games.txt
The first way:
faster to type
uses only one program
uses fewer resources / completes faster
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
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.
$ grep -c "something" file.log
heĀ -l
Ā flag will search filenames rather than their contents.
$ grep -l "something" ./*
The -L
Ā flag is the inverted matching for filename search.
$ grep -L "something" ./*
Use the -i
flag to ignore case.
$ grep -i "something" crash.log