Replace string linux bash

Replace a string in shell script using a variable

but it’s getting replaced with $replace instead of the value of that variable. Could anybody tell what went wrong?

For the common related question about handling values with slashes in them, see stackoverflow.com/questions/5864146/use-slashes-in-sed-replace

12 Answers 12

If you want to interpret $replace , you should not use single quotes since they prevent variable substitution.

echo $LINE | sed -e "s/12345678/$/g" 
pax> export replace=987654321 pax> echo X123456789X | sed "s/123456789/$/" X987654321X pax> _ 

Just be careful to ensure that $ doesn’t have any characters of significance to sed (like / for instance) since it will cause confusion unless escaped. But if, as you say, you’re replacing one number with another, that shouldn’t be a problem.

paxdiablo: set is also not necessary (and how were you going to use it anyway?). Just replace=987654321 .

I usually always use export to ensure that variables are set for children but, as you say, you could just as easily avoid it. However, the use or not of export is irrelevant here (a style issue) and has no effect on the actual answer, which is how to use variables within a sed command.

This solution doesn’t seem to be working with the -i option. Would you know why? or how to make it work?

Maybe also point out that switching from single to double quotes requires any $ or ` in the sed script to be escaped with a backslash, to protect it from the shell’s substitution mechanism for double-quoted strings.

Читайте также:  Rooting android with linux

you can use the shell (bash/ksh).

$ var="12345678abc" $ replace="test" $ echo $ testabc 

May want to mention that’s bash-specific (and ksh?). Probably not of import to most people but some of us are still forced to work on ancient UNIXen 🙂

Not specific to the question, but for folks who need the same kind of functionality expanded for clarity from previous answers:

# create some variables str="someFileName.foo" find=".foo" replace=".bar" # notice the the str isn't prefixed with $ # this is just how this feature works :/ result=$ echo $result # result is: someFileName.bar str="someFileName.sally" find=".foo" replace=".bar" result=$ echo $result # result is: someFileName.sally because ".foo" was not found 

Источник

Replacing a Substring With Another String in Bash

Learn how to replace a single or multiple occurrences of a substring inside a string in Bash.

Here’s the scenario. You have a big string and you want to replace part of it with another string.

For example, you want to change «I am writing a line today» to «I am writing a line now«.

In this quick tutorial, I’ll show you how to replace a substring natively in Bash. I’ll also show the sed command example as an extension.

Replace substring natively in bash (good for a single line)

Bash has some built-in methods for string manipulation. If you want to replace part of a string with another, this is how you do it:

Create a string variable consisting of the line: “I am writing a line today” without the quotes and then replace today with now :

[email protected]:~$ line="I am writing a line today" [email protected]:~$ echo "$" I am writing a line now 

Did you understand what just happened? In the syntax «$» , line is the name of the variable where I’ve just stored the entire sentence. Here, I’m instructing it to replace the first occurrence of the word today with now . So instead of displaying the contents of the original variable, it showed you the line with the changed word.

Читайте также:  Свой центр сертификации linux

Hence, the line variable hasn’t actually changed. It is still the same:

[email protected]:~$ echo $line I am writing a line today 

But you can definitely replace the word you want to and modify the same variable to make the changes permanent:

[email protected]:~$ line="$" [email protected]:~$ echo $line I am writing a line now 

Now the changes have been made permanent and that’s how you can permanently replace the first occurrence of a substring in a string.

You can also use other variables to store specific substrings that you wish to replace:

[email protected]:~$ replace="now" [email protected]:~$ replacewith="today" [email protected]:~$ line="$/$>" [email protected]ktop:~$ echo $line I am writing a line today 

Here, I stored the word to be replaced in a variable called replace and the word that it would be replaced with inside replacewith . After that, I used the same method as discussed above to “revise” the line. Now, the changes I had made in the beginning of this tutorial have been reverted.

Let us look at another example:

[email protected]:~$ hbday="Happy Birthday! Many Many Happy Returns!" [email protected]:~$ hbday="$" [email protected]:~$ echo $hbday Happy Birthday! So Many Many Happy Returns! 

Replacing all occurrences of a substring

You can also replace multiple occurrences of substrings inside strings. Let’s see it through another example:

[email protected]:~$ hbday="$" [email protected]:~$ echo $hbday Happy Birthday! So Many So Many Happy Returns! 

That extra / after hbday made it replace all occurrences of Many with So Many inside the sentence.

Replace string using sed command (can work on files as well)

Here’s another way for replacing substrings in a string in bash. Use the sed command in this manner:

Читайте также:  Linux автоматическое монтирование ntfs разделов

Replace the first occurrence:

line=$(sed "s/$replace/s//$replacewith/" 

If I take the first example, it can be played as:

Replace bash substring with sed

You can also replace all occurrences by adding g at the end:

line=$(sed "s/$replace/$replacewith/g" 

Now, you may think this is more complicated than the native bash string method. Perhaps, but sed is very powerful and you can use it to replace all the occurrences of a string in a file.

sed -i 's/$replace/$replacewith/' filename

Here's an example of some of Agatha Christie's books with 'Murder' in the title:

[email protected]:~$ cat agatha.txt The Murder in the Vicarage Murder in Mesopotamia Murder is Easy Murder on the Orient Express Murder In Retrospect 

I am going to replace Murder with Marriage because some people think both are the same:

sed -i "s/Murder/Marriage/g" agatha.txt

And here's the changed file now:

The Marriage in the Vicarage Marriage in Mesopotamia Marriage is Easy Marriage on the Orient Express Marriage In Retrospect

Sed is a very powerful tool for editing text files in Linux. You should at least learn its basics.

Now that you know about replacing substrings, how about splitting strings into substrings in bash?

Did you enjoy reading this article? If you have anything to share about replacing strings or this article, please do so in the comments below.

Источник

Оцените статью
Adblock
detector