I am reading a file and interested in knowing the operating system that generated the file. It can be either Windows or Unix. I am trying to determine the operating system by reading end of line character. I am facing difficulty in getting the character at end of line. I tried the following code but it always returns -2 irrespective of the operating system (which is correct as per the documentation). Thanks for your help.
def var cLog as char init "myfile.log".
input from value (cLog).
seek input to end.
readkey.
message lastkey.
This assumes there really is a end of line in the file so perhaps not a perfect solution (on the other hand - if there isn't - how can you tell).
I skip the SEEK part. Instead i just check for the pattern in the file.
Writing CHR(13) + CHR(10) is similar to what could be written as \r\n. This can also be escaped ~r~n in Progress ABL (see below).
DEFINE VARIABLE cFile AS LONGCHAR NO-UNDO.
COPY-LOB FROM FILE "c:\temp\file.txt" TO cFile.
IF cFile MATCHES "*" + CHR(13) + CHR(10) + "*" THEN DO:
MESSAGE "Likely a Windows-file" VIEW-AS ALERT-BOX.
END.
ELSE IF cFile MATCHES "*" + CHR(13) + "*" THEN DO:
MESSAGE "Likely a Mac file" VIEW-AS ALERT-BOX.
END.
ELSE IF cFile MATCHES "*" + CHR(10) + "*" THEN DO:
MESSAGE "Likely a Linux-file" VIEW-AS ALERT-BOX.
END.
ELSE DO:
MESSAGE "I cannot tell what file this is" VIEW-AS ALERT-BOX.
END.
With escaped characters
DEFINE VARIABLE cFile AS LONGCHAR NO-UNDO.
COPY-LOB FROM FILE "c:\temp\file.txt" TO cFile.
IF cFile MATCHES "*~r~n*" THEN DO:
MESSAGE "Likely a Windows-file" VIEW-AS ALERT-BOX.
END.
ELSE IF cFile MATCHES "*~r*" THEN DO:
MESSAGE "Likely a Mac file" VIEW-AS ALERT-BOX.
END.
ELSE IF cFile MATCHES "*~n*" THEN DO:
MESSAGE "Likely a Linux-file" VIEW-AS ALERT-BOX.
END.
ELSE DO:
MESSAGE "I cannot tell what file this is" VIEW-AS ALERT-BOX.
END.
Related
I am trying to just check that I got back something in a string using Should Contain
I can't figure out why there is no " " in '\r \r'
Any ideas what I am missing?
>>> x='\r \r'
>>> " " in x
True
If you use " " as an argument in Robot, it will be three chars : quotes, space, quote. If you want to use only space, then you should use ${SPACE} variable :
Should Contain ${result} ${SPACE}
See "handling spaces" section of the documentation.
I am trying to build a linux sendemail command in R using paste from a bunch of values from a contact form. The message portion will have single and/or double quotes in it. Based on this post (How to escape double quote inside a double quote?) I have determined that I need to finish already opened one ('), placing escaped one (\'), then opening another one (').
For example, this works when run directly from linux command line (when using actual email addresses):
sendemail -f mary#test.com -t bob#test.com -u 'Subject here' -m 'My message'\''s text has single quote or maybe "double quotes".' -s smtp.gmail.com:587 -o tls=yes -xu from#mail.com -xp xxxxxx
The message portion is coming from a contact form and I want to email that message to our team. I've tried using gsub to replace a single quote with '\'' so sendemail will send the message successfully but I have not found the correct gsub syntax. I've tried various gyrations of this type of thing:
gsub("'", "'\''", message)
gsub("\'", "'\\''", message)
and so on... I've tried using fixed, perl, etc. I cannot seem to get the needed syntax.
I get either no backslashes or 2 backslashes and sendemail wants only one backslash.
Certainly, I can just remove the single and double quotes from the message and it will work. I would prefer to preserve the message "as is" if possible, though.
Is this even possible with gsub and paste?
Here is a code snippet of what I'm doing:
message = gsub("\'", "\\\'", input$mailAndStoreModalText)
message = gsub("\"", "\\\"", message)
print( message)
If my input is - This's the "best" music - the gsubs above just result in the same exact thing with 0 backslashes. Then this fails:
team.email = paste0("sendemail -f ", user.email,
" -t ", distr.list,
" -u 'Contact Form Submitted' -m",
" 'Priority: ", priority," ", message,
"' -s smtp.gmail.com:587 -o tls=yes -xu sender#test.com -xp xxxxx")
t <- try(system(team.email, intern = TRUE))
If you chain the gsubs, you should pass message variable the second time. However, you may use it like this:
message <- gsub("\"", "\\\"", gsub("\'", "\\\'", input$mailAndStoreModalText, fixed=TRUE), fixed=TRUE)
Or a regex based replacement:
message <- gsub("([\"'])", "\\\\\\1", input$mailAndStoreModalText)
Both will output This\'s the \"best\" music as output.
See the R demo online. Note that cat(message, "\n") command shows you the literal string that message holds, not the string literal that you get when trying to just print message.
Also, the ([\"']) regex matches and captures into Group 1 either a " or ' and the "\\\\\\1" replacement pattern replaces the whole match with \ (that is defined with 4 backslashes) and then the value inside Group 1 (\\1).
I use Run Keyword Unless comparing a variable and a string:
Run Keyword Unless '${text}' == 'HelloWorld' My Keyword ${text}
Sometimes ${text} consists of two lines separated by "\n" (eg. "One line\ntwo lines"). If so, the tests fails with an error:
Evaluating expression ''One line
two lines' == 'HelloWorld'' failed: SyntaxError: EOL while scanning string literal (<string>, line 1)
I solved the problem removing '\n' with String.Replace String as follows:
${one_line_text}= String.Replace String ${text} \n ${SPACE}
Run Keyword Unless '${one_line_text}' == 'HelloWorld' My Keyword ${text}
Is there a way to do it without explicit removing of EOL in a separate keyword?
What about ${text.replace("\n", " ")}?
You can use python's string literals - """ or ''' - and not change the string at all:
Run Keyword Unless '''${text}''' == 'HelloWorld' My Keyword ${text}
They are designed for pretty much this purpose - to hold values having newline characters, plus quotes.
I am having a problem using printf on my unix system. It is throwing an error every time I try to print the following
printf "%-15s %-15.2s" "Total Acounts:\nChecks=$" checks
checks should be a decimal, but I have tried printing it as a float and a decimal and get the same error.
fatal: not enough arguments to satisfy format string
`%-15s %-15.2sTotal Acounts:
Checks=$2135.92'
^
I have been working at this for a while now and can't figure it out, so any help is appreciated.
That's not how you call printf in awk. You are missing the commas indicating arguments.
You've given printf only a format string (the concatenation of "%-15s %-15.2s", "Total Acounts:\nChecks=$" and the value of checks).
As you can see from the error message that shows the entire string as the format string and includes the value of checks in the string.
You probably meant:
printf "%-15s %-15.2s", "Total Acounts:\nChecks=$", checks
#---------------------^---------------------------^
though %-15s there isn't really doing anything useful for you as `"Total Acounts:\nChecks=$" is longer than 15 characters.
I'm using the python module PLY to write a parser, and I am implementing as I go. I have a simple rule to detect strings:
r'("|\').*("|\')'
When lexer errors are thrown I have this:
def t_error (t) :
print 'Illegal lexer input line ' + str(t.lineno) + ' ' + t.value[:16]
sys.exit(-1)
When I feed my parser the following input:
parse("preg_match('%^[\*\%]+$%', $keywords)")
I get back this in return:
Illegal lexer input line 1 %^[\*\%]+$%', $k
My questions are:
1) Why am I not parsing this string? It seems like my regex should properly handle this string.
2) How can I fix this?
edit:
I have narrowed the problem down a bit. The following strings throw illegal lexer input errors by themselves:
'%'
'^'
Even if this regex were working it isn't quite doing what you want it to, for example it would accept "this', which isn't really a string. This is also the cause of the "illegal lexer input"...
After having done it's job and found the first string in "preg_match(' the lexer is then upset when each of the next 11 characters %^[\*\%]+$% are illegal (and not in t_ignore), since they don't even start with " or '.
.
Try doing this with two cases for " and ': "Starts with quote, some things which aren't quote, ends with quote." That is:
r'("[^"]*")|(\'[^\']*\')'
Or, if you want to include escaped speech marks:
r'("(\\"|[^"])*")|(\'(\\\'|[^\'])*\')'