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)
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 ...im
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 >cat T.sh ###--------------------- d=`date +%d%m%y` echo $d cat V | sed 's/nithi/'$d'/g' cat V | sed 's:nithi:'$d':g' ###-------------------------- >sh T.sh 050710 Karuna050710 Karuna050710
Dinesh Sehra (26 Jun 2010, 05:10)
For solution to Mallik's question:
Use 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.
var=31/03/2010 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)
Vadiraj:
you can use a '?' character as the separator between IS and BECOMEs on a substitution: echo foo/bar | sed 's?/?BBB?' fooBBBbar 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.
P.S. 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:\\", "VSS SYSTEM FILESET:\\", "VSS OTHER:\\", "VSS SYSTEM SERVICES:\\", "VSS SYSTEM BOOT:\\"; to continuous data with only one <return> at the end of each line... example "C:\\", "E:\\", "VSS ASR DISK:\\", "VSS SYSTEM FILESET:\\", "VSS OTHER:\\","VSS SYSTEM SERVICES:\\", "VSS SYSTEM BOOT:\\";
Sanjib Dey (23 Feb 2010, 02:54)
Thanks for the Kool post :)
|
|
|
![]() |
|
| <Send This Link to a Friend> <Bookmark This Page> | ||