printf not printing past '.' in string - unix

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.

Related

R customize error message when string contains unrecognized escape

I would like to give a more informative error message when users of my R functions supply a string with an unrecognized escape
my_string <- "sql\sql"
# Error: '\s' is an unrecognized escape in character string starting ""sql\s"
Something like this would be ideal.
my_string <- "sql\sql"
# Error: my_string contains an unrecognized escape. Try sql\\sql with double backslashes instead.
I have tried an if statement that looks for single backslashes
if (stringr::str_detect("sql\sql", "\")) stop("my error message")
but I get the same error.
Almost all of my users are Windows users running R 3.3 and up.
Code execution in R happens in two phases. First, R takes the raw string you enter and parses that into commands that can be run; then, R actually runs those commands. The parsing step makes sure what you've written actually makes sense as code. If it doesn't make any sense, then R can't even turn it into anything it can attempt to run.
The error message you are getting about the unrecognized escape sequence is happening at the parsing stage. That means R isn't really even attempting to execute the command, it just straight up can't understand what you are saying. There is no way to catch in error like this in code because there's no user code that's running at that point.
So if you are counting on your users writing code like my_string <- "something", then they need to write valid code. They can't change how strings are encoded or what the assignment operator looks like or how variables can be named. They also can't type !my_string! <=== %something% because R can't parse that either. R can't parse my_string <- "sql\sql" but it can parse my_string <- "sql\\sql" (slashes much be escaped in string literals). If they are not savy users, you might want to consider providing an alternative interface that can sanitize user input before trying to run it as code. Maybe make a shiny front end or have users pass arguments to your scripts via command line parameters.
If you're capturing your user input correctly, for a string input of\, R will store that in my_string as \\.
readline()
\
[1] "\\"
readline()
sql\sql
[1] "sql\\sql"
That means internally in R:
my_string <- "sql\\sql"
However
cat(my_string)
sql\sql
To check the input, you need to escape each escape, because you're looking for \\
stringr::str_detect(my_string, "\\\\")
Which returns TRUE if the input string is sql\sql. So the full line is:
if (stringr::str_detect("sql\\sql", "\\\\")) stop("my error message")

Error while Scilab module is loading

I downloaded module Metanet 0.6.2 and ran by Scilab
atomsInstall
After that i ran
`atomsLoad('metanet')`
but it shows
atomsLoad: An error occurred while loading 'metanet-0.6.2':
error(msprintf(gettext('%s module required."),'graph'));
^^
Error: Heterogeneous string detected, starting with ' and ending with ".
at line 335 of function atomsLoad ( D:\Program Files\scilab-6.0.1\modules\atoms\macros\atomsLoad.sci line 351 )
Why did it happen so?
It turns out that the metanet module is not supported by Scilab 6.0.1 yet. I had to install version 5.5.2.
Unfortunately, both the question and the accepted answer here on this page are very vague and misleading. Ideally, this kind of post should be blocked / down-voted, but I will try to answer it as much as I can.
Firstly when you want to run a Scilab command you do not put it in quotation marks, unless you want to use execstr command. However, the characters you have used are not quotations but backticks! I'm not sure why you have done that.
Secondly, the error:
Error: Heterogeneous string detected, starting with ' and ending with "
happens when a double quotation is used inside the single quotation or vice versa:
"This is a' string"
'this is a" string'
to solve the issue you should change the above strings to
"This is a'' string"
'this is a'" string'
basically adding one single quotation before any of the ' and " characters to turn them into literal ' and ".
bonus point if you want to pass a string to Tcl use curly brackets
TCL_EvalStr("set myVar {Hello World!}")
or
TCL_EvalStr("set myVar '"Hello World!'"")
but for PowerShell
powershell('$myVar= ''Hello World!''')
or
powershell("$myVar= ''Hello World!''")

Teradata remove enclosing single quotes from variable

I need to replace single quotes in a string of numbers and use in a WHERE IN clause. for example, I have
WHERE Group_ID IN (''4532','3422','1289'')
The criteria within parenthesis is being passed as a parameter, so I have no control over that. I tried using :
WHERE Group_ID IN (REGEXP_REPLACE(''4532','3422','1289'', '[']', ' ',1,0,i))
also tried using OReplace
WHERE Group_ID IN (OReplace(''4532','3422','1289'', '[']', ' '))
but get the same error:
[Teradata Database] [3707] Syntax error, expected something like ','
between a string or a Unicode character literal and the integer '4532'.
Please suggest how to remove the single enclosing quotes or even removing all single quotes should work as well.
The string ''4532','3422','1289'' you are using is incorrect because it contains non-escaped single quotes. This is a syntax error in SQL. In this particular form, no matter what function you use to fix it or which RDBMS you use, it will result in error with standard SQL.
Functions in the SQL cannot fix syntax errors. REGEXP_REPLACE and OReplace never get executed because the query never enters the execution state. It never goes past the SQL syntax parser.
To see the error from perspective of the SQL parser, you may break the string in to multiple parts
'' -- SQL Parser sees this as a starting and ending quote and hence an empty string
4532 -- Now comes what appears to SQL parser as an integer value
',' -- Now this is a pair of quotes containing a single comma
3422 -- Again an integer
',' -- Again a comma
1289 -- Again integer
'' -- Again emtpy string
This amalgam of strings and numbers will not mean anything to the SQL parser and will result in an error.
Fix
The fix is to properly escape the data. Single quotes must be escaped using another preceding single quote. So correct string in this scenario becomes '''4532'',''3422'',''1289'''
Another thing is that the OReplace usage (once syntax is fixed) is like OReplace(yourStringValueHere, '''', ' ')) Observe the usage of escaped single quote here. Two outer quotes are for the string start and end. First inner quote is the escape character and second inner quote is the actual data passed to the function.

Issue with multibyte character in Unix

We are facing issue for multibyte character when we are trying to run the below command:
awk 'length<30'
The File content is :
ASDFGHJKLQWERTYUIOPZXJM0000023 حكمت مزبان إبراهيم العزاوي
ASDFGHJKLQWERTYUIOPZXJM000
So it should give only one record.
length<30
Will always return true - you want the length() function, length is a simple variable which is initialized as zero.
awk 'length($0)<30'

Add a newline to a string in Scilab

Create a simple string in Scilab containing a newline.
Seems simple enough, but Scilab only seems to interpret escape sequences through printf style functions and msprintf / sprintf splits the string into a vector of strings at the newline!
The only way I can see to achieve this is to actually write a newline out to a file and read it back in again. Surely there is a simpler way to do this!
Ok, found it. The ascii function will do the job, a newline can be added via its ascii decimal -
str = 'hello' + ascii(10) + 'world'

Resources