Questions tagged [substitution]

The action of replacing something with another thing.

The action of replacing something with another thing.

1879 questions
479
votes
3 answers

Substitute multiple whitespace with single whitespace in Python

I have this string: mystring = 'Here is some text I wrote ' How can I substitute the double, triple (...) whitespace chracters with a single space, so that I get: mystring = 'Here is some text I wrote'
creativz
  • 10,369
  • 13
  • 38
  • 35
427
votes
3 answers

JavaScript - Replace all commas in a string

I have a string with multiple commas, and the string replace method will only change the first one: var mystring = "this,is,a,test" mystring.replace(",","newchar", -1) Result: "thisnewcharis,a,test" The documentation indicates that the default…
mike
  • 22,931
  • 31
  • 77
  • 100
238
votes
14 answers

bash : Bad Substitution

#!/bin/bash jobname="job_201312161447_0003" jobname_pre=${jobname:0:16} jobname_post=${jobname:17} This bash script gives me Bad substitution error on ubuntu. Any help will be highly appreciated.
Arindam Choudhury
  • 2,816
  • 2
  • 18
  • 22
187
votes
3 answers

Command substitution: backticks or dollar sign / paren enclosed?

What's the preferred way to do command substitution in bash? I've always done it like this: echo "Hello, `whoami`." But recently, I've often seen it written like this: echo "Hello, $(whoami)." What's the preferred syntax, and why? Or are they…
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
145
votes
4 answers

sed error: "invalid reference \1 on `s' command's RHS"

I run several substitution commands as the core of a colorize script for maven. One of the sed commands uses a regular expression which works find in the shell as discussed here. The current (not working) implementation can be found here. When I…
JJD
  • 50,076
  • 60
  • 203
  • 339
137
votes
2 answers

How to avoid heredoc expanding variables?

I'm trying to create a script file using substitution string from ENV but want also to prevent some from escaping export PLACEHOLDER1="myPlaceholder1Value" sudo /bin/su -c "cat << EOF >…
TheCodeKiller
  • 1,733
  • 2
  • 12
  • 18
84
votes
6 answers

sed substitution with Bash variables

I am trying to change the values in a text file using sed in a Bash script with the line, sed 's/draw($prev_number;n_)/draw($number;n_)/g' file.txt > tmp This will be in a for loop. Why is it not working?
csta
  • 2,423
  • 5
  • 26
  • 34
70
votes
2 answers

Substitute with contents of register or lines range from elsewhere in file in Vim

I'm using Vim, and I want to substitute some placeholder text with a long string, that spans several lines, which is already written somewhere else in the file. Is it possible to replace a pattern with the contents of a register? Something…
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
63
votes
8 answers

Remove all newlines from inside a string

I'm trying to remove all newline characters from a string. I've read up on how to do it, but it seems that I for some reason am unable to do so. Here is step by step what I am doing: string1 = "Hello \n World" string2 = string1.strip('\n') print…
LandonWO
  • 1,249
  • 3
  • 14
  • 16
60
votes
9 answers

How can I use a variable in the replacement side of the Perl substitution operator?

I would like to do the following: $find = "start (.*) end"; $replace = "foo \1 bar"; $var = "start middle end"; $var =~ s/$find/$replace/; I would expect $var to contain "foo middle bar", but it does not work. Neither does: $replace = 'foo \1…
Manu
  • 28,753
  • 28
  • 75
  • 83
48
votes
7 answers

How to substitute text from files in git history?

I've always used an interface based git client (smartGit) and thus don't have much experience with the git console. However, I now face the need to substitute a string in all .txt files from history (so, not erasing the whole file but just…
Tom
  • 8,536
  • 31
  • 133
  • 232
38
votes
1 answer

replace newline character in bash variable?

I am trying to understand the "cdargs-bash.sh" script with cdargs packages. And I have a question about in the following function: function _cdargs_get_dir () { local bookmark extrapath # if there is one exact match (possibly with extra path info…
yorua007
  • 803
  • 3
  • 9
  • 14
38
votes
8 answers

How to repeat a command with substitution in Vim?

In Unix the ^ allows you to repeat a command with some text substituted for new text. For example: csh% grep "stuff" file1 >> Results grep "stuff" file1 csh% ^file1^file2^ grep "stuff" file2 csh% Is there a Vim equivalent? There are a lot of times…
Whaledawg
  • 4,234
  • 4
  • 26
  • 21
37
votes
2 answers

Why does my Bash code fail when I run it with 'sh'?

I have a line of code that works fine in my terminal: for i in *.mp4; do echo ffmpeg -i "$i" "${i/.mp4/.mp3}"; done Then I put the exact same line of code in a script myscript.sh: #!/bin/sh for i in *.mp4; do echo ffmpeg -i "$i" "${i/.mp4/.mp3}";…
WhatIsName
  • 2,294
  • 4
  • 24
  • 36
32
votes
3 answers

Adding Line Break After pattern in VIM

I have a css file and I want to add an empty line after every }. How can I do this in Vim?
Chalist
  • 3,160
  • 5
  • 39
  • 68
1
2 3
99 100