I have a bunch of web.config files each possibly containing a connection string to a database like this:-
<add name="MyConnectionString" connectionString="Data Source=myserver;Initial Catalog=mydb;Persist Security Info=False;User ID=myuser;Password=secret" providerName="System.Data.SqlClient" />
What I want to do is grep the User ID=myuser;Password=secret part out of the line if it exists.
I cannot assume that password is the last property in the connection string, there may be other properties following it. So basically the password will always end with " or ;.
Using cygwin I can search the files like this:-
find /cygdrive/c/inetpub/wwwroot/ -iname 'web.config' -printf '%p\0' | xargs -0 grep -i "password="
How would i improve the grep part to retrieve only the username and password instead of the entire line ??
I could then pipe it into sort -u to get an unique list of usernames and passwords in use.
You can use
grepwith the-Pflag to use Perl’s regular-expression engine, and the-oflag to return only the matching parts of the input:This will return the following, considering your sample input:
Give your existing command, it would be something like: