File replace command in linux

How to Replace a String in a File in Bash

As a programmer, you might need to work with different types of files to store data temporarily or permanently. Sometimes, you may need to replace part of the file or modify the particular content of the file. To replace content in a file, you must search for the particular file string. The ‘sed’ command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The ‘awk’ command can also be used to replace the string in a file. This tutorial will show you how to replace any string value from a file using a bash script.

Sample Data for Bash String Replacement

A text file named Sales.txt with the following content is created to show the replacement operations.
Sales.txt

01/01/2020 60000 Dhaka
10/02/2020 76000 Rajshahi
21/03/2020 54000 Khulna
15/04/2020 78000 Chandpur
17/05/2020 45000 Bogra
02/06/2020 67000 Comilla

General Syntax for Sed: Replace String in a File

The basic syntax of the `sed` command for replacing the particular string in a file is given below.

Every part of the above syntax is explained below.

‘-i’ option is used to modify the content of the original file with the replacement string if the search string exists in the file.
‘s’ indicates the substitute command.
‘search_string’ contains the string value that will be searched in the file for replacement.
‘replace_string’ contains the string value that will be used to replace the content of the file that matches the ‘search_string’ value.
‘filename’ contains the filename where the search and replace will be applied.

Example 1: Replace File with the ‘sed’ Command

In the following script, the search-and-replace text will be taken from the user. If the search string exists in ‘Sales.txt’, then it will be replaced by the replacement string. Here, a case-sensitive search will be performed.

# Assign the filename
filename = «Sales.txt»

# Take the search string
read -p «Enter the search string: » search

# Take the replace string
read -p «Enter the replace string: » replace

if [ [ $search ! = «» && $replace ! = «» ] ] ; then
sed -i «s/ $search / $replace /» $filename
fi

Example 2: Replace File with the ‘sed’ Command with ‘g’ and ‘i’ Flag

The following script will work like the previous example, but the search string will be searched globally for the ‘g’ flag, and the case-insensitive search will be done for the ‘i’ flag.

# Take the search string
read -p «Enter the search string: » search

Читайте также:  Где находятся иконки приложений linux

# Take the replace string
read -p «Enter the replace string: » replace

if [ [ $search ! = «» && $replace ! = «» ] ] ; then
sed -i «s/ $search / $replace /gi» $1
fi

Example 3: Replace File with `sed` Command and Matching Digit Pattern

The following script will search for all numerical content in a file and will replace the content by adding the ‘$’ symbol at the beginning of the numbers.

# Check the command line argument value exists or not
if [ $1 ! = «» ] ; then
# Search all string containing digits and add $
sed -i ‘s/\b2\\b/$&/g’ $1
fi

Replace String in a File with `awk` Command

The ‘awk’ command is another way to replace the string in a file, but this command cannot update the original file directly like the ‘sed’ command.

Example 4: Replace File with `awk` Command

The following script will store the updated content in the temp.txt file that will be renamed by the original file.

# Check the command line argument value exists or not
if [ $1 ! = «» ] ; then
# Search all string based on date
awk ‘ ‘ $1 > temp.txt && mv temp.txt $1
fi

In the above example `awk` command is used to search for a string “02/06/2020”, which is the last date in our sample file. The matched string will be replaced by a new string “12/06/2020”. The string resulting whether substituted or not is printed on each line.

Example 5: Replace string in file with Pure Bash Scripting

In this example we will do the same job with a Bash while loop and a bash string replacement command. We will write the output to a tmp file and replace the original at the end of the script. See the code below:

# Replace all instances on line of Khulna with Dhaka
echo $

mv / tmp / Sales.o.txt Sales.txt

You can see the echo command will do a replacement in the line variable, as the script reads each line in the text file, and replace all instances of Khulna with the new string Dhaka. The while loop reads from the input file Sales.txt and writes to the output file /tmp/Sales.o.txt. Finally the temporary output file replaces the original file with the mv command.

Example 6: Replace string in file with Perl one liner

This task is actually super simple with Perl. Perl is an old, less popular language but extremely powerful. This can be done with a line command in the bash command line:

-pi.bak tells perl to process the file in place and also create a backup file with the .bak extension. The replacement will be all instances of Dhaka for Bogra as the new text. Here you can see the easy output of this command:

Conclusion

This article showed you how to use bash scripts to replace strings in a file with search and replace on the command line in bash. There are many ways to do this including with commands sed, awk, perl; you can use native bash scripting with loops and regex and there are more ways even not discussed here. Bash and linux have many ways to do the same job.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Читайте также:  Linux локальный прокси сервер

Источник

Find and Replace Inside a Text File from a Bash Command

What’s the simplest way to do a find and replace for a given input string, say abc , and replace with another string, say XYZ in file /tmp/file.txt ? I am writting an app and using IronPython to execute commands through SSH — but I don’t know Unix that well and don’t know what to look for. I have heard that Bash, apart from being a command line interface, can be a very powerful scripting language. So, if this is true, I assume you can perform actions like these. Can I do it with bash, and what’s the simplest (one line) script to achieve my goal?

18 Answers 18

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 "$" done < /tmp/file.txt >/tmp/file.txt.t mv /tmp/file.txt

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)

Except that invoking mv is pretty much as ‘non Bash’ as using sed. I nearly said the same of echo, but it’s a shell builtin.

The -i argument for sed doesn’t exist for Solaris (and I would think some other implementations) however, so keep that in mind. Just spent several minutes figuring that out.

Note for Mac users who get an invalid command code C error. For in-place replacements, BSD sed requires a file extension after the -i flag because it saves a backup file with the given extension. For example: sed -i ‘.bak’ ‘s/find/replace/’ /file.txt You can skip the backup by using an empty string like so: sed -i » ‘s/find/replace/’ /file.txt

File manipulation isn’t normally done by Bash, but by programs invoked by Bash, e.g.:

perl -pi -e 's/abc/XYZ/g' /tmp/file.txt 

The -i flag tells it to do an in-place replacement.

See man perlrun for more details, including how to take a backup of the original file.

The purist in me says you can’t be sure Perl will be available on the system. But that’s very seldom the case nowadays. Perhaps I’m showing my age.

Can you show a more complex example. Something like replacing «chdir /blah» with «chdir /blah2». I tried perl -pi -e ‘s/chdir (?:\\/[\\w\\.\\-]+)+/chdir blah/g’ text , but I keep getting an error with Having no space between pattern and following word is deprecated at -e line 1. Unmatched ( in regex; marked by

This is an old post but for anyone wanting to use variables as @centurian said the single quotes mean nothing will be expanded.

A simple way to get variables in is to do string concatenation since this is done by juxtaposition in bash the following should work:

sed -i -e "s/$var1/$var2/g" /tmp/file.txt 

Problem solved, well, . $li comes from a file line, so there is e.g. \n and the error is there. So either awk or another language like python comes.

Читайте также:  Bind linux to ldap

I was surprised when I stumbled over this.

There is a replace command which ships with the «mysql-server» package, so if you have installed it try it out:

# replace string abc to XYZ in files replace "abc" "XYZ" -- file.txt file2.txt file3.txt # or pipe an echo to replace echo "abcdef" |replace "abc" "XYZ" 

See man replace for more on this.

Two things are possible here: a) replace is a useful independent tool and the MySQL folks should release it separately and depend on it b) replace requires some bit of MySQL o_O Either way, installing mysql-server to get replace would be the wrong thing to do 🙂

That’s because you don’t have mysql-server package installed. As pointed by @rayro, replace is part of it.

Be careful not to run the REPLACE command on Windows! On Windows the REPLACE command is for a fast replication of files. Not relevant to this discussion.

Bash, like other shells, is just a tool for coordinating other commands. Typically you would try to use standard UNIX commands, but you can of course use Bash to invoke anything, including your own compiled programs, other shell scripts, Python and Perl scripts etc.

In this case, there are a couple of ways to do it.

If you want to read a file, and write it to another file, doing search/replace as you go, use sed:

If you want to edit the file in place (as if opening the file in an editor, editing it, then saving it) supply instructions to the line editor ‘ex’

 echo "%s/abc/XYZ/g w q " | ex file 

Example is like vi without the fullscreen mode. You can give it the same commands you would at vi ‘s : prompt.

Not as far as I know. for f in report*.txt; do echo «%s/abc/XYZ/g \n w \n q \n» | ex file; done is clean and simple. Why put functionality into ex that the shell already has?

I found this thread among others and I agree it contains the most complete answers so I’m adding mine too:

    sed and ed are so useful. by hand. Look at this code from @Johnny:

sed -i -e 's/abc/XYZ/g' /tmp/file.txt 
x='abc' y='XYZ' sed -i -e 's/$x/$y/g' /tmp/file.txt #or, sed -i -e "s/$x/$y/g" /tmp/file.txt 

but, what can we do? As, @Johnny said use a while read. but, unfortunately that’s not the end of the story. The following worked well with me:

#edit user's virtual domain result= #if nullglob is set then, unset it temporarily is_nullglob=$( shopt -s | egrep -i '*nullglob' ) if [[ is_nullglob ]]; then shopt -u nullglob fi while IFS= read -r line; do line="$'/$server>" line="$'/$alias>" line="$'/$user>" line="$'/$group>" result="$result""$line"'\n' done < $tmp echo -e $result >$tmp #if nullglob was set then, re-enable it if [[ is_nullglob ]]; then shopt -s nullglob fi #move user's virtual domain to Apache 2 domain directory . 

The most suitable solution I can think of with the given assumptions of the problem.

Источник

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