ANS Documentation

Improve This Doc
  • Cloud
  • Domains and DNS management
  • Backup and High Availability
  • eCommerce Stacks
  • Security
  • Email
  • Monitoring and usage management
  • Networking
  • Operating systems
    • Linux
      • Basics
      • Apache
      • Control Panels
      • FTP
      • Magento
      • Miscellaneous
      • MySQL
      • NGINX
      • PHP
      • PHP-FPM
      • Setting up Websites
      • NFS
      • VPN
      • Percona
      • SSH
      • SSL
      • Git
      • Redis
      • Elasticsearch
      • MongoDB
      • Ncdu
    • VMware ESXi
    • Windows
  • Webcelerator
  • MyUKFast
  • Home >
  • Operating systems >
  • Linux >
  • Miscellaneous >
  • The grep command

The grep command¶

The grep command’s main use is to search through files or filter output.

As per other commands, it’s best demonstrating its use with examples. Here is the contents of the file test.txt which we are going to use:

[root@c7 ~]# cat test.txt
line 1
line 2
line 3
Line 4
newline 5
NEWline 6
NEWLINE 7
new line 8

First of all we’re going to search the file for the word line:

[root@c7 ~]# grep line test.txt
line 1
line 2
line 3
newline 5
NEWline 6
new line 8

As you can see, all lines were displayed apart from line 7. This is because that line contains the word LINE rather than line. We can tell grep to ignore the letter case with the -i flag:

[root@c7 ~]# grep -i line test.txt
line 1
line 2
line 3
Line 4
newline 5
NEWline 6
NEWLINE 7
new line 8

This displayed all lines. Here is another example using -i:

[root@c7 ~]# grep -i LiNe test.txt
line 1
line 2
line 3
Line 4
newline 5
NEWline 6
NEWLINE 7
new line 8

Again, all lines were displayed.

To narrow this down further we could run:

[root@c7 ~]# grep 'line 1' test.txt
line 1

The single quotes allowed us to search for the exact phrase line 1.

Using grep with a ‘pipe’¶

Filtering output is another common use for grep. Again, here is what we get if we cat out the test.txt file:

[root@c7 ~]# cat test.txt
line 1
line 2
line 3
Line 4
newline 5
NEWline 6
NEWLINE 7
new line 8

The | symbol can be used to pipe the output of the last command into another command. We are going to pipe it into grep:

[root@c7 ~]# cat test.txt | grep line
line 1
line 2
line 3
newline 5
NEWline 6
new line 8

As you can see, this gives us the exact same output as grep line test.txt:

[root@c7 ~]# grep line test.txt
line 1
line 2
line 3
newline 5
NEWline 6
new line 8

Here we will use the command to filter out PHP modules for the xml module:

[root@c7 ~]# php -m | grep xml
libxml
xml

As you can see this matched two modules.

Next Article > Linux Log Files

  • Useful Links
  • SMB
  • Enterprise
  • Channel
  • Public Sector
  • ANS Data Centres
  • About ANS
  • Careers
  • Blog
  • Get in touch
  •  
  • Sales 0800 458 4545
  • Support 0800 230 0032
  • Get in touch

© ANS Group Limited | Terms and Conditions | Corporate Guidance | Sitemap
ANS Group Limited, registered in England and Wales, company registration number 03176761, registered office 1 Archway, Birley Fields, Manchester M15 5QJ