Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»How to Use the Sed Command to Replace String in Files

    How to Use the Sed Command to Replace String in Files

    RahulBy RahulMay 17, 20133 Mins ReadUpdated:July 30, 2022

    Do you ever have a situation where you need to replace a word or sentence in many files? You could copy and paste it, or perhaps there’s an existing script that can do the job. But how about if you just need to replace it with another word or sentence somewhere else? You could use find, but that would be too generic and require writing a script for this task. And what if you don’t even know what word or sentence you must find and replace? How about using sed then? If you are not familiar with the Unix command line, sed is a stream editor. It’s a simple text editor that reads from standard input and performs editing operations on the stream as it is read. It is commonly used for search and replace functionality in scripts.

    In this article, we will show you some examples of how to use sed to perform search and replaces text in files.

    Find and Replace String in Files

    If you just need to perform a simple search and replace, you can use the following syntax:

    sed -i 's/string_to_find/string_to_replace/' file1 file2 fileN 
    

    Here:

    • -i: This flag indicates to sed that your input file is expected to be one of the files specified after the command.
    • string_to_find: This is the string that you want to search for in the files.
    • string_to_replace: This is the string that you want to replace the string what_to_find within the files.
    • file1 file2 fileN: This is the list of files that sed will search and replace.

    The following command will search for all strings “Hello” in the welcome.txt file. Then replace it with a new string “Howdy” in the same file.

    sed -i 's/Hello/Howdy/g' welcome.txt 
    

    You can also create a backup of original file before making changes with -i followed by some extension. For example:

    sed -i.backup 's/Hello/Howdy/g' welcome.txt 
    

    This will create a file welcome.txt.backup in the current directory with the original file content.

    Recursively Replace Text in Multiple files

    The Following command is helpful to replace strings in multiple files at once in a directory /opt/docs. Here find will navigate to all files under the defined directory and execute the sed command one by one.

    find /opt/docs/ -type f -exec sed -i 's/Hello/Howdy/g' {} ; 
    

    Replace Text Without Affecting Original File

    If you do not want to make changes in your original file and create new files with changed strings, use the following command.

    sed 's/Hello/Howdy/g' welcome.txt > welcome_2.txt 
    

    This command will create a new file welcome_2.txt with modified text. The original file with remain unchanged.

    Conclusion

    A sed command is a powerful tool that can be used to edit the text in files. It’s useful when you have a situation where you need to find and replace a string in many files. You could use find, but that would be too generic and require writing a script. And what if you don’t even know what word or sentence you must find and replace? Using sed is a better solution.

    Linux sed sed sed command
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install phpPgAdmin on CentOS & RHEL using Yum
    Next Article Watch ASCII Star Wars in Command Prompt

    Related Posts

    Running Multiple Commands At Once in Linux

    Updated:August 6, 20223 Mins Read

    Convert String to Lowercase in Bash – Easier Than You Think

    Updated:August 1, 20221 Min Read

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    tee Command in Linux with Examples

    Updated:July 1, 20224 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    1 Comment

    1. sunny on October 26, 2017 10:21 am

      Hi,

      Above command Not Working
      find /opt/docs/ -type f -exec sed -i ‘s/Hello/Howdy/g’ {} ;
      getting the below error
      find: missing argument to `-exec’

      solution:
      A -exec command must be terminated with a ; (so you usually need to type \;

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Import GPG Keys on Ubuntu & Debian (without apt-key)
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.