How to Replace String in File (Shell Script)
Obviously, when working on scripts, you may need to find and replace some strings in text files. This may be due to various reasons; in this article, you are going to learn how to use the "sed" command to find and replace strings in shell scripts.
Here you will find out:
- what the sed is and how it can be used
- how you can use sed in a shell script to replace string in file
- how you can find a string recursively
- when DiskInternals can help you
Are you ready? Let's read!
What can sed be used for?
Sed stands for “Stream EDitor”. It is a command-line utility used to find and replace strings, words, and lines in shell scripts. Also, sed allows you to use a regex if needed. The sed command reads a given file and modifies the file following a set of commands. The modified file can be saved as a new, renamed file. Also, sed changes can be done "in place." Mostly, sed is used when trying to replace the placeholders in a config script.
Using sed to search and replace a string
Here’s the general form for finding and replacing text with the sed command:
A practical example:
sed = Stream EDitor.
-i = this instructs sed to save the files in place (i.e., save the changes back to the original file). If you want the changes saved to a new (backup) file, then you should add an extension (e.g., –i.bak).
s = the substitute command.
original (search_regex) = the regular expression that sed should search for; this identifies the word to replace.
New (replacement) = the replacement text.
g = global replacement flag; when this is provided, sed will replace all occurrences of the "regular expression." If this is not provided, sed will change the first occurrence.
file.txt (inputfile) = the file name.
The slashes (/ / /) are the “Delimiter characters”.
Examples of using the sed command
Using the file “sample.txt”, which contains the code below, let’s see some examples of sed commands.
Notice that there’s no global flag (g)? So, sed changed the first occurrence.
Also, note that sed is careful with capital letters and small letters.
Example 2: using the global flag
Notice that it also changed the "foo" in the last line "foobar"? If you don't want sed to change combined words, such as changing “foobar” to “foodbar”, then use the word-boundary expression \b. Thus, you have:
Example 3: change all strings irrespective of capitalization
Add a capital “I” flag after the “g” flag:
How to find a string recursively and replace it
Let’s say you want to find and replace a particular string on many files at the same time; this can be done using the grep or find commands.
Using grep to recursively find strings in many files:
This will look for all the files containing the identified strings and pipe the filenames to sed.
-print0 is used to avoid issues of some filenames that contain spaces. –print0 will print the filename, while xargs -0 will pipe the output to sed.
How to open Linux files from Windows
Opening a Linux file on Windows is basically not possible; however, you can do it using software like DiskInternals Linux Reader. Linux Reader is an intelligent software tool that searches a Windows PC to discover and display all the Linux partitions on that PC. It has an intuitive and features an easy-to-use interface. This software is available for free and works with no algorithm.