Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Use sed to Find and Replace String in Files - DesignLinux

Dec 18 2020
designlinux 0 Comments
How to Use sed to Find and Replace String in Files

When working with text files, you’ll often need to find and replace strings of text in one or more files.

The sed is a stream editor. It is used to search, find and replace, insert and delete words and lines. You also can use the regular expressions. In this article we will show you how to find replace strings with sed.

Find and Replace String with sed#

By default most Linux distributions comes with GNU sed pre-installed. In macOS have the BSD version of the sed.

Following the common syntax for searching and replacing text using sed:

sed -i 's/SEARCH_REGEX/REPLACEMENT/g' INPUTFILE

Here,

  • -i – By default, sed shows output to the standard output. Using this option you sed will edit the files in place.
  • s – It’s substitute command and mostly used with sed command.
  • / / / – The delimiter character. It can be any character but usually the slash (/) character is used.
  • SEARCH_REGEX – Need to pass regular expression or normal string to search for.
  • REPLACEMENT – The string which want to replace with.
  • g – Indicates global replacement flag. The sed reads the files line by line and make changes for first occurrence of search keyword in a line. Using replacement flag it will replace for all occurrences.
  • INPUTFILE – File name for which you want to run the command.

It’s recommended to enclose the argument with quotes so shell meta-characters won’t expand.

Let’s see an example, there is a file named file1.txt:

7002 Kunj kunj kunj
kunj /bin/bash Ubuntu kunjtns 789

If the g flag is not used, it will replace the first instance of the search string in each line:

sed -i 's/kunj/tecnstuff/' file1.txt
7002 Kunj tecnstuff kunj
tecnstuff /bin/bash Ubuntu kunjtns 789

Using the global replacement flag sed command will replace all occurrences of the search pattern:

sed -i 's/kunj/tecnstuff/g' file1.txt
7002 Kunj tecnstuff tecnstuff
tecnstuff /bin/bash Ubuntu tecnstufftns 789

In above output you can see that the kunj inside the kuntns string is also replaced. To prevent such behavior, you should use the word-boundary expression (\b) at beginning and end of the search string.

sed -i 's/\bkunj\b/tecnstuff/g' file1.txt
7002 Kunj tecnstuff tecnstuff
tecnstuff /bin/bash Ubuntu kunjtns 789

Use I flag, to make the pattern match case insensitive. You would use the command as following:

sed -i 's/kunj/tecnstuff/gI' file1.txt
7002 tecnstuff tecnstuff tecnstuff
tecnstuff /bin/bash Ubuntu tecnstufftns 789

If your search or replacement string contains delimiter character (/) then you need to use the backslash (\) to escape the slash. For instance, to replace /bin/bash with /usr/bin/zsh you would run:

sed -i 's/\/bin\/bash/\/usr\/bin\/zsh/g' file1.txt

You can make it easy and more readable by using the other delimiter character such as pipe or colon or any other:

sed -i 's|/bin/bash|/usr/bin/zsh|g' file1.txt
7002 Kunj kunj kunj
kunj /usr/bin/zsh Ubuntu kunjtns 789

Use of regular expression is a short and easy way to search and replacement. For example, to search 4 digit number and replace with the string replnumber, you would run like below:

sed -i 's/\b[0-9]\{4\}\b/replnumber/g' file1.txt
replnumber Kunj kunj kunj
kunj /bin/bash Ubuntu kunjtns 789

Recursive Find and Replace#

Sometimes you have requirement to check each files of directories to find searched string and replace in all files. You can do this using the find or grep commands to recursively find files in directory.

For example, below command will recursively search for files in current working directory and pass the file name to sed.

find . -type f -exec sed -i 's/kunj/tecnstuff/g' {} +

Use the -print0 option to prevent the issue of space in file names. It will convert the space to null character and pipe the output to sed using xargs -0 :

If you want to exclude any directory you can do using -not -path option. For instance, you replacing a string in your Laravel project directory and want to exclude all files starting with dot (.), you would use:

find . -type f -not -path '*/\.*' -print0 | xargs -0 sed -i 's/kunj/tecnstuff/g'

To search and replace text only on files with a specific extension, you will use:

find . -type f -name "*.css" -print0 | xargs -0 sed -i 's/kunj/tecnstuff/g'

Alternatively, you can use the grep command to recursively find all files containing the search pattern and then pipe the filenames to sed:

grep -rlZ 'kunj' . | xargs -0 sed -i.bak 's/kunj/tecnstuff/g'

Conclusion#

I hope you learned successfully how to search and replace text in files using sed.

If you have any questions or feedback, feel free to leave a comment.

Related

Tags: sed, terminal

How To Create a New User and Grant Permissions in MySQL

Prev Post

What IP – A Network Information Tool for Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 77
  • 605,827

DesignLinux.com © All rights reserved

Go to mobile version