Operating System - Linux
1830201 Members
31194 Online
109999 Solutions
New Discussion

How to replace a string in a particular line using SED

 
Dave Faupel
New Member

How to replace a string in a particular line using SED

Hi guys,

I have a file(text) file and want to replace a part of string with another string. I've got SED running on a Win XP PC but are having trouble with the syntax.....

For example
*************
File contents

ProductKey= "aaaaa-aaaaa-aaaaa-aaaaa-aaaaa"

The above line is somewhere in the middle of the file,which contains around 100 lines.
I am unable to point to that particular line and replace it.

I have written a script , that prompts for the string to be replaced and if the input is bbbbb-bbbbb-bbbbb-bbbbb-bbbbb (without the quotation marks as in the target file), then the file should be updated as shown below.

ProductKey= "bbbbb-bbbbb-bbbbb-bbbbb-bbbbb"


Please help me to work this one out...

Cheers,

Dave.
6 REPLIES 6
Alexander Chuzhoy
Honored Contributor

Re: How to replace a string in a particular line using SED

Generally it's:
sed s/string to replace/replacement/ filename




where filename is the name ot the file for which you do the replacement.
If there's more than one occurence of such string you can use :
s/string to replace/replacement/g filename


this will replace all occurences of founded string.
Jan Sladky
Trusted Contributor

Re: How to replace a string in a particular line using SED

if you want script, simply like this:



sed 's/aaaa/bbbb/' file.txt > changed_file.txt


br Jan
GSM, Intelligent Networks, UNIX
Dave Faupel
New Member

Re: How to replace a string in a particular line using SED

Thanks guys - is there any way to get SED to read upto the first quotation mark and then replace the string in the line within the quotation marks? I'm putting together a script which will take a user input and then replace a product key in a unattend install file. Unfortunatly the string that I'm looking to replace will not always be the same, but I do know that it will always be located following 'ProductKey= "' and end at the second set of the quotation marks. i.e. the string to replace could be 'abcde-abcde-adshs-sdsds-dsdsd' or 'hfgwr-fddfd-dfdfd-yrryy-dgddg' but the string will always follow 'ProductKey= ' i.e

ProductKey= "kjkfd-dfdsf-sfrws-dfdfd-fdfdf"

The string will always be 29 characters in length and will be preceeded by a quotation mark and followed by a quotation mark.

Help appreciated....
Huc_1
Honored Contributor

Re: How to replace a string in a particular line using SED

Do not know about your setup / environment but this is how I use it from linux shell prompt.

ex -- if file g.test contains

-----------begin of file--------------
aa
bb
aaaaa-aaaaa-aaaaa-aaaaa-aaaaa
cc
dd
-----------end of file----------------

then type the following

shell_prompt > sed -e s/aaaaa-aaaaa-aaaaa-aaaaa-aaaaa/bbbbb-bbbbb-bbbbb-bbbbb-bbbbb/g g.test

this will display the g.test file and replace the aaa... by bbb..., mind you this does not change the content of the g.test file this interactive form of sed just display the change ! but this is good for testing..

The s/'orig_sting'/repl_string'/g placed in a sed command file will do the substitution for you the /g at the end stands for global (this is if you want to replace every occurence of the string"

Hope this get you codeding.

Jean-Pierre
Smile I will feel the difference
Alexander Chuzhoy
Honored Contributor

Re: How to replace a string in a particular line using SED

If you always have the string to replace under "ProductKey=",then:


sed "s/ProductKey=.*/ProductKey=aaabbbccdd/g" filename

will do the job...
Huc_1
Honored Contributor

Re: How to replace a string in a particular line using SED

I should have done a refresh on this note file before I submited my previous reply ...

But Alexander 2 reply seem right to me


Sorry about my slow typing.

Jean-Pierre
Smile I will feel the difference