- Computers & Software»
- Operating Systems
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.
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.
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 ”
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
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.