substitution

Default substituting %s in python scripts

我怕爱的太早我们不能终老 提交于 2019-11-30 11:25:58
Sometimes in Python scripts I see lines like: cmd = "%s/%s_tb -cm cond+line+fsm -ucli -do \"%s\"" Where is the %s in the above line substituted? Does Python have some stack of strings and it pops them and replaces %s ? That would be later used in something like: print cmd % ('foo','boo','bar') What you're seeing is just a string assignment with fields in it which will later be filled in. Basics of python string formatting Not a specific answer to your line of code, but since you said you're new to python I thought I'd use this as an example to share some joy ;) Simple Example Inline With a

How do I use the C preprocessor to make a substitution with an environment variable

半城伤御伤魂 提交于 2019-11-30 09:14:57
问题 In the code below, I would like the value of THE_VERSION_STRING to be taken from the value of the environment variable MY_VERSION at compile time namespace myPluginStrings { const char* pluginVendor = "me"; const char* pluginRequires = THE_VERSION_STRING; }; So that if I type: export MY_VERSION="2010.4" pluginRequires will be set at "2010.4", even if MY_VERSION is set to something else at run time. UPDATE: (feb 21) Thanks for your help everyone. It works. As I'm using Rake as a build system,

deparse(substitute(x)) in lapply?

谁都会走 提交于 2019-11-30 06:27:04
I would like use a function that uses the standard deparse(substitute(x)) trick within lapply . Unfortunately I just get the argument of the loop back. Here's my completely useless reproducible example: # some test data a <- 5 b <- 6 li <- list(a1=a,b2=b) # my test function tf <- function(obj){ nm <- deparse(substitute(obj)) res <- list(myName=nm) res } tf(a) #returns $myName [1] "a" which is fine. If I use lapply I either get [[1L]] or the x argument of an anonymous function. lapply(li,function(x) tf(x)) # returns $a1 $a1$myName [1] "x" $b2 $b2$myName [1] "x" Is there any way to obtain the

Perl: Loop through a file and substitute

这一生的挚爱 提交于 2019-11-30 05:18:45
问题 I simply wanna read in a logfile, do a search and replace, and then write out the changes to that same logfile. What's the best practice way of doing this in Perl? 回答1: I normally code up a one liner for this: perl -i -pe 's/some/thing/' log.file See Here 回答2: This is often done with a one-liner: perl -pi.bak -e "s/find/replace/g" <file> Note the -i.bak portion -- this creates a backup file with the extension .bak . If you want to play without a net you can do this to overwrite the existing

Perl regex substitution using external parameters

为君一笑 提交于 2019-11-30 04:56:43
问题 Consider the following example: my $text = "some_strange_thing"; $text =~ s/some_(\w+)_thing/no_$1_stuff/; print "Result: $text\n"; It prints "Result: no_ strange _stuff" So far so good. Now, I need to get both the match and replacement patterns from external sources (user input, config file, etc). Naive solution appears to be like this: my $match = "some_(\\w+)_thing"; my $repl = "no_\$1_stuff"; my $text = "some_strange_thing"; $text =~ s/$match/$repl/; print "Result: $text\n"; However:

Substitute the n-th occurrence of a word in vim

China☆狼群 提交于 2019-11-30 03:28:50
I saw other questions dealing with the finding the n-th occurrence of a word/pattern, but I couldn't find how you would actually substitute the n-th occurrence of a pattern in vim. There's the obvious way of hard coding all the occurrences like :s/.*\(word\).*\(word\).*\(word\).*/.*\1.*\2.*newWord.*/g Is there a better way of doing this? John Kugelman You can do this a little more simply by using multiple searches. The empty pattern in the :s/pattern/repl/ command means replace the most recent search result. :/word//word//word/ s//newWord/ or :/word//word/ s/word/newWord/ You could then repeat

What causes substitution in Vim to only match one element per line?

余生颓废 提交于 2019-11-30 03:25:21
问题 I've been making a lot of changes to my .vimrc lately, and somewhere along the line I've introduced an undesirable feature. When performing a substitution command where the search token appears more than once per line, only the first token is changed (although the remaining tokens are highlighted as a result of the substitution). I have seen a few posts here about how to enable this behavior on a case-by-case basis, but I have yet to see something about what would cause this to be the default

Default substituting %s in python scripts

情到浓时终转凉″ 提交于 2019-11-29 17:05:15
问题 Sometimes in Python scripts I see lines like: cmd = "%s/%s_tb -cm cond+line+fsm -ucli -do \"%s\"" Where is the %s in the above line substituted? Does Python have some stack of strings and it pops them and replaces %s ? 回答1: That would be later used in something like: print cmd % ('foo','boo','bar') What you're seeing is just a string assignment with fields in it which will later be filled in. 回答2: Basics of python string formatting Not a specific answer to your line of code, but since you

Command substitution within sed expression

两盒软妹~` 提交于 2019-11-29 16:53:06
I'm having little problem with bash/sed. I need to be able to use command substitution within sed expression. I have two big text files: first is logfile.txt which sometimes* shows error messages by ID (0xdeadbeef is common example) in format ERRORID:0xdeadbeef second errors.txt has error messages stored in pairs LONG_ERROR_DESCRIPTION, 0xdeadbeef I was trying to use sed with bash command substitution to do the task: cat logfile.txt | sed "s/ERRORID:\(0x[0-9a-f]*\)/ERROR:$(cat errors.txt | grep \1 | grep -o '^[A-Z_]*' )/g" (^^^ this should be in one line of course) If it would work then I

Substitution of characters limited to part of each input line

梦想与她 提交于 2019-11-29 16:45:30
Have a file eg. Inventory.conf with lines like: Int/domain—home.dir=/etc/int I need to replace / and — before the = but not after. Result should be: Int_domain_home_dir=/etc/int I have tried several sed commands but none seem to fit my need. SLePort Sed with a t loop (BRE): $ sed ':a;s/[-/—.]\(.*=\)/_\1/;ta;' <<< "Int/domain—home.dir=/etc/int" Int_domain_home_dir=/etc/int When one of the -/—. character is found, it's replaced with a _ . Following text up to = is captured and output using backreference. If the previous substitution succeeds, the t command loops to label :a to check for further