Contents
Introduction:
The sed command, a powerful stream editor in Linux/Unix, is a cornerstone for text manipulation. This guide will delve into the intricacies of using sed to search and replace strings within files. We’ll explore various scenarios, from replacing single occurrences to global substitutions, and even handling case-insensitive replacements. Whether you’re a seasoned system administrator or a budding developer, this comprehensive tutorial will equip you with the knowledge to effectively wield the sed command for your text processing needs. We will discuss more about how to Search and Replace String Using the sed.
My Requirement & solution:
We are maintaining the application in Linux machine (in AKS pods) and as a Devops team, we Got a requirement to replace some config values based on the environment (value need to be maintain in the AKS environment variable). To manage this, we thought to create one startup script in the docker image which will execute during the new image deployment ,where we used the sed command to achieve the find & replace of config value based on environments. Based on my experience i though to write this article (Search and Replace String Using the sed Command in Linux/Unix) immediately which will be helpful like me who are new to the Linux Operating system/Bash commands.
What Is the Sed Command in Linux?
The SED command in Linux stands for Stream Editor and it helps in operations like selecting the text, substituting text, modifying an original file, adding lines to text, or deleting lines from the text. Though most common use of SED command in UNIX is for substitution or for find and replace.
By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.
Syntax: sed OPTIONS… [SCRIPT] [INPUTFILE…]
- Options control the output of the Linux command.
- Script contains a list of Linux commands to run.
- File name (with extension) represents the file on which you’re using the sed command.
Key Advantages of Using sed
- Efficiency: sed allows for in-place editing, eliminating the need to manually open and modify files in a text editor.
- Flexibility: It supports a wide array of editing commands, enabling complex text manipulations.
- Automation: sed can be easily integrated into scripts for automated text processing tasks.
Search and Replace String Using the sed
Replace First Matched String
The below example, the script will replace the first found instance of the word test1 with test2 in every line of a file
1 2 3 |
sed -i 's/test1/test2/' opt/example.txt |
The command replaces the first instance of test1 with test2 in every line, including substrings. The match is exact, ignoring capitalization variations. -i tells the sed command to write the results to a file instead of standard output.
Search & Global Replacement (all the matches)
To replace every string match in a file, add the g flag to the script (To replace all occurrences of a pattern within each line). For example
1 2 3 |
sed -i 's/test1/test2/g' opt/example.txt |
The command globally replaces every instance of test1 with test2 in the /example.txt.
The command consists of the following:
- -i tells the sed command to write the results to a file instead of standard output.
- s indicates the substitute command.
- / is the most common delimiter character. The command also accepts other characters as delimiters, which is useful when the string contains forward slashes.
- g is the global replacement flag, which replaces all occurrences of a string instead of just the first.
“input file” is the file where the search and replace happens. The single quotes help avoid meta-character expansion in the shell.
Search and Replace All Cases
To find and replace all instances of a word and ignore capitalization, use the I parameter:
1 2 3 4 |
#I: The case-insensitive flag. sed -i 's/test1/tes2/gI' opt/example.txt |
The command replaces all instances of the word test1 with test2, ignoring capitalization.
Conclusion
The sed command is an invaluable tool for text manipulation in Linux/Unix environments. By mastering its basic usage and exploring its advanced features, you can streamline your text processing tasks and significantly improve your system administration and development workflows. This tutorial has provided a foundational understanding of sed’s search and replace capabilities. For further exploration, consider delving into more advanced sed scripting techniques and exploring its other powerful features.
I hope you found this tutorial helpful. What’s your favorite thing you learned from this tutorial? Let me know on comments!
Leave A Comment