Sed Tutorial: Advanced Sed Substitution Examples
In this article, let's review some interesting workarounds using the "s" substitution command in sed with a few real-world examples.
1. sed replaces the delimiter
As we discussed in our previous article, we can use different delimiters in sed substitution command, @ % | ; :
e.g.
Let us first create the path.txt file which will be used in all the examples mentioned below.
path.txt
/usr/kbos/bin:/usr/local/bin:/usr/jbin:/usr/bin:/usr/sas/bin /usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin: /opt/omni/lbin:/opt/omni/sbin:/root/bin
Example 1 – sed @ delimiter: Replace /opt/omni/lbin with /opt/tools/bin
When we replace /
a path name that contains , we can use @
as a delimiter instead of /
. In the following sed example, in the last line of the input file, **/opt/omni/lbin** has been changed to /opt/tools/bin .
$ sed 's@/opt/omni/lbin@/opt/tools/bin@g' path.txt
Example 2 – sed / delimiter: Replace /opt/omni/lbin with /opt/tools/bin
When we should use in pathname related replacement /
, we have to escape in replacement data /
as shown below. In this sed example, the delimiter is escaped /
in both REGEXP and REPLACEMENT parts.
$ sed 's/\/opt\/omni\/lbin/\/opt\/tools\/bin/g' path.txt
2. sed &
gets the matching string
The exact part of the input line that the regular expression matched is &
represented by and can then be used in the replacement part.
Example 1 – sed & Usage: Replace /usr/bin/ with /usr/bin/local
In the following example, in the replacement part, /usr/bin&
matching the pattern will be replaced and added to /local . So in the output all occurrences of /usr/bin will be replaced with /usr/bin/local .
$ sed 's@/usr/bin@&/local@g' path.txt
Example 2 – sed & usage: matching the entire line
&
Replace any matches with the given REGEXP .
$ sed 's@^.*$@<<<&>>>@g' path.txt
In the above example, the regular expression ^.*$
matches the entire line. The replacement part is written in the entire line using and <<<&>>>
at the beginning and end of the line respectively .<<<
>>>
3. Grouping and backreferences in Sed
Groups can be used in sed just like normal regular expressions. Groups \(
are turned on with , and \)
off with . Groups can be combined with backreferences.
A backreference is a reuse of a portion of a regular expression selected through grouping. Backreferences in sed can be used in both the regular expression and the replacement part of the substitute command.
Example 1: Get only the first path in each line
In the following example, the path available before \(\/[^:]*\)
the first colon is matched . The first matched group is replaced.:
\1
$ sed 's/\(\/[^:]*\).*/\1/g' path.txt
Example 2: Multiple Groups
In the file path.txt, change the order of the fields in the last line of the file.
$ sed '$s@\([^:]*\):\([^:]*\):\([^:]*\)@\3:\2:\1@g' path.txt
In the above command, $
you specify that replacement should be done only on the last line. The output shows that the order of the path values in the last line is reversed.
Example 3: Get a list of usernames in the /etc/passwd file
This sed example displays only the first field in the /etc/passwd file.
$ sed 's/\([^:]*\).*/\1/' /etc/passwd
Example 4: Add brackets around the first character of each word
This sed example adds brackets before the first character of each word.
$ echo "Welcome To The Jiyik.com" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
Example 5: commify a simple number.
Let us create a file called numbers which contains a list of numbers. The following sed command example is used to group the numbers up to thousands.
numbers
1234
12121
3434
123
Look at the following command
$ sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g' numbers
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Linux Sed Tutorial: 6 Examples of Sed Branching Operations
Publish Date:2025/04/06 Views:165 Category:OPERATING SYSTEM
-
Like any other programming language, sed also provides special branching commands to control the flow of the program. In this article, let's review the following two types of Sed branches. Sed unconditional branch Sed conditional branching
awk tutorial – 7 awk printing examples
Publish Date:2025/04/06 Views:118 Category:OPERATING SYSTEM
-
This is the first article in the new awk tutorial series. We will publish several articles on awk in the coming time, which will explain all the features of awk with practical examples. In this article, let's review the basic awk working me
How to install GrayLog on centos
Publish Date:2025/04/06 Views:122 Category:OPERATING SYSTEM
-
Installing Graylog on CentOS requires the following steps: 1. Add EPEL and MongoDB repositories: sudo yum install epel-release -y sudo rpm -Uvh https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.4-1.el7
export command in Linux
Publish Date:2025/04/05 Views:109 Category:OPERATING SYSTEM
-
The Linux terminal has many environment variables that contain necessary information about the system. Also, applications may require some environment variables in order to execute. export This article will use the command to define an envi
tr Command in Linux Bash
Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM
-
In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma
How to Copy Files and Directories Using Linux Terminal
Publish Date:2025/04/05 Views:122 Category:OPERATING SYSTEM
-
cp We can use the and commands in Linux Terminal rsync to copy files and directories. cp The command is generally used to copy files, while rsync the command is generally used to copy directories. Use cp the command to copy files We use com
Obtaining an IPv4 Address on Unix and Linux
Publish Date:2025/04/05 Views:200 Category:OPERATING SYSTEM
-
IP stands for Internet Protocol, which specifies the principles of Internet communications. An Internet Protocol (IP) address is a unique identifier for each device on the Internet, allowing data to be transferred between connected devices.
Changing permissions on folders and directories in Linux/Unix
Publish Date:2025/04/05 Views:79 Category:OPERATING SYSTEM
-
The file permission system is one of the fundamental security elements of the Linux operating system. These features allow us to adjust file permissions and access modes. We provide permissions based on appropriate rights to avoid vulnerabi
How to Delete a User Account in Linux
Publish Date:2025/04/05 Views:149 Category:OPERATING SYSTEM
-
In Linux, we can have multiple user accounts. Sometimes, we may need to remove some users to deny their access to the system. In such cases, we have to delete the user account. We can use userdel command line utilities to delete user accoun