Questions tagged [string-substitution]

Substituting string excerpts via sed, awk and other utilities.

220 questions
1
vote
3 answers

perl multiline regex to separate comments within paragraphs

The script below works, but it requires a kludge. By "kludge" I mean a line of code which makes the script do what I want --- but I do not understand why the line is necessary. Evidently, I do not understand exactly what the multiline regex…
Jacob Wegelin
  • 1,304
  • 11
  • 16
1
vote
2 answers

Replace character in a dataframe with another character

I want to change the character "F" to "X" in a dataframe. Please see below. df <- data.frame(N=c(1,2,3,4,5,6),CAT=c('A','B','C','D','E','F')) df Result: N CAT 1 1 A 2 2 B 3 3 C 4 4 D 5 5 E 6 6 F I've run…
H.Cheung
  • 855
  • 5
  • 12
1
vote
2 answers

Complex string with special characters substitutions

I've to substitute the string blacklist:[/^\/_/,/\/[^\/?]+\.[^\/]+$/] with blacklist: [/^\/_/,/\/[^\/?]+\.[^\/]+$/, /\/first|\/second|\/third/] in a file. I would like to use sedcommand to do it in shell. I opted to use ~ as separator and I tried…
1
vote
1 answer

Strange Ruby behavior when replacing with \'X string

I am generating some RTF strings and need the \'bd code. I am having problem with the sub and gsub commands. puts 'abc'.sub('a',"\\'B") => "bcBbc" The statement replaces the target with 'B' without the \', and copies the remainder of the string to…
Mike Blyth
  • 4,158
  • 4
  • 30
  • 41
1
vote
2 answers

How to remove a known last part from commands output string in one line?

To rephrase - I want to use Bash command substitution and string substitution in the same line. My actual commands are longer, but the ridiculous use of echo here is just a "substitution" for shortness and acts the same - with same errors ;) I know…
uldics
  • 117
  • 1
  • 11
1
vote
2 answers

More efficient method than string replace with ruby gsub

I have a third party JSON feed which is huge - lots of data. Eg { "data": [{ "name": "ABC", "price": "2.50" }, ... ] } I am required to strip the quotation marks from the price as the consumer of the JSON feed requires it in…
amburnside
  • 1,943
  • 5
  • 24
  • 43
1
vote
3 answers

Python, context sensitive string substitution

Is it possible to do something like this in Python using regular expressions? Increment every character that is a number in a string by 1 So input "123ab5" would become "234ab6" I know I could iterate over the string and manually increment each…
Mike
  • 58,961
  • 76
  • 175
  • 221
1
vote
1 answer

Why does my variable work in a test statement, but not in a case statement until I use echo to read it into itself?

I have a line with a comment. I use parameter substitution to condition the line into a variable "source". A test statement shows that the value of source is "Simple:", but the case statement can't match it. If I use command substitution to…
1
vote
1 answer

Python %s substitution works until I concatenate strings - what am I doing wrong?

I'm creating HTML with Python, with boilerplate formatting and substituted text using %s (I'm using %s because I'm posting on codepad.org, which runs Python2, even though I'm testing this using Python3). If I write a single block of HTML, with %s…
seqandmore
  • 13
  • 4
1
vote
1 answer

How to use Ruby gsub with regex to do partial string substitution

I have a pipe delimited file which has a line H||CUSTCHQH2H||PHPCCIPHP|1010032000|28092017|25001853|||| I want to substitute the date (28092017) with a regex "[0-9]{8}" if the first character is "H" I tried the following example to test my…
Banjo
  • 91
  • 2
  • 9
1
vote
3 answers

In R, how do I map multiple values to different values based on a conversion table?

I have a large vector of many values. I also have a table that shows what each of those values should be converted to. I know how to do this for one value of a vector at a time using gsub, but I'm not sure how to do this for all values…
Jay
  • 442
  • 1
  • 5
  • 13
1
vote
1 answer

why do I have to pipe perl output into a new perl command

I want to remove whitespace between back-tick (\x60) and word characters, inside of a perl script that performs many other regex substitutions. If I print the result of the earlier substitutions to the bash shell and then pipe into a new perl…
Jacob Wegelin
  • 1,304
  • 11
  • 16
1
vote
2 answers

Perl string substitution in bash using field separator - Skip header line

I am working on the anonymisation of several fields in a semicolon-separated text file. For now I have the following command: perl -aF'(;)' -ne "s/^.{$length}/$x_string/ for @F[2*$index]; print @F" file Where $index corresponds to the index of the…
Julien Camus
  • 43
  • 1
  • 4
1
vote
2 answers

Regular Expression: PATTERN with exception using AWK gsub

I have a data file (cou.data) USSR 8649 275 Asia Cananda 3852 25 North America China 3705 1032 Asia USA 3615 237 North America Brazil 3286 134 South America India 1267 746 Asia Mexico 762 78 North America France 211…
1
vote
2 answers

Remove character and multiply remainder with number in R

I have a simple data frame as follows: Date <- seq(as.Date("2013/1/1"), by = "day", length.out = 12) test < -data.frame(Date) test$Value <- c("1,4","2,3","3,6","< 1,4","2,3","3,6","1,4","2,3","3,6","< 1,4","2,3","3,6") I need to go through each…
Matt
  • 323
  • 2
  • 12