Search Text On The Terminal

Background

For us who spend a lot of time on the command line in terminal node, we always have to sort through data returned through applications.

The find and grep command comes in handy.

Text File

Let us use a simple text file.

namelist.txt

john
tom
sallie
sally
bob
felix
frank
paul
john
saul

 

Search

Platform

MS Windows

findstr command

Outline
  1. Display File
  2. Pipe Output to findstr command
  3. Pass along words to search for
    • For regular expression separate words by spaces

 

Syntax

type <file> | findstr "word1 word2"

 

Sample

type namelist.txt | findstr "bob sall"

Output
Output – Image

Output – Textual
 

>type namelist.txt | findstr "bob sall"
sallie
sally
bob

 

MS Windows

findstr command

Outline
  1. Display File
  2. Pipe Output to grep command
  3. Pass along words to search for
    • For regular expression
      • Use the -e option
      • Word Divide
        • separate words by |
        • escaped by \
        • As | is a special word in UNIX \ Linux, please escape it by prefixing it with the backslash character ( \ )
      • That is use \|

 

Syntax

cat <file> | grep -e "word1\|word2"

 

Sample

cat namelist.txt | grep -e "bob\|sall"

Output
Output – Image

Output – Textual
 

>cat namelist.txt | grep -e "bob\|sall"
sallie
sally
bob
>

 

Summary

In MS Windows, we have the find and findstr commands.

Findstr lets us use regular expressions.

In Unix\Linux, please use grep or egrep.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s