string-substitution

sed wildcard substitution

烂漫一生 提交于 2019-12-21 16:49:15
问题 I want to do a substitution based on a wildcard. For example, change all "tenure" to "disposition" only if the word "tenure" comes after an '=' sign. Basically a regex that would match this =.*tenure The sed command that I have so for this is: sed 's/=.*tenure/=.*disposition/g' file.txt However, if I pass this to a file containing: blah blah blah = change "tenure" to "disposition" I get blah blah blah =.*disposition" to "disposition" instead of: blah blah blah = change "disposition" to

Cannot Figure out how String Substitution works in Python 3.x [closed]

梦想的初衷 提交于 2019-12-19 20:49:36
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . In python 2.x you were allowed to do something like this: >>> print '%.2f' % 315.15321531321 315.15 However, i cannot get it to work for python 3.x, I

Why does the pattern *([^\/]) work in my interactive shell but not a script? [duplicate]

两盒软妹~` 提交于 2019-12-11 20:42:56
问题 This question already has an answer here : Bash parameter expansion works only in interactive shell, but not in script (1 answer) Closed 3 years ago . I'm writing a script to strip a working directory to a limited length using bash parameter substitution. It works on the command line, but the same substitution does nothing in the script. The code: #!/bin/bash # Limit of working directory length LIMIT=10 dir=${1/#$HOME/\~} # If it's too long, normalize it by stripping ~ and adding ... if [ ${

Skipping particular positions in a string using substitution operator in perl

主宰稳场 提交于 2019-12-11 03:19:59
问题 Yesterday, I got stuck in a perl script. Let me simplify it, suppose there is a string (say ABCDEABCDEABCDEPABCDEABCDEPABCDEABCD), first I've to break it at every position where "E" comes, and secondly, break it specifically where the user wants to be at. But, the condition is, program should not cut at those sites where E is followed by P. For example there are 6 Es in this sequence, so one should get 7 fragments, but as 2 Es are followed by P one will get 5 only fragments in the output. I

Search-and-replace on a list of strings - gsub eapply?

前提是你 提交于 2019-12-08 12:25:19
问题 Here is a simplified excerpt of my code for reproduction purposes: library("quantmod") stockData <- new.env() stocksLst <- c("AAB.TO", "BBD-B.TO", "BB.TO", "ZZZ.TO") nrstocks = length(stocksLst) startDate = as.Date("2016-09-01") for (i in 1:nrstocks) { getSymbols(stocksLst[i], env = stockData, src = "yahoo", from = startDate) } My data is then stored in this environment stockData which I use to do some analysis. I'd like to clean up the names of the xts objects, which are currently: ls

How to use ruby gsub Regexp with many matches?

和自甴很熟 提交于 2019-12-05 09:53:36
问题 I have csv file contents having double quotes inside quoted text test,first,line,"you are a "kind" man",thanks again,second,li,"my "boss" is you",good I need to replace every double quote not preceded or succeeded by a comma by "" test,first,line,"you are a ""kind"" man",thanks again,second,li,"my ""boss"" is you",good so " is replaced by "" I tried x.gsub(/([^,])"([^,])/, "#{$1}\"\"#{$2}") but didn't work 回答1: Your regex needs to be a little more bold, in case the quotes occur at the start

sed wildcard substitution

时光毁灭记忆、已成空白 提交于 2019-12-04 08:17:54
I want to do a substitution based on a wildcard. For example, change all "tenure" to "disposition" only if the word "tenure" comes after an '=' sign. Basically a regex that would match this =.*tenure The sed command that I have so for this is: sed 's/=.*tenure/=.*disposition/g' file.txt However, if I pass this to a file containing: blah blah blah = change "tenure" to "disposition" I get blah blah blah =.*disposition" to "disposition" instead of: blah blah blah = change "disposition" to "disposition" How do I do the substitution such that the wildcard in the regex won't be part of the

How to use ruby gsub Regexp with many matches?

拈花ヽ惹草 提交于 2019-12-03 22:28:55
I have csv file contents having double quotes inside quoted text test,first,line,"you are a "kind" man",thanks again,second,li,"my "boss" is you",good I need to replace every double quote not preceded or succeeded by a comma by "" test,first,line,"you are a ""kind"" man",thanks again,second,li,"my ""boss"" is you",good so " is replaced by "" I tried x.gsub(/([^,])"([^,])/, "#{$1}\"\"#{$2}") but didn't work Phrogz Your regex needs to be a little more bold, in case the quotes occur at the start of the first value, or at the end of the last value: csv = <<ENDCSV test,first,line,"you are a "kind"

Perl hash substitution with special characters in keys

爱⌒轻易说出口 提交于 2019-12-02 13:58:54
My current script will take an expression, ex: my $expression = '( a || b || c )'; and go through each boolean combination of inputs using sub/replace, like so: my $keys = join '|', keys %stimhash; $expression =~ s/($keys)\b/$stimhash{$1}/g; So for example expression may hold, ( 0 || 1 || 0 ) This works great. However, I would like to allow the variables (also in %stimhash) to contain a tag, *. my $expression = '( a* || b* || c* )'; Also, printing the keys of the stimhash returns: a*|b*|c* It is not properly substituting/replacing with the extra special character, *. It gives this warning: Use

Cannot Figure out how String Substitution works in Python 3.x [closed]

坚强是说给别人听的谎言 提交于 2019-12-01 18:31:01
In python 2.x you were allowed to do something like this: >>> print '%.2f' % 315.15321531321 315.15 However, i cannot get it to work for python 3.x, I tried different things, such as >>> print ('%.2f') % 315.15321531321 %.2f Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for %: 'NoneType' and 'float' >>> print ("my number %") % 315.15321531321 my number % Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for %: 'NoneType' and 'float' Then, I read about the .format() method