Compose string manipulation operations in shell script - string

I'm trying to remove the directory prefix from $soy:
a=${soy#*$PREFIX}
then changing slashes per dots:
b=${a//\//.}
the goal is to convert a file-path to a module path inside a program.
Anyways, is there any way to do this i one expression using composition?
This doesn't work :(
${${soy#*$PREFIX}//\//.}

According to this blog comment at Linux Journal, you can't do multiple operations in one expression.

Related

JMeter functions and variables

I'm new to JMeter so this question may sound absolutely dumb...
I have a loop in which a variable (let's say it is called "raw") is being changed and written to file every iteration. The variable contains HTML encoded text so it has to be converted into plain text. I found out this can be done using __unescapeHtml function. When I tried using it worked but I ended up always receiving the same text as on the first iteration. Then I learned that I have to use vars.get instead of ${} to access a variable. So I changed ${__unescapeHtml("${raw}")} to ${__unescapeHtml(vars.get("raw")} which kind of helped: vars.get is getting the new value of raw each iteration but __unescapeHtml didn't work at all now - it just returns the encoded text from raw. I didn't succeded finding anything about this exact problem so I'm kind of stuck.
Ended up using
import org.apache.commons.lang3.StringEscapeUtils
...
StringEscapeUtils.unescapeHtml4(vars.get("raw"))
Don't know if it is a good way to do this but at least it works.
I assume, that you are using the expression ${...} inside a JSR-223 sampler or similar context. The user manual for JSR-223 Sampler states, that those scripts can be cached by JMeter. That is why you only get the values from the first time the context gets created.
The same is true for simple variable evaluations as ${varname}, as for function calls like ${__unescapeHtml(...)}.
The solution here is:
don't use ${...} inside of JSR-223 contexts, that might be cached.
you can however pass those expressions (${...}) into the context by using them as parameters through the input labeled Parameters on the JSR-223 Sampler – again assuming, that you are using it.
you can use the features, that your chosen JSR-223 context gives you, as you have done, by using the StringEscapeUtils#unescapeHtml4

How to put triple qoutes around existing string variable?

Let's say I have this variable:
st='MI'
and I want to convert it to:
st=''' 'MI' '''
to use it in a SQL command.
What's the best way to accomplish this?
Thanks in advance!
Tripleor single quoting are just ways of typing strings into source code files.
Once your program is running, your string is already a string, and there is no need to make any cnversion to use it as a parameter to a SQL driver function call.
What you may want i to have a string with an SQL statement that itself contains various (single or double) quote characters. If that is typed in your Python source code file, you can type the triple-quote straight. If you are getting these SQL statements from elsewhere, they are already strings, as I said above.
Now, there are a few instances in which you have a string in a running Python program, or a Python interactive session, that you would like printed, so that you can paste it directly in source code. For these cases you can try the "unicode_escape" codec (and recode it to text so that it does not double your backslashes:
In [56]: print("\n".encode("unicode_escape").decode("utf-8"))
\n

php expand string from file_get_contents

i have a file with below data
this is a text file with html format $testarray['G']
i use this script but it is not work properly.
<?php $testarray=array();
$testarray['G']=100;
$result222 =file_get_contents(realpath('file.php'));
printf($result222);?>
i want this script, print out below line
this is a text file with html format 100
i do't want to use include or require function because user can insert php code in this file and i do't want user can use php codes in this file
can anybody help me?
many thanks
If you know the string you expect to perform a substitution on, you can always do it manually with str_replace(). However, it looks like you want to be able to substitute arbitrary variables into the string, which I implore you to not do. You are giving this PHP script way too much power as is. I can think of a few exploits off the top of my head, such as injecting superglobals among simply brute forcing variable names.
Edit: I realize now that I also didn't give you the straightforward answer which does as you request: the eval() function. You should be able to craft a string that assigns the contents of the file to a variable, which should perform variable substitutions by the PHP parser. Please don't use it, but if you do be very careful.
i used below script
$result222 =file_get_contents(realpath('file.php'));
$result222=str_replace(array("<?","?>")," ",$result222);
print eval("return<<<ENDEVAL\n$result222\nENDEVAL;\n");

How to get (translatable) strings from specific domain with POEdit

I have been trying for hours finding a way to setup POEdit so that it can grab the text from specific domain only
My gettext function looks like this:
function ri($id, $parameters = array(), $domain = 'default', $locale = null)
A sample call:
echo ri('Text %xyz%', array('%xyz%'=>100), 'myDomain');
I will need to grab only the text with the domain myDomain to translate, or at least I want POEdit to put these texts into domain specific files. Is there a way to do it?
I found several questions that are similar but the answers don't really tell me what to do (I think I'm such a noob it must be explained in plain English for me to understand):
How to set gettext text domain in Poedit?
How to get list of translatable messages
So I finally figured it out after days of searching, I finally found the answer here:
http://sourceforge.net/mailarchive/message.php?msg_id=27691818
xgettext recognizes context in strings, and gives a msgctxt field in the *.pot file, which is recognized by translation software as a
context and is shown as such (check image of Pootle showing context
below)
This can be done in 3 ways:
String in code should be in the format _t('context','string'); and xgettext invocation should be in the form --keyword=_t:1c,2
(this basically explains to xgettext that there are 2 arguments in
the keyword function, 1st one is context, 2nd one is string)
String in code in the format _t('string','context'); and xgettext invocation should be in the form --keyword=_t:1,2c
String in the code should be as _t('context|string') and xgettext invocation should be in the form --keyword=_t:1g
So to answer my own question, I added this to the "sources keywords" tab of Poedit:
ri:1,3c
ri is the function name, 1 is the location of the stringid, 3 is the location of the context/domain
Hope this helps someone else, I hate all these cryptic documents
(This is a repost of my answer to the same thing here.)
Neither GNU gettext tools nor Poedit (which uses them) support this particular misuse of gettext.
In gettext, domain is roughly “a piece of software” — a program, a library, a plugin, a theme. As such, it typically resides in a single directory tree and is alone there — or at the very least, if you have multiple pieces=domains, you have them organized sanely into some subdirectories that you can limit the extraction to.
Mixing and matching domains within a single file as you do is not how gettext was intended to be used, and there’s no reasonable solution to handle it other than using your own helper function, e.g. by wrapping all myDomain texts into __mydomain (which you must define, obviously) and adding that to the list of keywords in Poedit when extracting for myDomain and not adding that to the list of keywords for other domains' files.

How can I copy picture and text from one file into another file dynamically using node.js

Suppose i have two files a_b_c_d.txt and e_f_g_h.png S,At runtime i.e., by using command prompt i have to create b folder inside that c folder inside that d folder inside that a.txt and same also for another file f->g->h->e.png and i have some text in a and image in epng . .So,how can I get values from those existing file into created files. .
You can find all the file system operations inside the fs module. http://nodejs.org/api/fs.html
But like tapan says if you need to do complex synchronous execution that manipulates the file system something like Bash will be a lot better suited for that.
So if I'm understanding you correctly you want to take a file named "a_b_c_d.txt" in some folder, and move that into a nested folder as:
./a_b_c_d.txt -> ./b/c/d/a.txt
The general solution would be:
Grab the file name using process.argv if it varies.
For example, if you supply the file as an argument to node, e.g.
node move.js "a_b_c_d.txt", the argument, "a_b_c_d.txt", will be in the argv array.
Process the file name using a combination of string and array methods.
Nodes current directory is stored in the __dirname global variable
if you need it.
You can split the extension from the rest of the path
using string's split(...) method.
For the above argument, split('.') will result in the array ['a_b_c_d', 'txt']
You can then split 'a_b_c_d' using '_',
and use various array operations to pull the file name 'a'
out of the array, so that you're left with the path ['b', 'c', 'd']
and the file name and extension sitting in their own variables somewhere.
Use fs.mkdirSync(...) on the path array to make each nested folder,
starting with b (e.g. using array's forEach(...) method).
You could also use the async fs.mkdir(...) and supply callbacks,
but the sync version is easier in this case.
Finally use fs.renameSync(...) to move ./a_b_c_d.txt to ./b/c/d/a.txt.
As you can see, python or bash (as tapan suggested) would probably be simpler for this use case, but if for some reason you have to use node, the above advice will hopefully be enough to get you started.

Resources