Problems using gsub

Hi everyone,

with Python I usually handle this SQL query:

/* Query for existing (?) user / person. */

SELECT *
FROM person
WHERE LOWER(unix_account) = LOWER('{reciever}')
OR LOWER(given_name) = LOWER('{reciever}')
OR LOWER(surname) = LOWER('{reciever}')

I would like to replace new line characters ("\n") from the string that I get by:

readr::read_file(file.path)

Also I would like to replace "{reciever}" with a name like "Bob".

Using gsub I get either this error:

'Invalid contents of {}'

Or, if I use

gsub(…, perl = TRUE)

I end up getting a string containing only one whitespace (I wanted to replace \n with a whitespace).

Any ideas what I am doing wrong?

Thanks in advance
Markus

Does this get you what you want?

Q <- "SELECT *
  FROM person
WHERE LOWER(unix_account) = LOWER('{reciever}')
OR LOWER(given_name) = LOWER('{reciever}')
OR LOWER(surname) = LOWER('{reciever}')"
Q
#> [1] "SELECT *\n  FROM person\nWHERE LOWER(unix_account) = LOWER('{reciever}')\nOR LOWER(given_name) = LOWER('{reciever}')\nOR LOWER(surname) = LOWER('{reciever}')"
Q <- gsub("\\n",replacement = " ",Q)
Q <- gsub("\\{reciever}","Bob",Q)
Q
#> [1] "SELECT *   FROM person WHERE LOWER(unix_account) = LOWER('Bob') OR LOWER(given_name) = LOWER('Bob') OR LOWER(surname) = LOWER('Bob')"

Created on 2022-10-22 with reprex v2.0.2

Yes it does, thank you! I feel stupid. I just mixed up the order of the arguments in gsub...

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.