Find and Replace in Shell Scripting

Find and replace in shell scripting

Sure, you can do this using sed or awk. sed example:

sed -i 's/Andrew/James/g' /home/oleksandr/names.txt

Find and Replace Inside a Text File from a Bash Command

The easiest way is to use sed (or perl):

sed -i -e 's/abc/XYZ/g' /tmp/file.txt

Which will invoke sed to do an in-place edit due to the -i option. This can be called from bash.

If you really really want to use just bash, then the following can work:

while IFS='' read -r a; do
echo "${a//abc/XYZ}"
done < /tmp/file.txt > /tmp/file.txt.t
mv /tmp/file.txt{.t,}

This loops over each line, doing a substitution, and writing to a temporary file (don't want to clobber the input). The move at the end just moves temporary to the original name. (For robustness and security, the temporary file name should not be static or predictable, but let's not go there.)

For Mac users:

sed -i '' 's/abc/XYZ/g' /tmp/file.txt

(See the comment below why)

Replace a string in shell script using a variable

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

Try:

echo $LINE | sed -e "s/12345678/${replace}/g"

Transcript:

pax> export replace=987654321
pax> echo X123456789X | sed "s/123456789/${replace}/"
X987654321X
pax> _

Just be careful to ensure that ${replace} 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.

Replace one substring for another string in shell script

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

Find and replace inside a string variable using sed command - shell scripting

You don't use -i, as that's for changing a file in place. If you want to replace the value in the variable, you need to reassign to it.

Find and replace text using a bash script

Perfect job for sed:

sed -i '/^AS_ADMIN_PASSWORD=/d' /tmp/password.txt

This deletes the line matching the given regex, in-place.

How to search and replace a content of a file using shell script?

I believe you missed the g option in your sed command. See below

sed "s/find/replace/g" filename

Find and replace in shell script (special characters)

To get the correct result with the least change to your command, try:

partitionno=$(echo "$new" | sed "s|$old||g")

There are two key points here:

  1. Shell variables are not expanded inside single quotes. So '$old' remains as the original four characters: $, o, l, and d. For the shell variables to be expanded, use double quotes.

  2. sed "s/$old//g" still won't work because there are too many slashes. A substitute command uses three slashes. After the shell expands $old, there are five slashes. The solution is to use a different delimiter for the substitute command. I chose | above because | is not likely to be in a file name.



Related Topics



Leave a reply



Submit