Perl script replace syntax - perl

Below is my Perl script
str_replace(rtrim(c_manager),'''','_'),'."\n".
throws error
str_replace(rtrim(c_manager), '''"
Bad name after _' at pl_recert_output.pl line 262.
Please help to solve this issue:
$sql = 'select rtrim(f_admin_disabled),'."\n".
' convert(varchar,t_password,101),'."\n".
' rtrim(c_email),'."\n".
' str_replace(rtrim(c_manager),'''','_'),'."\n".
' rtrim(c_mgr_email)'."\n".
' from tuserprofile'."\n".
' where ic_user1 = '."'$user_id'"."\n";

You have single quotes within your single quoted string.
Single quotes within a quote string must be escaped with \'.
However, you would be better off using the multi-line quoting syntax for cleaner code:
$sql = <<EOF;
select rtrim(f_admin_disabled),
convert(varchar,t_password,101),
rtrim(c_email),
str_replace(rtrim(c_manager),'''','_'),
rtrim(c_mgr_email)
from tuserprofile
where ic_user1 = '$user_id'
EOF
;
This has the intended result without messy escaping and string concatenation.

You need to escape the single quotes in your variable assignment:
my $user_id = 'test';
my $sql = 'select rtrim(f_admin_disabled),'."\n".
' convert(varchar,t_password,101),'."\n".
' rtrim(c_email),'."\n".
' str_replace(rtrim(c_manager),\'\'\'\',\'_\'),'."\n".
' rtrim(c_mgr_email)'."\n".
' from tuserprofile'."\n".
' where ic_user1 = '."'$user_id'"."\n";
print $sql;
results in :
select rtrim(f_admin_disabled),
convert(varchar,t_password,101),
rtrim(c_email),
str_replace(rtrim(c_manager),'''','_'),
rtrim(c_mgr_email)
from tuserprofile
where ic_user1 = 'test'
Check if that is the result you expected.

Related

How to add a single quote before each comma

a have a column as below
mystring
AC1853551,AC1854125,AC1855220,AC188115,AC1884120,AC1884390,AC1885102
I need to transformm it to get this output
mystring
('AC1853551','AC1854125','AC1855220','AC188115','AC1884120','AC1884390','AC1885102')
Here is my query that i tried
select CONCAT('( , CONCAT (mystring, ')')) from mytablename
I'm getting an error when it comes to insert a single quote '
Then i thought about replacing the comma with a ','
How to get desired output
i'm using postgres 10
A literal quote is coded as a doubled quote:
select '(''' || replace(mycolumn, ',', ''',''') || ''')'
from mytable
See live demo.

DB2 TRANSLATE for Question Mark in Data

I have a statement on DB2 using the TRANSLATE function:
,TRANSLATE( FIELD1, ' ', x'042021222324282A3638FF') FIELD1A
How to I modify the statement for a question mark character in data
Thank you.
ihope that i understood youright. You want to replace a '?' by ' ' is that true? in this case translate works like this:
VALUES translate('sdnfojs?sdmf', ' ', x'6F'),
translate('sdnfojs?sdmf', ' ', '?');
--result: 'sdnfojs sdmf'

Redshift: how to remove all newline characters in a field

I am wondering how I can remove all newline characters in Redshift from a field. I tried something like this:
replace(replace(body, '\n', ' '), '\r', ' ')
and
regexp_replace(body, '[\n\r]+', ' ')
But it didn't work. Please share if you know how to do this.
Use chr(10) instead of \n
example:
select replace(CONCAT('Text 1' , chr(10), 'Text 2'), chr(10), '-') as txt
This should help
regexp_replace(column, '\r|\n', '')
To remove line breaks:
SELECT REPLACE('This line has
a line break', CHR(10), '');
This gives output: This line hasa line break. You can see more ASCII or CHR() codes here: https://www.petefreitag.com/cheatsheets/ascii-codes/
To remove special characters like \r, \n, \t
Assuming col1 has text like This line has\r\n special characters.
Using replace()
SELECT REPLACE(REPLACE(col1, '\\r', ''), '\\n', '');
We need to escape \ because backslash is special character in SQL (used to escape double quotes, etc...)
Using regexp_replace()
SELECT REGEXP_REPLACE(col1, '(\\\\r|\\\\n)', '');
We need to escape \ because it is a special character in SQL and we need to escape the resulting backslashes again because backslash is a special character in regex as well.
Both replace() and regexp_replace() gives output: This line has special characters.

How to ignore escape character ?

this is my string
$mystring = "INSERT INTO `glpi_networkports` (`entities_id`,`is_recursive`,`items_id`,`itemtype`,`comment`,`logical_number`,`name`,`networkinterfaces_id`,`ip`,`mac`,`netmask`,`gateway`,`subnet`,`netpoints_id`)"
How can i ignore escape ?
Because it gave me string like this :
INSERT INTO glpi_networkports (entities_id,is_recursive,items_id,itemtype,comment,logical_number,
ame,
etworkinterfaces_id,ip,mac,
etmask,gateway,subnet,
etpoints_id)
Thanks
Currently your select statement is changed because you used a double quoted string and `n is expanded to a new line, turning the select to an invalid statement.
Use single quotes to maintain the backticks and disable string expansion.
use quote or double the escape char:
$mystring = "INSERT INTO 'glpi_networkports' ..."
$mystring = "INSERT INTO ``glpi_networkports`` ...

Problem with mysqli_real_escape_string (and magic quotes is off)

I am having a problem with mysqli_real_escape_string and single quotes.
Anytime an insert takes a value that includes a ' mysqli_real_escape_string replaces it with \' causing the insert to fail.
I do not have magic quotes enabled, which seems to be a common cause of this problem, but still find I get \' and not \' or even \\'
Can anyone tell me what might have gone wrong here?
taking this to an answer since comments are a bit limiting.
So you're generating the query like
$a = mysqli_real_escape_string(... something ...);
$b = mysqli_real_escape_string(... something else ...);
$sql = "INSERT ... VALUES ('$a', '$b')";
?
ok. so let's say this:
$a = "'"; // a is now a single quote: '
$escaped_a = mysql_real_escape_string($a); // should be \' now
$sql1 = "INSERT ... VALUES ('$a' ..."
$sql2 = "INSERT ... VALUES ('$escaped_a' ..."
you should end up with
INSERT ... vALUES (''' ...
INSERT ... VALUES ('\'' ...
The first one is "bad". because of the extra quote. The second is valid.