GREP command

grep command line tool

grep stands for global regular expression print.

It is a very commonly used tool to match patterns from files.

It can be also used with stdin.

Examples :

Create a text file and insert the following text :

one 
two 
three

Now to capture the text two :

grep two filename.txt

Now suppose the user entered two in block letters :

one 
TWO 
three

To make the search case insensitive :

grep -i two filename.txt

Counting the number of occurences :

Dummy json data : filename -> data.json

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "email": "alice@example.com"
    },
    {
      "id": 2,
      "name": "Bob",
      "email": "bob@example.com"
    },
    {
      "id": 3,
      "name": "Charlie",
      "email": "charlie@example.com"
    },
    {
      "id": 2,
      "name": "Bob",
      "email": "bob@example.com"
    },
    {
      "id": 4,
      "name": "Diana",
      "email": "diana@example.com"
    },
    {
      "id": 1,
      "name": "Alice",
      "email": "alice@example.com"
    }
  ],
  "products": [
    {
      "product_id": 101,
      "name": "Laptop",
      "price": 1200
    },
    {
      "product_id": 102,
      "name": "Phone",
      "price": 800
    },
    {
      "product_id": 103,
      "name": "Tablet",
      "price": 500
    },
    {
      "product_id": 101,
      "name": "Laptop",
      "price": 1200
    }
  ]
}

To count the number of occurences of alice@example.com.

grep -i -c alice@example.com data.json