The grep command

The grep command matches regular expression patterns against lines of input data. Regular expressions are a compact string syntax that can be used to describe patterns in text. They can be simple text strings, such as telnet, which will match any lines that have those six exact letters in sequence:

$ grep 'telnet' /etc/services
telnet   23/tcp
rtelnet  107/tcp    # Remote Telnet
rtelnet  107/udp
telnets  992/tcp    # Telnet over SSL
tfido    60177/tcp  # fidonet EMSI over telnet

We could use some regular expression properties to filter this data more precisely. For example, to limit the output only to lines that begin with the word telnet, we could use the ^ metacharacter:

$ grep '^telnet' /etc/services telnet 23/tcp telnets 992/tcp # Telnet ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.