Linux Classes
Share This With a Friend  

Linux Sed Command

Does Linux Have a Search & Replace Feature?

You can use the sed command to change all occurrences of one string to another within a file, just like the search-and-replace feature of your word processor. The sed command can also delete a range of lines from a file. Since sed is a stream editor, it takes the file given as input, and sends the output to the screen, unless you redirect output to a file. In other words, sed does not change the input file. The general forms of the sed command are as follows:

Substitution sed 's/<oldstring>/<newstri ng>/g' <file>
Deletion sed '<start>,<end>d' < file>

Let's start with a substitution example. If you want to change all occurrences of lamb to ham in the poem.txt file in the grep example, enter this:

sed 's/lamb/ham/g' poem.txt
Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a ham spamwich

In the quoted string, the "s" means substitute, and the "g" means make a global change. You can also leave off the "g" (to change only the first occurrence on each line) or specify a number instead (to change the first n occurrences on each line).

Now let's try an example involving deletion of lines. The values for start and end can be either a line number or a pattern to match. All lines from the start line to the end line are removed from the output. This example will delete starting at line 2, up to and including line 3:

sed '2,3d' poem.txt
Mary had a little lamb
Jill had a lamb spamwich

This example will delete starting at line 1, up to and including the next line containing Jack:

sed '1,/Jack/d' poem.txt
Jill had a lamb spamwich

The most common use of sed is to change one string of text to another string of text. But I should mention that the strings that sed uses for search and delete are actually regular expressions. This means you can use pattern matching, just as with grep. Although you'll probably never need to do anything like this, here's an example anyway. To change any occurrences of lamb at the end of a line to ham, and save the results in a new file, enter this:

sed 's/lamb$/ham/g' poem.txt > new.file

Since we directed output to a file, sed didn't print anything on the screen. If you look at the contents of new.file it will show these lines:

Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich

Use the man sed command for more information on using sed.

For more information on the sed command, see the sed manual.

Previous Lesson: Selecting Records
Next Lesson: Crunching Data



Comments - most recent first
(Please feel free to answer questions posted by others!)

Hassan     (02 Nov 2014, 15:51)
Hi i have a file contain:

1,2.5,Jan 10 2014 5:16:15:066AM,Jan 10 2014 5:16:15:066AM
2,3.5,Jan 10 2014 5:16:15:066AM,Jan 10 2014 5:16:15:066AM
i want to replace it by

1,2.5,Jan 10 2014 5:16:15.066AM,Jan 10 2014 5:16:15.066AM
2,3.5,Jan 10 2014 5:16:15.066AM,Jan 10 2014 5:16:15.066AM
can anyone help me please.thanks in advance.

Israel     (02 Apr 2013, 20:46)
Hi, I am trying to write a shell script using sed, and grep to look in /usr/share/applications and convert the list of *.desktop into a config file for jwm (Joe's Window Manager). I use grep to look in every .desktop file, and pull out the icon name into a file and sed to remove the non useful parts and insert the correct .jwmrc syntax.
So... my question is:
How do I take text at the beginning of a line and move it to the end?

make this line:
abiword</Program>:<Program icon="abiword_48.png" label=" ">exec

look like this:
<Program icon="abiword_48.png" label=" ">exec abiword</Program>

I think I should be able to get everything before the : and a(append) to the $(End of Line).
mohit saini     (24 Feb 2013, 11:41)
for remove all word "mohit" u can use this .....
sed 's/mohit//g' try.txt try1.txt

situ     (11 Dec 2012, 03:32)
hii @jerzy if u want to do it through sed try this
sed -e 's/\/\*pattern string line\*\//\/\*pattern string line/g' <filename>
Y Jagadeesh     (31 Oct 2012, 10:46)
@Jerzy:try this bellow command
grep -i '*/' file1 | cut -d'/' -f2 | sed 's/*/ /g'

Jerzy     (12 Oct 2012, 13:29)
I'd like to change line in file.txt
/*pattern string line*/
pattern string line
sed -i "s/\/*patern.*/pattern string line/" file.txt
replace line
/*pattern string line*/
/*pattern string line

Any idea?
<a href="">Forex Broker Comparison</a>     (06 Sep 2012, 10:17)
along with it; and, in short, I thought the best thing I could do unconscious instruments, and co-operation is the last thing dreamed When in May a white-edged, black cloud discharges a storm of hail upon
jonny     (05 Aug 2012, 20:41)
write an sed script each line is repeated 3 times
test     (17 Apr 2012, 17:25)
Mozi     (25 Mar 2012, 09:42)

I found the mistake.
I forgot the complete path when I assigned the content of the Rul.inf File to Variable Rul.
Here the solution:
Rul=`/bin/cat /usr/mtcl/Scripts/Rul.inf`
String="/bin/sed "s/Normal/$Rul/g" /usr/mtcl/Scripts/template.mgr"
echo $String > /usr/mtcl/Scripts/sed.log
$String > /usr/mtcl/Scripts/RulName.mgr
Mozi     (22 Mar 2012, 11:37)
Hi I use sed in a script:
Rul=`cat Rul.inf`
String="/bin/sed "s/Normal/$Rul/g" /usr/mtcl/Scripts/template.mgr"
echo $String > /usr/mtcl/Scripts/sed.log
$String > /usr/mtcl/Scripts/RulName.mgr

When I run the script manually it works
but when I run it with the crontab the sed comand dosen't use the content of $Rul

sed.log: after manually:
/bin/sed s/Normal/820/g /usr2/mtcl/Scripts/Vorlage.mgr

sed.log: after crontab:
/bin/sed s/Normal//g /usr2/mtcl/Scripts/Vorlage.mgr

Any idea what is wrong?

sugeng     (07 Mar 2012, 02:18)
Hi friends,
I have many rows, I'd like to delete the only zero values, example :
I only erase row number 2,4,5 using linux command
Thanks a lot
dan rabinowitz     (14 Feb 2012, 18:52)
with a VERY big file, thousands of row, hundreds of thousands of columns, is there a way to get a file with a list of the unique values in each column? maybe using awk?
many thanks!
ktn     (10 Feb 2012, 21:01)
hmmmm... 3 weeks and still not one comment.
So, it looks like sed could be very fast, but can be used only on fix pattern. a dynamic pattern search would be too complex for it.
Bob Rankin     (06 Feb 2012, 13:25)
@Marie, Your find syntax is wrong. Try this:

find /home/marie/Fotos -name "*.jpg" -ls

or without the -name parameter:

find /home/marie/Fotos -ls

This will lisdt all files with relative paths included.
Marie     (06 Feb 2012, 10:58)
Hi Bob, I am trying to find files that exist twice on my computer first in a folder called Videos and then in a folder called Fotos, both of them in my home. All files in the Video folder must be present in the Fotos folder, as I want to delete the Video folder. But in Fotos there are subdirectories and the files are distributed everywhere. I am trying this:

find . -exec find /home/marie/Fotos -name '{}' \;

But then I get the message, that Unix filenames do not contain slashes etc... and I see that the replaced names all begin with ./ as {} seems to give out the path and not only the file name.

I tried with grep but I do not seem to understand the syntax. Could you help?

EnterWeb Tech     (30 Jan 2012, 15:17)
SED -i "s/user = \'.*\'/user = '$DBUSER'/" configfile

With that setup it fails to match anything.
Bob Rankin     (30 Jan 2012, 15:09)
@Tech - Did you try escaping the single quotes with a backslash?
EnterWeb Tech     (30 Jan 2012, 15:04)
I am having trouble trying to use wildcards in the following way:
sed -i "s/$user = '.*';/$user = '$DBUSER';/" configfile

I basicly need to be able to change what is inside of the single quotes. What happens is that every set of single quotes gets replaced through out the entire file. What am I missing here?
ktn     (28 Jan 2012, 15:26)
I am looking for a dynamic search with sed. Sed should find in a text file words like waterlooo,acccommmodation which has an 'o' or 'c' or 'm' repeated 3 times don't let it through. But word like waterfall, accommodation is ok, because 'l' or 'm' or 'c' repeats correctly only twice, so let through into a new file. How could we do that in sed? Thanks in advance
Scott     (18 Jan 2012, 15:14)
I attempted to try the sed substitution examples on a .ppd printer file. I was trying to change the word "default" to "dpi600". What worked however was this:

sed -i s/default/600dpi/ /etc/cups/ppd/prt1.ppd

Great site, keep up the fantastic work!
Bob Rankin     (08 Jan 2012, 09:43)
@vimal - Sed is actually a small marsupial, a Narrow-nosed Planigale to be precise. See 704/narrownosed_planigale/
vimal     (07 Jan 2012, 22:43)
Hi, what is sed? how to use and example?
Bob Rankin     (03 Jan 2012, 08:16)
@Mithun, No problem, just use backslash "\" instead of slash "/" as the delimiter.

ex: sed -i "s\SRC\DEST\g" tools.txt
Mithun     (29 Dec 2011, 06:26)
Hi All,

I want to replace a string having '/' to a string having '/'. Is there any possibility of using sed command?

For example,

sed -i "s/SRC/DEST/g" tools.txt

where SRC= D:/tools
DEST= D/tools/plain/test
kalai     (26 Dec 2011, 08:52)
hi i need to extract 5th,10th and 17th line from a file using sed command,please help me
Lalit     (16 Dec 2011, 08:55)
Hi Guys,

I have Another Problem can anyone help me out...

I have two Files in this output is Coming like this-

And Also Second file have output like this-

We need to do sum taken from coloum to coloum with both sheet...
Like from first sheet we take 1 and from second sheet we take 10
and we need output like-
.and so on !!!!
kumar     (06 Dec 2011, 12:29)
hi guys,

i was trying with command line arguments in sed..
wanted to get the file, patterns as input string and wanted to replace the patterns in the file so my script was...

echo "enter file"
read file
echo "enter old pattern"
read old
echo "enter new pattern"
read new
sed 's/$o/$n/g' $f

kindly help me out...
Bob Rankin     (06 Dec 2011, 07:48)
@arunshory - Why not try it and find out??
arunshory     (06 Dec 2011, 06:32)

What is the output of "date -u +%W`uname`|sha256sum|sed 's/\W//g'"?

Buzz     (30 Nov 2011, 14:40)
How to change a strings in xml format

" <string>9999</string> " to " <string>8888</string> "

and also it will be great if I get option to get option to use a wild match like

" <string>*</string> " to " <string>8888</string> "
CNA Training Online     (22 Nov 2011, 22:41)
Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.
nasreen     (19 Nov 2011, 05:35)
salam and thank u for ur web
I want to add
2011 10 2356
to 48 txt files in linux without opening them
help me please
rakesh sreepathi     (14 Nov 2011, 02:22)
how to use sed command and their options in linux
Fatima     (03 Nov 2011, 19:38)
I would be very very appreciated if someone can help me on this

this should be really easy but I don't know what I am doing wrong

I have a file like this:
#PBS -N pore2_5_nc3_10
#PBS -l nodes=1:ppn=1
#PBS -l walltime=168:00:00
#PBS -S /bin/bash

I want to change the firs line as
#PBS -N pore2_5_nc3_20

can anyone use a variable string=20 and then use sed to get the job done

I have tried sed but I have two problems
1- sed print the result on STDOUT but doesnot change the file!
2-it replace 10 with $string instead of 20(the value of string)

Thanks in advance

Shynu Sivarajan     (21 Oct 2011, 09:11)
hi all,
i need to do a simple task.
i have 2 files file1.txt and file2.txt

file1.txt has follwing content:
i woh

and file2.txt has:

i need the output as:

i woh

i need to replace the 'replace' string with another file2.txt content with that order

thanks in advance..!!
Rex     (15 Oct 2011, 08:52)
Thanks Bob for your help. You have no idea how much work this saved me.

Gedit treats a paragraph of many sentences as one line. Finding the capital letter at the first of every sentence is difficult when you have almost 130,000 words.

You suggested: sed 's/\. \([a-z]\)/. \u\1/g' 'test of novel.txt' >nov1sed.txt

This searched for a period at the end of a sentence and then the space.

I put apostrophes around a pseudo file and had to use > to direct sed to make a new file.

I found thirty such errors.

Thanks again Bob.
shailendra khade     (01 Oct 2011, 18:34)
Hi I want to change/replace some content in html files(nearby 100 html pages),
1) Delete tag <html> </html>
2) Replace xyz to empty (blank)
3) This should done at once
I have try lots of thing with sed but not succeed
Lokesh     (29 Sep 2011, 02:23)
Found that sed works well for replacing existing text. But, it doesn't return any error if the string doesn't exist.
E.g. If the a string HOME_PATH is not there in a config file, I want to add it. If it's there, based on need, I want to change the value.
Is there a way to achieve this using sed?
Bob Rankin     (14 Aug 2011, 13:44)
@Rex, try this:

sed 's/^[a-z]/\u&/g' test.txt

This tells sed to look for lines that start with a lowercase letter and capitalize it.
Rex     (30 Jul 2011, 19:44)
I use gedit to write a novel. I'd like to find the occasional lower case letter starting a sentence and convert it to upper case. I believe gedit treats a paragraph of many sentences as one line that wraps. Thanks
Syed najam Imtiaz     (21 Apr 2011, 01:39)
Thanks to sharing ,It is really very helpfull.
Can you tell me how to search and delete lines using sed?
Marcus     (04 Apr 2011, 10:00)
Thanx Bob it did the trick =)
have a good week

Bob Rankin     (04 Apr 2011, 09:57)
@Marcus - Close, but not quite. Try this:

TODAY=`date +%Y%m%d`
sed -i "s/2010.*/$TODAY; /g" pri.domain
Marcus     (04 Apr 2011, 09:26)
Hi, im strugling with sed and to change the date/serial in pri.domain file.

$TTL 86400
@ IN SOA (
2010120502 ; serial, todays date + todays serial #
28800 ; refresh, seconds
7200 ; retry, seconds

i whant to change "20101205" to todays date and add 01 on the end of the date.

i try sed -i 's/2010*/"$TODAY; "/g' pri.*
but it dident work.

Best regards
kadosakek     (22 Mar 2011, 14:32)
Great Post. I add this Post to my bookmarks.
Ravi     (03 Mar 2011, 17:08)
Thanks all of you. This forum has provided good piece of information.
khodadi     (23 Feb 2011, 23:41)
I have a file that have several colomn unsefully that there are several colomn usefull between
them.I want to delete them with useing
sed command but one time.
thank you
Elizabeth     (02 Feb 2011, 16:29)
I wanted help on how to search for the end of a function and need to place two lines before the function ends. Is it possible using the sed command? Thanks
Soypeescacy     (25 Jan 2011, 19:46)
Very Interesting Information! Thank You For Thi Blog!
charitha     (24 Jan 2011, 15:13)
Hi, I got to split the lines of a file as follows.
Input file has text like this.
A, B, C, D, (and so on).
Output file has to be like this.
A, B
A, C
A, D

How can I do this? I know I can use sed command for this but don't know how to implement. Need help. Thanks in advance.
ImmimeLig     (24 Jan 2011, 08:28)
Very Interesting Blog! Thank You For Thi Blog!
GrouchyGaijin     (24 Jan 2011, 07:14)
I'm trying to do the examples listed in the article, but when I try to direct the output to a file all I get is an empty file.

Any idea what is wrong?
Fealuenda     (10 Jan 2011, 23:25)
Very informative post. Thanks for taking the time to share your view with us.
Ajendra     (07 Jan 2011, 03:36)
How to search for the words irrespective of its case
eg: Rollback, ROLLBACK, rollback
Paul Cobb     (02 Jan 2011, 17:43)
Have been looking at SED documention but need a little poin the in the right direction

I have 200 files I want to modify in a batch
Source is html file
Need to create a new file for the changes

Want to delete the firat part of the file up to the first </tr> tag (This is 20 or so lines but can vary slightly)
Then insert the contents of a reference file (the same for all files) into the new target file. The number of lines does not match the number that are deleted though.

Hope you can help

muhtar     (28 Dec 2010, 23:24)
sed 's:^:data/:' could you please tell me the what is function of this code one by one ?
Ramanath     (23 Nov 2010, 11:03)
I want to get the RGB values alone in the below text specified, and filter out the unique values of it. please help me out in this

framework/ Color backGround = new Color (82, 113, 115); //106,131,147
framework/ private Color fgc = new Color (57, 78, 79);
framework/ this.setBackground ( new Color(106,131,147));
framework/ = new Color (107, 134, 148);
framework/ = new Color (90,97,115);
Hemant     (11 Nov 2010, 07:17)
How to find the next string of the pattern/string?
srinivas     (30 Oct 2010, 07:36)
hi fnds am a fresher now...plz send me which r the topics are most useful in project
rubi     (28 Oct 2010, 02:45)

u should try this cmd:
cut -c 1-10 filname

this will cut first ten characters which r ur dates
rubi     (28 Oct 2010, 02:31)
sed 's/$fruit/$newfruit/g' $sinname > trysin
jacklyn u should change ur stment vth above one
Bob Rankin     (19 Oct 2010, 10:38)
@Jacklyn - Change the single quotes to double quotes and it will work!
Jacklyn     (19 Oct 2010, 10:32)

I am trying to search for a string in a file and replace it with a new value. The original string is input by the user and then replaced and saved to a new file:

echo "please enter the file name"
read sinname
echo "Please enter your fruit"
read fruit
sed -e '/$fruit/s/$fruit/$NewFruit/g' $sinname > try.SIN

This does not change the old fruit value to the new fruit value!! please help!
rahul     (14 Oct 2010, 03:10)
I want to add '' in a file
Say : 9823,2,5,2010-10-15
want to change in

Pls help
Kid     (14 Oct 2010, 02:30)
So what if my search pattern is present in multiple places in the file?

Consider a file like

... <lots of veggies here>
cucumber apple carrot
banana apple tomato
potato apple banana
...<lots of veggies here too>

I want to change the "apple" in the line starting with banana to "onion", but not in the other lines.

konnina     (11 Oct 2010, 05:54)
Hi N.Seshaphani, this version of sed will suite you

sed -i '/21\/06/d' filename

with -i you can replace the content of your file ar else you can redirect the output of the command in a new filename (sed '/21\/06/d' filename > newfilename)
N.Seshaphani     (05 Oct 2010, 06:57)

We have a large file it contqains more no of lines all the lines are starting with below
how to delete the line based on the dates which are appearing lines.

Any body can pull out from this...

thanks in advance
konnina     (29 Sep 2010, 04:14)
I use sed to find a string between 2 other strings and replace it but it does work. I could use some help!
res=`sed -n '/string1/,/string2/p' file.xml`
sed -i "s/$res/newstring/g" file.xml
Bob Rankin     (24 Sep 2010, 11:07)
@jeffrey - You can use sed to make the changes, escaping the slashes as follows:

sed 's/\//\\/g'
jeffrey     (23 Sep 2010, 14:53)
I am trying to convert a mac path from the server to a PC path so I can paste it on a email and when they open up the email they can click on the link and it will take to to the file.

Can you show me how to do it change // and / to \\ and \ together.
barrank     (11 Sep 2010, 18:52)
A bit late mabye, but I think your problem can be solved with the "dos2unix" command. I sugest you check it out. It replaces the "\r\n" (car return & new line) line terminators (used by windows programs) with "\n" (new line).
HateIgnorant     (06 Aug 2010, 09:26)
@Tom there's no need to be rude!!! loser
tom     (14 Jul 2010, 16:36)
I'd get an English certificate first.
srinivas     (13 Jul 2010, 02:33)
can some one acknowlege me how linux certficate course is done intrested in certiificate course
Satyadeep     (07 Jul 2010, 00:48)
Thanks Dinesh Sehra for your info. I spent lot of time searching web but didnt find how to replace a string with a variable.
Karunanithi     (05 Jul 2010, 09:27)
I tried with single quotes.I got Correct Output.
File V have the value Karunanithi

d=`date +%d%m%y`
echo $d
cat V | sed 's/nithi/'$d'/g'
cat V | sed 's:nithi:'$d':g'
Dinesh Sehra     (26 Jun 2010, 05:10)
For solution to Mallik's question:
sed s:searchstring:"$var":g

The "/" in the var are causing problems. The idea is any single character after "s" in sed, ":" here, will be used as separator. I have changed the separator from "/" to ":".
Mallik     (23 Jun 2010, 02:34)
I tried placing the variable in quotes.


sed s/searchstring/"$var"/g

But didn't work.
I tried various combinations by trial and error, bit didn't get it.

Please suggest any suitable solution.
Mallik     (22 Jun 2010, 23:38)
Hi, how can we replace a string with a date variable using the sed command in unix?

Carola     (27 May 2010, 17:37)
This blog is very useful! Thanks you so much
vadiraj     (05 May 2010, 21:57)
Thanks Bob & Lyle.
Its working.
Lyle     (05 May 2010, 12:42)
you can use a '?' character as the separator between IS and BECOMEs on a substitution:

echo foo/bar | sed 's?/?BBB?'

Change the character / between foo and bar to 3 upper case 'B's... use the ? instead of / in your sed expression. (Same works in vi/vim etc)
Bob Rankin     (05 May 2010, 06:24)
You have to escape the symbols with a blackslash. For example:

sed 's/\//xyz'
vadiraj     (04 May 2010, 22:39)
Can you please tell me how I can replace symbols. For exaple I want to raplce "/" to "xyz"
Suraj     (01 May 2010, 02:54)
nice one thanks
zeyad jabri     (19 Apr 2010, 03:00)
shokran kteer = thank u very much
clrs     (24 Mar 2010, 01:38)
the best ever linux tutorial online...thanks a zillion :)
Dennis     (17 Mar 2010, 09:11)
It works just fine.

I'm the real Dennis not the one from above :)))
Camus     (17 Mar 2010, 01:36)
Thanks! It works fine in DOS prompt too.
Sanjib     (04 Mar 2010, 18:32)
Good site
Dennis     (04 Mar 2010, 15:44)
what sed syntax to remove <return> at the end of the line with ;...
"C:\\", "E:\\", "VSS ASR DISK:\\",

to continuous data with only one <return> at the end of each line... example
Sanjib Dey     (23 Feb 2010, 02:54)
Thanks for the Kool post :)

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.