tags:

views:

30

answers:

2

Hello, guys! I have a connection string in a ini file as following:

1 ...
2 ...
3 #someline:
n ConnectionString = "Data Source=localhost;Database=grid;User ID=grid;Password=grid;"

And I would like to locate to the line n and replcate Data Source, Database, User Id and Password field. So how can I make it in sed?

+1  A: 
sed -e 's/localhost/DataSource/' -e 's/grid/Database/' -e 's/grid/UserId/' -e 's/grid/Password'

This assumes that the fields are always in this order, and that the string 'grid' does not appear in either the text or any previously substitued fields.

Phil Wallach
The first "grid" should be "localhost".
Dennis Williamson
Yep indeed. Edited. Thanks.
Phil Wallach
A: 

you can use awk

$ awk -vds="Datasource" -vdb="dbname" -vuser="userid" -vpswd="pass" '/Connection/{$0="Data Source="ds";Database="db";User ID="userid";Password="pswd}1' file
ghostdog74
It seems that awk will remove all the other lines in my file.
xiao
No, the code will not remove all other lines. have you tried running it ?
ghostdog74