0

I need to replace DatabaseConnection.GetConnection() with DatabaseConnection.GetConnection(_connectionString)

Using grep (GNU grep) 3.1

grep GetConnection() CustomerRepository.cs

yields

bash: syntax error near unexpected token `('

however

grep GetConnection\(\) CustomerRepository.cs`

outputs

using (IDbConnection connection = DatabaseConnection.GetConnection())

as expected

Using sed (GNU sed) 4.8

sed -e 's/GetConnection()/GetConnection(_connectionString)/' CustomerRepository.cs

outputs the entire contents of the input file, as it does when I escape the search parentheses.

Totally lost, what do I need to do to have this work?

6
  • Well sed and grep have different purposes - what is it that you want to do? Aug 30, 2020 at 17:59
  • Replace DatabaseConnection.GetConnection() with DatabaseConnection.GetConnection(_connectionString) Aug 30, 2020 at 18:02
  • Add _connectionString parameter between the parentheses. Aug 30, 2020 at 18:03
  • Well your sed command should do that: there's no need to escape the parentheses - unlike in your grep command, they're protected from the shell by the single quotes, and sed's default regular expression dialect is BRE in which parentheses are not special (you would need to escape the ones on the LHS if you'd used sed's -E or -r options to change the dialect to ERE) Aug 30, 2020 at 18:21
  • sed -e 's/GetConnection\(\)/GetConnection(_connectionString)/' CustomerRepository.cs displays the entire contents of the input file, as do sed -E.. and sed -r.... Aug 30, 2020 at 18:35

1 Answer 1

0

"grep GetConnection() CustomerRepository.cs"

is right to complain, as "(" is specially interpreted by the shell and doesn't make sense the way you use it.

Escaping "(" as "(" and ")" as ")" is a good thing to do, but putting the whole argument into quotes would be even better:

grep 'GetConnection()' CustomerRepository.cs

As for your second try,

sed -e 's/GetConnection()/GetConnection(_connectionString)/' CustomerRepository.cs

sed replaces each line according to your command, and puts out every line not regarding if your substitution toke place or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .