To replace a string, we need to use the following format. $ sed s/replacement target string/replacement string/file name In the following we replace the string "sample.txt" written as "appleorangemelon". $ sed s/orange/ORANGE/ sample.txt The execution result is appleORANGEmelon Replace and output a string. In addition, as shown below, you can also get the same result by connecting the sed command with "|" after the cat command. $ cat sample.txt | sed s/apple/APPLE/ Note that the sed command only replaces the string and outputs it, but does not rewrite the actual file contents. If you want to keep the replaced content in text, use the redirection ">". Options used by sed command
Use of sed command Replace all lines In the format described previously, even if there are strings matching multiple replacement target strings in one line, only the first matching string is replaced. So, to replace all matching strings, do: $ sed -es /apple/APPLE/g sample.txt The execution result is APPLEorangemelonAPPLE Replace the beginning and end of a line $ sed -e "s/^apple/APPLE/" sample.txt $ sed -e "s/apple\$/APPLE/" sample.txt If you want to replace multiple substrings, you can specify multiple scripts. $ sed -e "s/apple/APPLE/" -e "s/orange/ORANGE/" sample.txt Deleting a row Specify "d" to delete the specified line. For example, to delete the second line, enter "2d". $ sed -e '2d' sample.txt In addition, you can also delete multiple lines. The following is to delete lines 1 to 3. $ sed -e '1,3d' sample2.txt You may also be interested in:
|
<<: Detailed explanation of MySql automatic truncation example
>>: React event mechanism source code analysis
http return code list (below is an overview) for ...
Questions about select elements in HTML have been...
Table of contents Three steps to operate the data...
This article shares the specific code of Javascri...
Add monitoring host Host 192.168.179.104 is added...
What is React React is a simple javascript UI lib...
Does color influence website visitors? A few year...
When we check the source code of many websites, w...
Table of contents 1. Rename table method 2. Notes...
Template 1: login.vue <template> <p clas...
Table of contents 1. Import files using script ta...
1. Refine the selector By using combinators, the ...
This article shares the specific code of the pull...
In the Linux system, there is a kind of file call...
Table of contents Overview computed watch monitor...