Problem solve Get help with specific problems with your technologies, process and projects.

Using grep and sed to find and replace

Learn how to use grep and sed to search for a string of text within a file and to replace a string.

This article covers two beneficial Linux tools, grep and sed. If searching for or handling text, grep and sed can...

increase your efficiency with Linux bash scripting and configuration files. We'll learn how to use these commands and get some helpful tips, too. Using Grep Need to find a single matching line in a 300 page script or a large configuration file that you've customized? Don't have the time to search line by line to find that single bug that is halting your system when trying to start the service? You can search for a string quickly using grep.

You can search with grep (an acronym for searching globally for lines matching the regular expression, and print them) many of the files on a Linux machine. When you give it a list of files to read, it will search for lines of text that match the expressions you're looking for and, if it find any, it will output the pieces you want. Just think of as a grip: using grep is a great way to grab a hold of specific lines or files. This command line utility that was originally designed for Unix systems.

For example, let's say we have a list of numbers in a file, numlist.txt, and we want to see if the number 8723 is in the list without searching for hours. Using grep can easily find the string in the following command line:

grep –i 8723 numlist.txt


If we get an output of 8723, then it is in the list. If we do not get an output, it is not there. The tool will print the whole line where the text string is found. If the list contained 8723 297r, we would get:

[[email protected] root]# grep 8723 numlist.txt 8723 297r [[email protected] root]#


Grep is case sensitive by default. To disable this, use the –i flag.

Here's something useful in a daily RHEL system checkup. Say you need to find out which version of sendmail rpm you installed. To list which rpm packages are installed on the system, you enter the following:

rpm –qa


If you have a system like mine, you have just too many packages to sort through. It might take forever to go through them line by line to find the package you're looking for. Wouldn't it be easier to find a line with a text string matching the word "sendmail?" Thankfully, we can use grep like this:

rpm –qa | grep sendmail

Which should give you:

[[email protected] root]# rpm -qa | grep sendmail sendmail-8.13.1-3.RHEL4.5 [[email protected] root]#


Don't know if the string is capitalized or not? Just turn off the case-sensitive default:

rpm –qa | grep –i sendmail


Here's another example of how handy grep is. If we want some information on a process that is running on our server, we would run the command ps, which returns a fairly large list of running processes. To find the certain process that we're looking for, put the grep command after ps. Say I need to make sure my DHCP Server is running. I'll run the following:

ps ax | grep –i dhcp


You should see something like the following:

3058 ? Ss 0:00 /usr/sbin/dhcpd -cf /etc/dhcp/dhcpd.conf -q -pf /var/run/dhcp/ -user dhcp -group dhcp


All set. Our DHCP server is running thanks to the simple and efficient grep.

Using sed

The command sed (stream editor) is used to take a file, read a line, and change the line as specified by the command. This is another former UNIX utility available today in most Linux distributions.

Let's say we have a file called sedtest.txt, which contains the string "1.0". We want to modify this file, changing the string to "2.0". We can do this by doing the following:

sed -e 's/1.0/2.0/g' sedtest.txt > sedtest2.txt mv –f sedtest2.txt sedtest.txt


In this command line, s stands for substitute and g stands for global. Sedtest.txt is the input file (the one we're changing) and sedtest2.txt is the file we've created from the changes of sedtest.txt. The second command takes the output file and renames it to the input file name (replacing the changes made)

Besides substitution, there are other things sed is useful for. For example, the following deletes empty lines or lines that only contain spaces:

sed -e '/^ *$/d' sedtest1.txt


Notice how this one does not need an output file.

Just like the Replace function in Word and Wordpad, sed is the Linux version of this handy tool.


Dig Deeper on Linux servers