Unix Shell - How to use Sed command for Search and Replace more than one word in a file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > How to use Sed command for Search and Replace more than one word in a file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to use Sed command for Search and Replace more than one word in a file
rajashekar14@yahoo.com

2005-02-02, 5:55 pm

Hi,

How to use Sed command for Search and Replace more than one word in a
file. I know parital characters in the word for search criteria.

Let me exaplin you with a example.
The below line is the content of file Testfile.

"usmUserEntry localSnmpID user usmHMACMD5AuthProtocol
---------------------
usmDESPrivProtocol nonVolat"
------------------

Note: Underline words needs to be replaced.


case 1:
I want to replace 'HMACMD5Auth' of word "usmHMACMD5AuthProtocol" with
'NoAuth' . So the replaced word looks like "usmNoAuthProtocol".

Case 2:
I want to replace 'DESPriv' of word "usmDESPrivProtocol" with 'NoPriv'.
So the replaced word looks like "usmNoPrivProtocol".

Then final out of Sed command replaces 2 words of the file. the
resultant line in the file should look like below line

"usmUserEntry localSnmpID user usmNoAuthProtocol usmNoPrivProtocol
------------------ ---------------
nonVolat".


Note: The underlined word got replaced.

Can you give the syntax for repalcing these tow words in a file using
Sed command or anyother command

Stephane CHAZELAS

2005-02-02, 5:55 pm

2005-02-2, 06:01(-08), rajashekar14@yahoo.com:
[...]
> case 1:
> I want to replace 'HMACMD5Auth' of word "usmHMACMD5AuthProtocol" with
> 'NoAuth' . So the replaced word looks like "usmNoAuthProtocol".
>
> Case 2:
> I want to replace 'DESPriv' of word "usmDESPrivProtocol" with 'NoPriv'.
> So the replaced word looks like "usmNoPrivProtocol".

[...]

sed '
s/HMACMD5Auth/NoAuth/g
s/DESPriv/NoPriv/g
' < some-file

or:

sed 's/HMACMD5Auth/NoAuth/g; s/DESPriv/NoPriv/g' < some-file

or:

sed -e s/HMACMD5Auth/NoAuth/g -e s/DESPriv/NoPriv/g < some-file

--
Stéphane
rajashekar14@yahoo.com

2005-02-03, 2:48 am

Hi Stephane,
Thank you so much for your answer for this.
But the problem is it will be a static . what i mean to say is...
In the string "usmHMACMD5AuthPro=ADtocol" I only know part of string and
this 'Auth' and what i know is it starts with 'usm' and after 'usm' it
can be anyother words not just 'HMACMD5'. say for example there it can
be 'No' or 'Testing' so on. So what i meant is using of wildcharacter
there. because in between 'usm' and 'Auth' there can be any word. so i
need not static sed comman d to repalce but a wild character to do so.

Note: When i tried with the below sed command for second command was
failing. Hope you got what I am saying.
sed -e 's/usm[HN].*Auth.*/NoAuth/'

Rakesh Sharma

2005-02-03, 2:48 am



sed -e 's/\(usm\)[^ ]*\(AuthProtocol\)/\1No\2/g' \
-e 's/\(usm\)[^ ]*\(PrivProtocol\)/\1No\2/g' < yourfile

rajashekar14@yahoo.com

2005-02-03, 2:48 am

This worked.

Thanks Rakesh.

rajashekar14@yahoo.com

2005-02-09, 8:50 pm

Hi Stephane,
Thank you so much for your answer for this.
But the problem is it will be a static . what i mean to say is...
In the string "usmHMACMD5AuthPro=ADtocol" I only know part of string and
this 'Auth' and what i know is it starts with 'usm' and after 'usm' it
can be anyother words not just 'HMACMD5'. say for example there it can
be 'No' or 'Testing' so on. So what i meant is using of wildcharacter
there. because in between 'usm' and 'Auth' there can be any word. so i
need not static sed comman d to repalce but a wild character to do so.

Note: When i tried with the below sed command for second command was
failing. Hope you got what I am saying.
sed -e 's/usm[HN].*Auth.*/NoAuth/'

Rakesh Sharma

2005-02-09, 8:50 pm



sed -e 's/\(usm\)[^ ]*\(AuthProtocol\)/\1No\2/g' \
-e 's/\(usm\)[^ ]*\(PrivProtocol\)/\1No\2/g' < yourfile

rajashekar14@yahoo.com

2005-02-09, 8:50 pm

This worked.

Thanks Rakesh.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com