Tag: grep

  • Linux: grep, part0

    The grep command is a Linux shell one for finding strings.

    The following is by my understanding.

    The grep command is used to find a string and will print out each line that contains it.

    grep “log” a-file.txt

    will print, to terminal, each line from a-file.txt that contains the string “log”.

    grep log a-file.txt

    seems to work the same as if “log” is in quotes.

    The grep command can be used with regular expressions. For example,

    grep -E gre”e|a”t a-file.txt

    will find “great” or “greet” in a-file.txt. The -E switch means you don’t need to escape the | operator.

    Source:

    linux.die.net

    -JS

  • Linux terminal commands: showing non-match

    Sometimes you want to weed out the “cannots.”

    The following is as I recall it.

    Yesterday I was in a server file system using the Linux terminal. I wanted to know about the folders the terminal could access, not the ones it couldn’t. Yet, there were more it couldn’t.

    I pipelined the command into grep:

    command | grep -v “cannot”

    but it was still showing the “cannot” lines.

    I learned from another source that the reason is grep only works on standard output, file handle 1 in the terminal, whereas “cannot” typically will come from standard error, file handle 2.

    When I tried

    command 2>&1 | grep -v cannot

    I got what I was hoping for. Apparently 2>&1 feeds standard error into standard output, so grep can work on it.

    Interesting, eh?

    Source:

    stackoverflow.com

    man7.org

    -JS