{"id":4005,"date":"2021-02-03T08:42:09","date_gmt":"2021-02-03T08:42:09","guid":{"rendered":"https:\/\/linuxways.net\/?p=4005"},"modified":"2021-02-03T08:44:42","modified_gmt":"2021-02-03T08:44:42","slug":"how-to-use-sed-command-to-find-and-replace-string-in-files","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-use-sed-command-to-find-and-replace-string-in-files\/","title":{"rendered":"How to Use SED Command to Find and Replace String in Files"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Sed stands for <strong>S<\/strong>tream <strong>ED<\/strong>itor. It is a Linux command used for searching, finding and replacing, or inserting and deleting words and lines in a text file.<\/p>\n<p>The most common use case of the Sed command is to find and replace strings in files. When using the Sed command, you can edit files without opening them.<\/p>\n<p>This article will explain the detail of using the Sed command to find and replace strings in files.<\/p>\n<p><span style=\"font-weight: 400;\">All of the examples in this tutorial were executed on Ubuntu 20.04.<\/span><\/p>\n<h2>Find and Replace<\/h2>\n<p>By default, Sed is installed in most Linux distributions. You can verify the installation of Sed on your Linux system by running:<\/p>\n<pre>$ sed --v<\/pre>\n<p>The output will be something like this:<\/p>\n<p>The basic syntax of using the Sed command to find and replace text is:<\/p>\n<pre>$ sed -i 's\/Search_pattern\/Replacement_pattern\/g' FILE<\/pre>\n<p>In which:<\/p>\n<p>-i: This option makes Sed command edit files in place instead of writing output to the standard output.<\/p>\n<p>s: It stands for substitute.<\/p>\n<p>\/ \/: Delimiter characters.<\/p>\n<p>g: It is a Global replacement flag.<\/p>\n<p>FILE: The file you want to manipulate by the Sed command.<\/p>\n<p>It\u2019s highly recommended to put search pattern, replacement pattern, and other arguments in quotes.<\/p>\n<p>For example, here is a text file that used for demonstration:<\/p>\n<pre>567 Test test test<\/pre>\n<pre>test \/bin\/zsh linuxways.net testbar 789<\/pre>\n<p>To replace all of the \u2018test\u2019 words with \u2018linux\u2019 words, run the following command:<\/p>\n<pre>$ sed -i 's\/test\/linux\/g' test.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"304\" class=\"wp-image-4006\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-11.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-11-300x59.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-11-1024x203.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-11-768x152.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>If you don\u2019t use the g flag, only the first occurrence of the search pattern in each line is replaced:<\/p>\n<pre>$ sed -i 's\/test\/linux\/' test.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"234\" class=\"wp-image-4007\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-12.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-12-300x46.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-12-1024x156.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-12-768x117.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>As you can see, in the previous example, the substring test inside the testbar string is also placed. If you don\u2019t want to replace a substring in the search pattern, let\u2019s put the search pattern between two word-boundary \\b:<\/p>\n<pre>$ sed -i 's\/\\btest\\b\/linux\/g' test.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"236\" class=\"wp-image-4008\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-13.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-13-300x46.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-13-1024x157.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-13-768x118.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>To make the search pattern match case insensitive, let\u2019s use the I flag:<\/p>\n<pre>$ sed -i 's\/test\/linux\/gI' test.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"212\" class=\"wp-image-4009\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-14.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-14-300x41.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-14-1024x141.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-14-768x106.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>In the Linux world, we\u2019ll encounter many use cases that have the delimiter characters in text files. We have to use the backslash \\ to breakout the delimiter characters. For example, to replace \/bin\/zsh with \/usr\/bin\/bash, run:<\/p>\n<pre>$ sed -i 's\/\\\/bin\\\/zsh\/\\\/usr\\\/bin\\\/bash\/g' test.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"196\" class=\"wp-image-4010\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-15.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-15-300x38.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-15-1024x131.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/02\/word-image-15-768x98.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<h2>Recursive Find and Replace<\/h2>\n<p>In many cases, you want to recursively search directories for files that have a specific string and replace that string in all files.<\/p>\n<p>For example, the following command will search and replace the string \u2018way\u2019 with \u2018road\u2019 in all of the files in the current directory.<\/p>\n<pre>$ find . -type f -exec sed -i 's\/way\/road\/g' {} +<\/pre>\n<p>If the filenames contain space characters, let\u2019s use -print0 option:<\/p>\n<pre>$ find . -type f -print0 | xargs -0 sed -i 's\/way\/road\/g'<\/pre>\n<p>To search and replace a string only on files that have a specific extension, run:<\/p>\n<pre>$ find . -type f -name \"*.java\" -print0 | xargs -0 sed -i 's\/way\/road\/g'<\/pre>\n<h2>Conclusion<\/h2>\n<p>You\u2019ve gone through some common examples of using the Sed command. Searching and replacing a string of text in a file with the Sed command isn\u2019t complicated as you imagine.<\/p>\n<p>If you have any concerns, please let me know in the comment section.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction Sed stands for Stream EDitor. It is a Linux command used for searching, finding and replacing, or inserting and deleting words and lines in a text file.&hellip;<\/p>","protected":false},"author":23,"featured_media":4011,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4005"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4005\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/4011"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}