Answers for "linux replace part of string"

1

linux substitute string

#replace all string occurence
sed -i 's/old-text/new-text/g' input.txt 

#replace all string occurence and create new file
sed -i 's/old-text/new-text/g' input.txt > newfile.txt

#see link 
#https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/
Posted by: Guest on January-04-2022
3

replace substring in shell

#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".)
Posted by: Guest on August-13-2020

Code answers related to "linux replace part of string"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language