Linux replace string with string

How to Replace Substring from String — Bash

In this short article, we will see how to replace a string with another in bash script. We will cover multiple examples and situations.

Suppose we have 3 variables:

text="Hello World!" repl="Bash" str_find="World" 
  • text — the main string from which we will replace
  • repl — the new string / replacement
  • str_find — the search pattern which will be replaced

The image below shows most examples and results of string replacement in bash:

Replace substring with new in string

We use the following syntax to replace string in Bash:

Example of replacing string with / :

text="Hello World! World!" repl="Bash" str_find="World" echo "$" 

The code above will replace only the first match. To replace all occurrences, check the next solution.

bash replace all occurrences string

To replace all occurrences of substring in text we need to use Bash operator — // .

replace string by sed

result=$(echo "$text" | sed "s/$str_find/$repl/") 

sed — replace all occurrences

by adding g at the end of the sed command we can replace all occurrences of given pattern in a string

result=$(echo "$text" | sed "s/$str_find/$repl/g") 

replace string by empty string

regex: search and replace

We can use regex to replace all letters or numbers from string in bash:

text="Hello World! 735!" echo $ echo $ 

The new string after the replacements looks like

WWWWW WWWWW! 735! Hello World! NNN! 

regex + sed: search and replace

We can use regex to search for a pattern and replace all matches in bash script:

text="Hello World! 735!" echo "$text" | sed -e 's/[a-zA-Z]/W/g' -e 's/6/N/g' 

the result is replace all letters with W and digits by D:

replace all newlines to space

text="line 1 line 2 end" echo "$" 

Источник

Replace one substring for another string in shell script

For the record, this may have been a fine question once upon a time, but since many years now, Stack Overflow does not encourage «give me code» type of questions. Please don’t take this as a good example of how to ask questions here.

Читайте также:  Linux on g4 ibook g4

@tripleee what exactly is the purpose of Stack Overflow if people aren’t allowed to ask for code examples?

Most questions of this type are too broad, and these days also duplicates. Probably review the help center and in particular How to ask as well as the guidance for providing a minimal reproducible example. Perhaps see also the Stack Overflow homework FAQ which expands on a related topic.

16 Answers 16

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 "$" # prints 'I love Sara and Marry' 

To replace all occurrences, use $parameter//pattern/string> :

message='The secret code is 12345' echo "$" # prints 'The secret code is XXXXX' 

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».

@ruakh how do I write this statement with a or condition. Just if I want to replace Suzi or Marry with new string.

@Bu: No, because \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=»$>» . (Though you probably want STRING// — replace-all — instead of just STRING/ .)

To be clear, since this confused me for a bit, the first part has to be a variable reference. You can’t do echo $ . You’d need input=»my string foo»; echo $

This can be done entirely with bash string manipulation:

first="I love Suzy and Mary" second="Sara" first=$ 

That will replace only the first occurrence; to replace them all, double the first slash:

first="Suzy, Suzy, Suzy" second="Sara" first=$ # first is now "Sara, Sara, Sara" 

What if first or second contain special characters, like / , $ , < , >, , . , + , ( , ) , * , etc.? (Problems with formatting of backslash in this comment.) Perhaps address that in the answer?

As to wildcards this worked for me r_getfilter=»$» to replace .. with .* (a grep «match anything» string). Using ‘.*’ did not go well, the single quotes ended up in the substitution.

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") 

I got this: $ echo $(echo $firstString | sed ‘s/Suzi/$secondString/g’) I love $secondString and Marry

Читайте также:  Kali linux запустить network manager

@Qstnr_La use double quotes for variable substitution: result=$(echo $firstString | sed «s/Suzi/$secondString/g»)

I fixed the single quotes and also added the missing quotes around the echo argument. It deceptively works without quoting with simple strings, but easily breaks on any nontrivial input string (irregular spacing, shell metacharacters, etc).

In sh (AWS Codebuild / Ubuntu sh) I found that I need a single slash at the end, not a double. I’m going to edit the comment as the comments above also show a single slash.

I guess this is tagged «bash» but came here because needed something simple for another shell. This is a nice succinct alternative to what wiki.ubuntu.com/… made it look like I’d need.

@NamGVU, or anyone else getting that error, it usually means that one of the strings in your search or replace section has the same character as your delimiters. For example, it’s common to use / as a delimiter, but if your strings contain *nix directories, then you’ll see that error since they also have the / character.

It’s better to use Bash than sed if strings have regular expression characters.

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:

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:

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.

This answer stopped me from using 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?

@MarkHaferkamp See this from the «further reading recommended» link. About «escaping the ~ «: 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.

Читайте также:  Linux with directx 11

The «further reading» link explains the «#». On Bash 4.3.30, echo «$» 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.

Источник

Linux command to replace string in LARGE file with another string

I have a huge SQL file that gets executed on the server. The dump is from my machine and in it there are a few settings relating to my machine. So basically, I want every occurance of «c://temp» to be replace by «//home//some//blah» How can this be done from the command line?

7 Answers 7

sed is a good choice for large files.

sed -i.bak -e 's%C://temp%//home//some//blah%' large_file.sql 

It is a good choice because doesn’t read the whole file at once to change it. Quoting the manual:

A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

The relevant manual section is here. A small explanation follows

-i.bak enables in place editing leaving a backup copy with .bak extension

s%foo%bar% uses s, the substitution command, which substitutes matches of first string in between the % sign, ‘foo’, for the second string, ‘bar’. It’s usually written as s// but because your strings have plenty of slashes, it’s more convenient to change them for something else so you avoid having to escape them.

vinko@mithril:~$ sed -i.bak -e 's%C://temp%//home//some//blah%' a.txt vinko@mithril:~$ more a.txt //home//some//blah D://temp //home//some//blah D://temp vinko@mithril:~$ more a.txt.bak C://temp D://temp C://temp D://temp

Источник

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