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:
Explanation:
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.
Example 1:
Sed command:
Output:
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
Output:
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:
Output:
Example 3: change all strings irrespective of capitalization
Add a capital “I” flag after the “g” flag:
Output:
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.
Using find:
-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 works with different file systems (Ext2/3/4, ReiserFS, Reiser4, HFS, HFS+, FAT, exFAT, NTFS, ReFS, UFS2, RomFS(reader), ZFS (preview only*), XFS (preview only*), Hikvision NAS and DVR (preview only*)). It has an intuitive and features an easy-to-use interface. This software is available for free.
Related articles
- How to Access Linux Ext2 or Ext3 on Windows
- Mount Linux Drive on Windows for Free
- Guide in Pictures
- A Bash Dirname Command
- Learn about Korn shell scripting
- Bash: how to split strings in Linux
- Here is everything you need to know about Bash for Windows
- How to Use Shell Script Sleep Command
- Linux: Sudo in Bash Scripts
- Use a Shell Script to Send an Email
- Learn about useful bash scripts
- Learn about systemd startup script
- Using /usr/bin/env command
- The disk you inserted was not readable by this computer error
- Shell script usage
- About a bash date command
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- A crontab service shell script
- A Bash Multiline Command
- Bash and sh: is there any difference
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Install Oracle Database
- AWK in a Bash Script
- How to Use Linux Wait Command




