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
- Display File
- Pipe Output to findstr command
- 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
- Display File
- Pipe Output to grep command
- 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 \|
- For regular expression
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.