Linux Guide: An Introduction to grep

A Brief Introduction

Linux is an operating system that is very powerful and offers many great tools. It is a free operating system and due to its reliability, is used as the backbone of the Internet.

When you need a new piece of software, whether it be a game, web browser,a Linux onscreen keyboard, or a Linux driver for your video camera, you can quickly and easily search for it and download it from the repositories. If you are using Ubuntu, it is simply a matter of firing up the Synaptic Package Manager and preforming a search using the name of the software or what it does.

For me, one of my favorite parts of Linux is how useful and powerful the command line, or terminal, is. There are literally thousands of different tools to do pretty much anything you could want, from converting audio or video files, to analyzing the security of a network.

One such command line tool that is very powerful and useful is grep. Which is typically used to search a file and find out where a specific phrase is. It can be very useful, especially when you are editing a new wordpress theme or working on a web server and need to find where a specific phrase is.This can be especially useful if you usea web host that allows you to SSH into their web server.

Technical Jargon Disclaimer

What follows includes a lot of Linux Specific Technical Jargon, so if you are unfamilar with Linux, it probably won't make too much sense.

The purpose of this Hub is to provide a grep tutorial, which provides a basic introduciton to the grep command. It will be followed up with a more advanced Hub that shows how to use some of the other many neat features of grep.

If you have any questions or can think of any improvements, please leave a comment.

Getting Started

Before we begin, lets make a text file to search. Open up your favorite text editor and copy the following information, including the Line Number, into text file.

Line 1: asdf
Line 2: pasta
Line 3: We the people of the United States of America
Line 4: How you have felt, O men of Athens, at hearing the speeches 
Line 5: of my accusers, I cannot tell; but I know that their 
Line 6: persuasive words almost made me forget who I was
Line 7: Lasagna 

Now, save the file as “test.txt” on your desktop. Open up a terminal and goto the desktop. If you are using Ubuntu, you can goto: Applications → Accessories → Terminal, type “cd Desktop,” and press return.

The Basics:

Lets start slowly and I will show you just a few of the ways you can use grep.

At its most basic, you can use two arguments; the word you are searching for and the file you want to search.

1. Type: grep pasta test.txt
 
   This should print: Line 2: pasta
 
 
2. Type: grep Lasagna test.txt 
 
   This should print: Line 7: Lasagna 

Showing the Line Numbers:

Now in our example, test.txt, we included the line numbers in our text file. However, in the real world you will seldom encounter this, but luckily grep includes a way of printing the line numbers, by including the “-n” option.

1. Type: grep -n pasta test.txt 
 
   This should print: 2:Line 2: pasta

 
2. Type: grep -n Lasagna test.txt
 
   This should print: 7:Line 7: Lasagna

Note that both times, the line number is printed first, followed by a full colon. Due tothe way hubpages does its formatting, your line numbers could vary though.

Showing the File Name:

One of the great things about grep is that you can use it to search multiple files at once. However, in the above examples, you would not be able to tell where the word came from. To show the file name, just add the -H option.

1. Type: grep -n -H pasta test.txt 
 
   This should print: test.txt:2:Line 2: pasta 
 

Note that it prints the name of the file, the line number of the phrase, and then the line that contains the phrase.

This next command is a little different and uses the '*' operator. In Linux, the '*' operator is used to match any phrase. In this context, we are telling grep to search any text file that is on your desktop.

2. Type: grep -n -H  Lasagna *.txt 

The results for number 2 will depend on how many text files you have on your desktop, that contain the word “Lasagna”, but it should at least print: “test.txt:7:Line 7: Lasagna ”

Ignoring Case:

As I am sure you know, Linux is case sensitive and by defualt so is grep. However, by including the -i command, you can tell grep to ignore the case of the letter.

1. Type: grep -n -H lasagna *.txt 

 
   This should not return any results, unless you happen to have
     a text file with lasagna in it. 
 
2. Type: grep -n -H -i lasagna *.txt
 

   This should print: test.txt:7:Line 7: Lasagna

Learning More:

These are just a few of the many ways that you can use grep. There are a number of other ways you can search a file or files using grep. I will do another Hub on advanced grep techniques shortly, but in the mean time, you can read the grep manual to learn more. Simply type “man grep” into the terminal to see the many options available.

More by this Author


Comments 4 comments

rlakies profile image

rlakies 7 years ago

i hate grep its always been a pain to use. no wonder i failed the linux cert :)


brad4l profile image

brad4l 7 years ago from USA Author

Thanks for stopping by and commenting rlakies. It takes some getting used to, but grep is a very powerful tool.

Sorry to hear that you failed your Linux Certification, it can be a really nice thing to have on your resume as it can be very attractive to some potential employers.

I find that I use it a great deal when I need to quickly find a piece of html or php code that might occur in a lot of different files. I think a lot of people who use shared hosting that allows them to SSH into their server, don't know the power that is available to them.


nicomp profile image

nicomp 7 years ago from Ohio, USA

Excellent intro to this useful utlility.

I have heard several explanations for the acronym "grep";

"General Regular Expression Processor", "General Regular Expression Parser", and others.

Do you know the history of it?


brad4l profile image

brad4l 7 years ago from USA Author

Thanks nicomp, great question!

grep stands for Global Regular Expression Print and was originally a function in a text editor called "ed."

In "ed", the "Global Regular Expression Print" function  was used to search files for specific patterns called regular expressions. For example, you could search the file for all words that start with the letter "a."

Today, regular expressions still play a very important role in grep and are what makes grep such a powerful search tool. I intend to go into a little more detail on regular expressions and how they can be used in grep in one of my next few hubs.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working