how to match exact word in sed ?

3,327 views
Skip to first unread message

Vandan Joshi

unread,
Jun 22, 2009, 7:15:00 AM6/22/09
to VG...@googlegroups.com
hello friends,

This is my sample.txt file
-------------
(1) this is sample
(2) that is also a sample.11
(3) here is sample-22
(4) i am no gg.sample
(5) i am no sample_ee
-------------

Now I wan to replace the exact 'sample' word by word 'image'.

So I write like this
$sed 's/\<sample\>/image/gi' sample.txt

and i m getting is below
-------------
(1) this is image
(2) that is also a image.11
(3) here is image-22
(4) i am no gg.image
(5) i am no sample_ee
-------------

As I wanna replace exactly the 'sample' word, but sed replaces 2nd, 3rd and 4th lines also.

So, wt shud i do to replace only 'sample' ?? not with '.' or '-' chars ..... ????

thnx


Shakthi Kannan

unread,
Jun 22, 2009, 7:46:23 AM6/22/09
to VG...@googlegroups.com
Hi,

--- On Mon, Jun 22, 2009 at 4:45 PM, Vandan Joshi<vandan...@in.com> wrote:
| So, wt shud i do to replace only 'sample' ?? not with '.' or '-' chars .....
| ????

\--

Maybe?

$ sed 's/ sample /image/gi' sample.txt

SK

--
Shakthi Kannan
http://www.shakthimaan.com

bhavin mehta

unread,
Jun 22, 2009, 8:22:14 AM6/22/09
to VG...@googlegroups.com
Hi
sed 's/\<sample\>/image/ig' sample.txt


Bhavin

Nilesh J. Vaghela

unread,
Jun 22, 2009, 2:35:19 PM6/22/09
to VG...@googlegroups.com
Hi,

This is perfect. Small correction

bhavin mehta wrote:
> Hi
> sed 's/\<sample\>/image/ig' sample.txt

when you use "i" means ignore case if you want to match exact word do
not use "i"

--
Nilesh Vaghela
ElectroMech
Redhat Channel Partner and Training Partner
Ahmedabad: 404,Maulik Arcad,Nr. Mansi cross Road,Satellite Rd
Baroda : 25, The Emperor, Fatehgunj
www.electromech.info


vilas jagani

unread,
Jun 23, 2009, 1:21:09 AM6/23/09
to VG...@googlegroups.com
hi ,

use this syntax sed -e '1s/sample/image/g' , it wil replace 1st occurance of pattern .

Regards
Vilash Jagani

--- On Mon, 6/22/09, Nilesh J. Vaghela <elect...@electromech.info> wrote:

Shakthi Kannan

unread,
Jun 23, 2009, 1:25:30 AM6/23/09
to VG...@googlegroups.com
Hi,

--- On Tue, Jun 23, 2009 at 10:51 AM, vilas


jagani<vilas_ja...@yahoo.com> wrote:
| use this syntax sed -e '1s/sample/image/g' , it wil replace 1st occurance of
| pattern .

\--

Because you know that the matched pattern is occurring first, you are
suggesting the above. That may not be the case, everytime.

Nilesh J. Vaghela

unread,
Jun 23, 2009, 7:23:26 AM6/23/09
to VG...@googlegroups.com
Vandan Joshi wrote:

You are correct.


> hello friends,
>
> This is my sample.txt file
> -------------
> (1) this is sample
> (2) that is also a sample.11
> (3) here is sample-22
> (4) i am no gg.sample
> (5) i am no sample_ee
> -------------

Actually the special char are considered as end or start of the words.

Let me check and let you know.

bhavin mehta

unread,
Jun 23, 2009, 7:04:15 AM6/23/09
to VG...@googlegroups.com
Hi

sed -i 's/image/sample/g' sample.txt

Bhavin

bhavin mehta

unread,
Jun 23, 2009, 7:05:44 AM6/23/09
to VG...@googlegroups.com


On Tue, Jun 23, 2009 at 4:34 PM, bhavin mehta <bhavin....@gmail.com> wrote:
Hi
sorry it was typing mistake
 
use this command
 

sed -i 's/sample/image/g' sample.txt

Vandan Joshi

unread,
Jun 23, 2009, 9:21:19 AM6/23/09
to VGLUG
hello bhavin
I have tried without 'i' also, but getting the same results....
any other way ??


---------- Original message ----------
From:Nilesh J. Vaghela< elect...@electromech.info >
Date: 23 Jun 09 00:05:19
Subject: [VGLUG] Re: how to match exact word in sed ?
To: VG...@googlegroups.com


Hi,

This is perfect. Small correction

bhavin mehta wrote:
> Hi
> sed 's/\/image/ig' sample.txt

bhavin mehta

unread,
Jun 23, 2009, 9:24:18 AM6/23/09
to VG...@googlegroups.com
Hi

try this command
 I checked twice here it is working


sed -i 's/sample/image/g' sample.txt

Bhavin


Vandan Joshi

unread,
Jun 23, 2009, 9:31:31 AM6/23/09
to VGLUG

which distro u r using ??
i am using "Red Hat Enterprise Linux Server release 5 (Tikanga)"

ur suggested command is not working 4 me.

here is my terminal output....

=========================================================
[root@localhost Desktop]# sed -i 's/sample/image/g' sample.txt ; cat sample.txt

this is image
that is image.test
i am 11-image
hu ha image.33
ha ha hya ha image-testing

[root@localhost Desktop]#
============== ===========================================

Shakthi Kannan

unread,
Jun 23, 2009, 9:35:22 AM6/23/09
to VG...@googlegroups.com
Hi Vandan:

--- On Tue, Jun 23, 2009 at 7:01 PM, Vandan Joshi<vandan...@in.com> wrote:
| ur suggested command is not working 4 me.

\--

I thought I already gave you the solution:

$ sed 's/ sample /image/gi' sample.txt

There is a space on either side of 'sample'.

Vandan Joshi

unread,
Jun 23, 2009, 9:36:35 AM6/23/09
to VGLUG

hey shakthimaan,
thnx 4 the ans but its not working 4 me here

i m using RHEL-5

below is my output

=========================================================
[root@localhost Desktop]# sed -e 's/ sample /image/gi' sample.txt
this is sample
sample is sample
that is sample.test
i am 11-sample
hu ha sample.33
ha ha hya ha sample-testing
[root@localhost Desktop]#
=========================================================

---------- Original message ----------
From:Shakthi Kannan< shakt...@gmail.com >
Date: 23 Jun 09 19:05:22
Subject: [VGLUG] Re: how to match exact word in sed ?
To: VG...@googlegroups.com


Hi Vandan:


--- On Tue, Jun 23, 2009 at 7:01 PM, Vandan Joshi wrote:
| ur suggested command is not working 4 me.
\--

I thought I already gave you the solution:

$ sed 's/ sample /image/gi' sample.txt

There is a space on either side of 'sample'.

SK

--
Shakthi Kannan
http://www.shakthimaan.com



Nilesh J. Vaghela

unread,
Jun 23, 2009, 3:47:00 PM6/23/09
to VG...@googlegroups.com
Hi,

Vandan Joshi wrote:

Should not work.

>
> hey shakthimaan,
> thnx 4 the ans but its not working 4 me here
>
> i m using RHEL-5
>
> below is my output
>
> =========================================================
> [root@localhost Desktop]# sed -e 's/ sample /image/gi' sample.txt
> this is sample
> sample is sample
> that is sample.test
> i am 11-sample
> hu ha sample.33
> ha ha hya ha sample-testing
> [root@localhost Desktop]#
> =========================================================

as sample.test REGEX consider it as a two word.
Similler for all others which includes "-" , "." etc.

So including space is not solution because than the simple "sample" word
will also not match if it start or end without space.

:(

Interesting one as always with Mr. Vandan !!

Shakthi Kannan

unread,
Jun 23, 2009, 10:26:02 AM6/23/09
to VG...@googlegroups.com
Hi,

--- On Wed, Jun 24, 2009 at 1:17 AM, Nilesh J.


Vaghela<elect...@electromech.info> wrote:
| So including space is not solution because than the simple "sample" word
| will also not match if it start or end without space.
|
| :(

\--

Right. I had used a file where 'sample' had a space on either side,
and hence thought it will work in all cases! Anyways, put the
following in a file, say, sed.txt:

=== sed.txt ===

s/[[:space:]]\+sample[[:space:]]\+/ image /gi
s/[[:space:]]\+sample$/ image/gi
s/^sample[[:space:]]\+/image /gi

=== END ===

The first will match " sample ". The second will match " sample" at
the end of the line. The third will match "sample " if it begins from
a new line. Now invoke your sed using:

sed -f sed.txt sample.txt

Nilesh J. Vaghela

unread,
Jun 24, 2009, 6:49:47 AM6/24/09
to VG...@googlegroups.com
Hi,

:)

Working great.

Shakthi Kannan wrote:
> Hi,
>
> --- On Wed, Jun 24, 2009 at 1:17 AM, Nilesh J.
> Vaghela<elect...@electromech.info> wrote:
> | So including space is not solution because than the simple "sample" word
> | will also not match if it start or end without space.
> |
> | :(
> \--
>
> Right. I had used a file where 'sample' had a space on either side,
> and hence thought it will work in all cases! Anyways, put the
> following in a file, say, sed.txt:
>
> === sed.txt ===
>
> s/[[:space:]]\+sample[[:space:]]\+/ image /gi
> s/[[:space:]]\+sample$/ image/gi
> s/^sample[[:space:]]\+/image /gi
>
> === END ===
>

The perfect solution !!!!

Vandan Joshi

unread,
Jun 24, 2009, 5:19:07 AM6/24/09
to VGLUG

Thnk u nilesh sir,

I have tried a lot for the same but didnt find the idea abt grouping of sed statments.
becoz every time single condition goes true, and my solution lakes of the other conditions.

anyway, thnx 1s again.

---------- Original message ----------
From:Nilesh J. Vaghela< elect...@electromech.info >
Date: 24 Jun 09 16:19:47
Subject: [VGLUG] Re: how to match exact word in sed ?
To: VG...@googlegroups.com


Hi,

:)

Working great.

Shakthi Kannan wrote:
> Hi,
>
> --- On Wed, Jun 24, 2009 at 1:17 AM, Nilesh J.

Nilesh J. Vaghela

unread,
Jun 24, 2009, 12:32:28 PM6/24/09
to VG...@googlegroups.com
Hi Vandanbhhai,

Vandan Joshi wrote:
>
> Thnk u nilesh sir,
>
No , thanks to Mr. Shakthi :).

> Shakthi Kannan wrote:
> > Hi,
> >
> > --- On Wed, Jun 24, 2009 at 1:17 AM, Nilesh J.
> > Vaghela wrote:
> > | So including space is not solution because than the simple
> "sample" word
> > | will also not match if it start or end without space.
> > |
> > | :(
> > \--
> >
> > Right. I had used a file where 'sample' had a space on either side,
> > and hence thought it will work in all cases! Anyways, put the
> > following in a file, say, sed.txt:
> >
> > === sed.txt ===
> >
> > s/[[:space:]]\+sample[[:space:]]\+/ image /gi
> > s/[[:space:]]\+sample$/ image/gi
> > s/^sample[[:space:]]\+/image /gi
> >
> > === END ===
>

He had given the perfect solution.

Thanks to Shakthi.

Vandan Joshi

unread,
Jun 27, 2009, 1:17:20 AM6/27/09
to VGLUG
thnk u sahkthimaan,
thnx 4 the solution.

---------- Original message ----------
From:Nilesh J. Vaghela< elect...@electromech.info >
Date: 24 Jun 09 22:02:28
Subject: [VGLUG] Re: how to match exact word in sed ?
To: VG...@googlegroups.com


Reply all
Reply to author
Forward
0 new messages