I have "I love Suzi and Marry" and I want to change "Suzi" to "Sara".
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
# do something...
The result must be like this:
firstString="I love Sara and Marry"
I have "I love Suzi and Marry" and I want to change "Suzi" to "Sara".
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
# do something...
The result must be like this:
firstString="I love Sara and Marry"
To replace the first occurrence of a pattern with a given string, use ${parameter/pattern/string}
:
#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/"$secondString"}"
# prints 'I love Sara and Marry'
To replace all occurrences, use ${parameter//pattern/string}
:
message='The secret code is 12345'
echo "${message//[0-9]/X}"
# prints 'The secret code is XXXXX'
(This is documented in the Bash Reference Manual, §3.5.3 "Shell Parameter Expansion".)
Note that this feature is not specified by POSIX — it's a Bash extension — so not all Unix shells implement it. For the relevant POSIX documentation, see The Open Group Technical Standard Base Specifications, Issue 7, the Shell & Utilities volume, §2.6.2 "Parameter Expansion".
$STRING="${STRING/\n/<br />}"
Jul 23, 2015 at 1:34
\n
in that context would represent itself, not a newline. I don't have Bash handy right now to test, but you should be able to write something like, $STRING="${STRING/$'\n'/<br />}"
. (Though you probably want STRING//
-- replace-all -- instead of just STRING/
.)
echo ${my string foo/foo/bar}
. You'd need input="my string foo"; echo ${input/foo/bar}
This can be done entirely with bash string manipulation:
first="I love Suzy and Mary"
second="Sara"
first=${first/Suzy/$second}
That will replace only the first occurrence; to replace them all, double the first slash:
first="Suzy, Suzy, Suzy"
second="Sara"
first=${first//Suzy/$second}
# first is now "Sara, Sara, Sara"
first
or second
contain special characters, like /
, $
, {
, }
, <backslash>, .
, +
, (
, )
, *
, etc.? (Problems with formatting of backslash in this comment.) Perhaps address that in the answer?
Jan 27 at 14:57
For Dash all previous posts aren't working
The POSIX sh
compatible solution is:
result=$(echo "$firstString" | sed "s/Suzi/$secondString/")
This will replace the first occurrence on each line of input. Add a /g
flag to replace all occurrences:
result=$(echo "$firstString" | sed "s/Suzi/$secondString/g")
result=$(echo $firstString | sed "s/Suzi/$secondString/g")
echo
argument. It deceptively works without quoting with simple strings, but easily breaks on any nontrivial input string (irregular spacing, shell metacharacters, etc).
Try this:
sed "s/Suzi/$secondString/g" <<<"$firstString"
It's better to use Bash than sed
if strings have regular expression characters.
echo ${first_string/Suzi/$second_string}
It's portable to Windows and works with at least as old as Bash 3.1.
To show you don't need to worry much about escaping, let's turn this:
/home/name/foo/bar
Into this:
~/foo/bar
But only if /home/name
is in the beginning. We don't need sed
!
Given that Bash gives us magic variables $PWD
and $HOME
, we can:
echo "${PWD/#$HOME/\~}"
Thanks for Mark Haferkamp in the comments for the note on quoting/escaping ~
.*
Note how the variable $HOME
contains slashes, but this didn't break anything.
Further reading: Advanced Bash-Scripting Guide.
If using sed
is a must, be sure to escape every character.
sed
with the pwd
command to avoid defining a new variable each time my custom $PS1
runs. Does Bash provide a more general way than magic variables to use the output of a command for string replacement? As for your code, I had to escape the ~
to keep Bash from expanding it into $HOME. Also, what does the #
in your command do?
May 26, 2015 at 0:05
~
": notice how I quoted stuff. Remember to always quote stuff! And this doesn't just work for magic variables: any variable is capable of substitutions, getting string length, and more, within bash. Congrats on trying to your $PS1
fast: you may also be interested in $PROMPT_COMMAND
if you are more comfortable in another programming language and want to code a compiled prompt.
May 27, 2015 at 19:33
echo "${PWD/#$HOME/~}"
doesn't replace my $HOME
with ~
. Replacing ~
with \~
or '~'
works. Any of these work on Bash 4.2.53 on another distro. Can you please update your post to quote or escape the ~
for better compatibility? What I meant by my "magic variables" question was: Can I use Bash's variable substitution on, e.g., the output of uname
without saving it as a variable first? As for my personal $PROMPT_COMMAND
, it's complicated.
May 30, 2015 at 7:46
If tomorrow you decide you don't love Marry either she can be replaced as well:
today=$(</tmp/lovers.txt)
tomorrow="${today//Suzi/Sara}"
echo "${tomorrow//Marry/Jesica}" > /tmp/lovers.txt
There must be 50 ways to leave your lover.
echo [string] | sed "s/[original]/[target]/g"
g
flag is hugely misunderstood. Without it, sed
will replace the first occurrence on each line but if you don't expect multiple occurrences per line, you don't need g
. (Frequently you see it in expressions where there could only ever be a single match per line, like s/.*everything.*/all of it/g
where obviously you are matching the entire line in the first place, so there is no way you could match the regex several times).
Since I can't add a comment. @ruaka To make the example more readable write it like this
full_string="I love Suzy and Mary"
search_string="Suzy"
replace_string="Sara"
my_string=${full_string/$search_string/$replace_string}
or
my_string=${full_string/Suzy/Sarah}
Using AWK:
firstString="I love Suzi and Marry"
echo $firstString | awk '{gsub("Suzi","Sara"); print}'
Pure POSIX shell method, which unlike Roman Kazanovskyi's sed
-based answer needs no external tools, just the shell's own native parameter expansions. Note that long file names are minimized so the code fits better on one line:
f="I love Suzi and Marry"
s=Sara
t=Suzi
[ "${f%$t*}" != "$f" ] && f="${f%$t*}$s${f#*$t}"
echo "$f"
Output:
I love Sara and Marry
How it works:
Remove Smallest Suffix Pattern. "${f%$t*}"
returns "I love
" if the suffix $t
"Suzi*
" is in $f
"I love
Suzi and Marry
".
But if t=Zelda
, then "${f%$t*}"
deletes nothing, and returns the whole string "I love Suzi and Marry
".
This is used to test if $t
is in $f
with [ "${f%$t*}" != "$f" ]
which will evaluate to true if the $f
string contains "Suzi*
" and false if not.
If the test returns true, construct the desired string using Remove Smallest Suffix Pattern ${f%$t*}
"I love
" and Remove Smallest Prefix Pattern ${f#*$t}
"and Marry
", with the 2nd string $s
"Sara
" in between.