Trim whitespaces from esql string - ibm-integration-bus

I want to Trim the white-spaces from a string which I am getting from XML file using esql.
I am using trim command but it doesn't seems to work while trimming spaces, whereas if you want to trim something else the Trim() function seems to be working fine .
example
Trim(' ' From ' Nitin ');
Result
Nitin
Trim('i' From 'Nitin');
Result
Ntn

DECLARE whiteSpace CONSTANT CHARACTER CAST( X'090D0A20' AS CHAR CCSID 1208);
-- tab, cr, lf, space
DECLARE input2 CHARACTER 'smith';
SET input2 = whiteSpace || input2 || whiteSpace;
SET OutputRoot.XMLNSC.Top.Out2 = TRIM( whiteSpace FROM input2);
output:
<Top><Out2>smith</Out2></Top>

Related

why does a comma "," get counted in [.] type expression in antlr lexer

I am making a grammar for bash scripts. I am facing a problem while tokenising the "," symbol. The following grammar tokenises it as <BLOB> while I expect it to be tokenised as <OTHER>.
grammar newgram;
code : KEY (BLOB)+ (EOF | '\n')+;
KEY : 'wget';
BLOB : [a-zA-Z0-9#!$^%*&+-.]+?;
OTHER : .;
However, if I make BLOB to be [a-zA-Z0-9#!$^%*&+.-]+?;, then it is tokenised as <OTHER>.
I cannot understand why is it happening like this.
In the former case, the characters : and / are also tokenised as <OTHER>, so I do not see a reason for ,, to be marked <BLOB>.
Input I am tokenising, wget -o --quiet https,://www.google.com
The output I am receiving with the mentioned grammar,
[#0,0:3='wget',<'wget'>,1:0]
[#1,4:4=' ',<OTHER>,1:4]
[#2,5:5='-',<BLOB>,1:5]
[#3,6:6='o',<BLOB>,1:6]
[#4,7:7=' ',<OTHER>,1:7]
[#5,8:8='-',<BLOB>,1:8]
[#6,9:9='-',<BLOB>,1:9]
[#7,10:10='q',<BLOB>,1:10]
[#8,11:11='u',<BLOB>,1:11]
[#9,12:12='i',<BLOB>,1:12]
[#10,13:13='e',<BLOB>,1:13]
[#11,14:14='t',<BLOB>,1:14]
[#12,15:15=' ',<OTHER>,1:15]
[#13,16:16='h',<BLOB>,1:16]
[#14,17:17='t',<BLOB>,1:17]
[#15,18:18='t',<BLOB>,1:18]
[#16,19:19='p',<BLOB>,1:19]
[#17,20:20='s',<BLOB>,1:20]
[#18,21:21=',',<BLOB>,1:21]
[#19,22:22=':',<OTHER>,1:22]
[#20,23:23='/',<OTHER>,1:23]
[#21,24:24='/',<OTHER>,1:24]
[#22,25:25='w',<BLOB>,1:25]
[#23,26:26='w',<BLOB>,1:26]
[#24,27:27='w',<BLOB>,1:27]
[#25,28:28='.',<BLOB>,1:28]
[#26,29:29='g',<BLOB>,1:29]
[#27,30:30='o',<BLOB>,1:30]
[#28,31:31='o',<BLOB>,1:31]
[#29,32:32='g',<BLOB>,1:32]
[#30,33:33='l',<BLOB>,1:33]
[#31,34:34='e',<BLOB>,1:34]
[#32,35:35='.',<BLOB>,1:35]
[#33,36:36='c',<BLOB>,1:36]
[#34,37:37='o',<BLOB>,1:37]
[#35,38:38='m',<BLOB>,1:38]
[#36,39:39='\n',<'
'>,1:39]
[#37,40:39='<EOF>',<EOF>,2:0]
line 1:4 extraneous input ' ' expecting BLOB
line 1:7 extraneous input ' ' expecting {<EOF>, '
', BLOB}
line 1:15 extraneous input ' ' expecting {<EOF>, '
', BLOB}
line 1:22 extraneous input ':' expecting {<EOF>, '
', BLOB}
As already mentioned in a comment, the - in +-. inside your character class is interpreted as a range operator. And the , is inside that range. Escape it like this: [a-zA-Z0-9#!$^%*&+\-.]+?
Also, a trailing [ ... ]+? at the end of a lexer rule will always match a single character. So [a-zA-Z0-9#!$^%*&+\-.]+? can just as well be written as [a-zA-Z0-9#!$^%*&+\-.]

Replacing and substituting characters of the the string in Ruby

What is the best way to replace all characters in the string?
numbers to '.'
'.' to ';'
'-' to '_'
'_' to '-'
I used temporary characters to do it. But it is messed up when the temp character itself appeared in the first string.
I also tried tr method, and it doesn't work for dash and underline.
You have to escape the dash:
print "abc123.-_def.456".tr('-_.0-9', '_\-;.')
// here ___^
Output:
abc...;_-def;...

How to replace \r in a string in ruby

I have a string that looks like this.
mystring="The Body of a\r\n\t\t\t\tSpider"
I want to replace all the \r, \n, \t etc with a whitespace.
The code I wrote for this is :
mystring.gsub(/\\./, " ")
But this isn't doing anything to the string.
Help.
\r, \n and \t are escape sequences representing carriage return, line feed and tab. Although they are written as two characters, they are interpreted as a single character:
"\r\n\t".codepoints #=> [13, 10, 9]
Because it is such a common requirement, there's a shortcut \s to match all whitespace characters:
mystring.gsub(/\s/, ' ')
#=> "The Body of a Spider"
Or \s+ to match multiple whitespace characters:
mystring.gsub(/\s+/, ' ')
#=> "The Body of a Spider"
/\s/ is equivalent to /[ \t\r\n\f]/
String#tr is designed for stream symbol substitution. It appears to be a bit quickier, than String#gsub:
mystring.tr "\r", ' '
It hasan insplace version also (this will replace all carriage returns, line feed and spaces with space):
mystring.tr! "\s\r\n\t\f", ' '
Stefen's Answer is really very Cool as always comeup with very short and clean solutions. But here what I tried to remove all special characters. [Posted as just optional solution] ;)
> a = "The Body of a\r\n\t\t\t\tSpider"
=> "The Body of a\r\n\t\t\t\tSpider"
> a.gsub(/[^0-9A-Za-z]/, ' ')
=> "The Body of a Spider"
you can use strip , then add a space to your string
mystring.strip . " "
If you literally has \r\n\t in your string:
mystring="The Body of a\r\n\t\t\t\tSpider"
mystring.split(/[\r\t\n]/)

Bash - replace string inside all files in directory

I have 31 .ctl files in a directory, they looks like this:
load data CHARACTERSET AL32UTF8
infile '../dane/kontakty_Biura_wyborcze.csv' "str '\n'"
append
into table ODI_PUW_OSOBY2
fields terminated by ';'
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
( LP CHAR(4000),
WOJEWODZTWO CHAR(4000),
POWIAT CHAR(4000),
GMINA CHAR(4000),
NAZWA_INSTYTUCJI CHAR(4000),
KOD CHAR(4000),
MIEJSCOWOSC CHAR(4000),
ADRES CHAR(4000),
NAZWISKO_I_IMIE CHAR(4000),
FUNKCJA CHAR(4000),
TEL_SLUZB_STACJON_1 CHAR(4000),
TEL_SLUZB_STACJON_2 CHAR(4000),
TEL_SLUZB_STACJON_3 CHAR(4000),
TEL_SLUZB_KOM_1 CHAR(4000),
TEL_SLUZB_KOM_2 CHAR(4000),
FAX_SLUZB_1 CHAR(4000),
FAX_SLUZB_2 CHAR(4000),
EMAIL_SLUZB_1 CHAR(4000),
EMAIL_SLUZB_2 CHAR(4000),
WWW CHAR(4000),
TYP CONSTANT "Biura wyborcze.",
ODI_SESJA_ID CONSTANT "20130717144702"
ODI_STATUS CONSTANT "0",
IMIE EXPRESSION "pg_odi_utils.zwroc_imiona(pg_odi_utils.usun_przyrostki(:NAZWISKO_I_IMIE),0)",
NAZWISKO EXPRESSION "pg_odi_utils.zwroc_nazwisko(pg_odi_utils.usun_przyrostki(:NAZWISKO_I_IMIE),0)"
)
There are 31 files like this. I need to replace value in this line:
ODI_SESJA_ID CONSTANT '20130717144702'
to new timestamp, the same for all files. Current timestamp is not known (I mean value that exists in file currently, in this case '20130717144702').
So I need to (for each file found in directory):
find line starting from ODI_SESJA_ID
replace value after 'ODI_SESJA_ID CONSTANT ' with new one
the rest lines in file should stay untouched
What is the best way to do this using bash? Should I use sed or similar tools? How?
Something like:
sed 's/\(^[ \t]\+ODI_SESJA_ID\ CONSTANT\).*/\1 \"newtimestamp\"/' tmp
should work.
Group the string that will be retained, adding the placeholder (\1) in the replacement string. Replace newtimestamp with whatever value you prefer, of course.
I would do this using sed like so:
sed -i "/^[ \t]*ODI_SESJA_ID CONSTANT/s/'[^']\+'/'REPLACEMENT'/" *.ctl
The -i flag to sed means it modifies the files in place, so I usually try it on a single file first with the -e flag instead of the -i flag and confirm that sed's output is what I was looking for.
Explanation:
The double-quotes protect my regex from the shell.
/^[ \t]*ODI_SESJA_ID CONSTANT/ matches only the lines that start with whitespace followed by 'ODI_SESJA_ID CONSTANT'.
s/'[^']\+'/'REPLACEMENT'/ substitutes 'REPLACEMENT' (quoted) for the first quoted portion of the text on matching lines.
The document at http://www.catonmat.net/blog/wp-content/uploads/2008/09/sed1line.txt (top Google hit for 'sed one liners' is pretty helpful for quickly dispatching these sort of tasks.
I found some simplest solution, it seems to be good:
sed -i 's/.*ODI_SESJA_ID.*/ ODI_SESJA_ID CONSTANT "'$(date +%s)'",/' *.ctl
It replaces lines that contains ODI_SESJA_ID to new value. Not very elegant, because it replaces entire line, instead of only value that need to be processed.

Salesforce Apex String replacement funny business

Is anyone aware of how to differentiate between the characters '\"' and '"'?
I am trying to pre-process a string and this statement confuses me.
system.assert(' "b" ' == ' "\"" '.replace('\"','b'); //FAILS, returns ' bbb '
In your example, Salesforce is essentially ignoring the backslash as illustrated here:
system.assert('"' == '\"'); // yup
system.assertEquals(1, '\"'.length()); // just one character
system.assertEquals(1, '"'.length()); // just one character--the same one
If your original string has a real backslash character in it, it's the backslash that you need to escape with another backslash like this:
system.assertEquals(1, '\\'.length()); // just one character: \
system.assertEquals(2, '\\"'.length()); // two characters: \"
system.assert(' "b" ' == ' "\\"" '.replace('\\"','b'));
Can you please try this one (replace('\"' instead of replace('\"'):
system.assert(' "b" ' == ' "\"" '.replace('\\"','b');

Resources