From 98cb2ca30e96b8ef7c6cf1e626314d34126cb645 Mon Sep 17 00:00:00 2001 From: Michael Diamond Date: Thu, 23 Apr 2020 11:58:24 -0700 Subject: [PATCH] Updated SC2126 (markdown) --- SC2126.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/SC2126.md b/SC2126.md index ea31897..731aa33 100644 --- a/SC2126.md +++ b/SC2126.md @@ -20,7 +20,7 @@ Instead of: grep foo *.log | wc -l ``` -Pipe all the file contents into `grep` (passing the files directly to `grep` causes `-c` to print each file's count separately, rather than the total): +You can pipe all the file contents into `grep` (passing the files directly to `grep` causes `-c` to print each file's count separately, rather than the total): ```sh cat *.log | grep foo -c @@ -30,11 +30,10 @@ cat *.log | grep foo -c This is purely a stylistic issue. `grep` can count lines without piping to `wc`. -Note that in many cases, this number is only used to see whether there are matches (i.e. `== 0`). In these cases, it's better and more efficient to use `grep -q` and check its exit status: +Often this number is only used to see whether there are matches (i.e. `== 0`). In these cases it's clearer and more efficient to use `grep -q` and check its exit status: ```sh -if grep -q pattern file -then +if grep -q pattern file; then echo "The file contains the pattern" fi ``` @@ -43,4 +42,4 @@ Also note that in `foo | grep bar | wc -l`, wc will mask the exit code of grep b ### Exceptions -If you e.g. want to count characters instead of lines, and you actually care about the number and not just whether it's greater than 0, you can [[ignore]] this error. +If you find piping to `wc` is clearer in a given situation it's fine to [[ignore]] this error.