Working principle: The sed command will read the currently processed row and place it in the "pattern space" for processing. After processing, the result will be output and the "pattern space" will be cleared. Next, read the next line and place it in the "pattern space" for processing, and so on, until the last line. Some documents also mention a term called "hold space" (also known as "temporary-storage space"), which can temporarily store some processed data and output it through "pattern space".
"pattern space" and "hold space": An area of memory where data is processed and stored.
Output text lines that will only be processed by the sed command to the screen
-e
Apply multiple sed operation commands to the input text line data
-f
Call and execute sed script command file
-i
Modify the original file
-r
Regular expression
Operation command (sometimes called operation instruction)
description
s/regexp/replacement/
Replacement string
p
Print the current "pattern space". Often used with the -n option, for example: cat -n /etc/services \| sed -n '3,5p'
d
Delete "pattern space". Start next cycle
D
Delete the first line of the "pattern space" and start next cycle
=
Print Line Number
a \text
Add one or more lines of content after the matching line. When adding multiple lines, all lines except the last line need to use "\" to indicate that the content has not ended
i \text
Add one or more lines of content before the matching line. When adding multiple lines, all lines except the last line need to use "\" to indicate that the content is not ended
c \text
Replace matching lines with new text
q
Immediately exit the sed script
r
Append text read from file
: label
Label for b and t commands
b label
Branch to label; if label is omitted, branch to end of script
t label
If "s///" is a successful replacement, then jump to the label
h H
Copy/append "pattern space" to "hold space"
g G
Copy/append "hold space" to "pattern space"
x
Exchange the contents of the hold and pattern spaces
l
List out the current line in a "visually unambiguous" form
n N
Read/append the next line of input into the "pattern space"
w FILENAME
Write the current pattern space to FILENAME
!
negation
&
Referencing a string that already matches
Addresses
description
first~step
Use "first" to specify the first line, and 'step' to specify the step size. For example, outputting odd lines of text with sed -n "1~2p" /etc/services
$
Match the last line of text
/regexp/
Using regular expressions to match text lines
number
Specify line number
addr1,addr2
Use line number positioning to match all lines from "addr1" to "addr2"
addr1,+N
Use line number positioning to match addr1 and the N lines following addr1
Shell>cat/etc/services|sed-n'/^netbios/p'
netbios-ns137/tcp# NETBIOS Name Service
netbios-ns137/udp
netbios-dgm138/tcp# NETBIOS Datagram Service
netbios-dgm138/udp
netbios-ssn139/tcp# NETBIOS session service
netbios-ssn139/udp
Tip
As we all know, double and single quotation marks in a shell play different roles. The $, `, and \ in double quotes have a special meaning. The recommendation is to use single quotes more often when using the sed command.
Print the text from lines 23 to 26
Shell>cat-n/etc/services|sed-n'23,26p'23tcpmux1/tcp# TCP port service multiplexer24tcpmux1/udp# TCP port service multiplexer25rje5/tcp# Remote Job Entry26rje5/udp# Remote Job Entry
Print odd lines
Shell>cat-n/etc/services|sed-n'1~2p'1# /etc/services:3#5# IANA services version: last updated 2016-07-087# Note that it is presently the policy of IANA to assign a single well-known9# even if the protocol doesn't support UDP operations.11# are included, only the more common ones.13# The latest IANA port assignments can be gotten from15# The Well Known Ports are those from 0 through 1023.17# The Dynamic and/or Private Ports are those from 49152 through 6553519# Each line describes one service, and is of the form:
...
Print line 10 to the last line
Shell>cat-n/etc/services|sed-n'10,$p'10# Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports11# are included, only the more common ones.12#13# The latest IANA port assignments can be gotten from14# http://www.iana.org/assignments/port-numbers15# The Well Known Ports are those from 0 through 1023.16# The Registered Ports are those from 1024 through 4915117# The Dynamic and/or Private Ports are those from 49152 through 65535
...
Lines 10 to the last do not print
Shell>cat-n/etc/services|sed-n'10,$!p'1# /etc/services:2# $Id: services,v 1.49 2017/08/18 12:43:23 ovasik Exp $3#4# Network services, Internet style5# IANA services version: last updated 2016-07-086#7# Note that it is presently the policy of IANA to assign a single well-known8# port number for both TCP and UDP; hence, most entries here have two entries9# even if the protocol doesn't support UDP operations.
Print the line number and content of the matched string
Shell>cat/etc/services|sed-n'/^netbios/,/^imap/p'
netbios-ns137/tcp# NETBIOS Name Service
netbios-ns137/udp
netbios-dgm138/tcp# NETBIOS Datagram Service
netbios-dgm138/udp
netbios-ssn139/tcp# NETBIOS session service
netbios-ssn139/udp
imap143/tcpimap2# Interim Mail Access Proto v2
Info
Start of range: Match the line where the string is located, only matching the first string that appears.
End of range: Match the line where the string is located, only matching the first string that appears.
Shell>grep-n^netbios/etc/services
123:netbios-ns137/tcp# NETBIOS Name Service124:netbios-ns137/udp
125:netbios-dgm138/tcp# NETBIOS Datagram Service126:netbios-dgm138/udp
127:netbios-ssn139/tcp# NETBIOS session service128:netbios-ssn139/udp
Shell>grep-n^imap/etc/services
129:imap143/tcpimap2# Interim Mail Access Proto v2130:imap143/udpimap2
168:imap3220/tcp# Interactive Mail Access169:imap3220/udp# Protocol v3260:imaps993/tcp# IMAP over SSL261:imaps993/udp# IMAP over SSL
In other words, the content printed above is lines 123 to 129
Print the line where the string is located and until the last line
Shell>cat/etc/services|sed-n'/^netbios/,$p'
Using variables in bash scripts
Shell>vimtest1.sh
#!/bin/basha=10
sed-n''${a}',$!p'/etc/services
# or
sed-n"${a},\$!p"/etc/services
It is similar to printing, except that the operation command is replaced with d and the -n option is not required.
Delete all lines that match the udp string, and delete all comment lines, and delete all Blank line
Shell>sed-e'/udp/d'-e'/^#/d'-e'/^$/d'/etc/services
tcpmux1/tcp# TCP port service multiplexer
rje5/tcp# Remote Job Entryecho7/tcp
discard9/tcpsinknull
systat11/tcpusers
daytime13/tcp
qotd17/tcpquote
chargen19/tcpttytstsource
ftp-data20/tcp
ftp21/tcp
ssh22/tcp# The Secure Shell (SSH) Protocol
telnet23/tcp
...
Delete consecutive lines of text
Shell>cat-n/etc/services|sed'10,$d'1# /etc/services:2# $Id: services,v 1.49 2017/08/18 12:43:23 ovasik Exp $3#4# Network services, Internet style5# IANA services version: last updated 2016-07-086#7# Note that it is presently the policy of IANA to assign a single well-known8# port number for both TCP and UDP; hence, most entries here have two entries9# even if the protocol doesn't support UDP operations.
Regular expression
Shell>cat/etc/services|sed-r'/(tcp)|(udp)|(^#)|(^$)/d'
http80/sctp# HyperText Transfer Protocol
bgp179/sctp
https443/sctp# http protocol over TLS/SSL
h323hostcall1720/sctp# H.323 Call Control
nfs2049/sctpnfsdshilp# Network File System
rtmp1/ddp# Routing Table Maintenance Protocol
nbp2/ddp# Name Binding Protocolecho4/ddp# AppleTalk Echo Protocol
zip6/ddp# Zone Information Protocol
discard9/sctp# Discard
discard9/dccp# Discard SC:DISC
...
Replace strings(s///g)
Syntax
Syntax description
sed 's/string/replace/g' FILENAME
s: All lines representing the content of the file. You can also specify the range of lines, for example: sed '20,200s/netbios/TMP/g' /etc/services g (Global): If there is no g, This means that when multiple matching strings appear on a single line, only the first matching string will be replaced. /: Delimiter style. You can also specify other styles, for example: sed '20,200s?netbios?TMP?g' /etc/services
Tip
Example in the bash script:
Shell>vim/root/sedReplace.sh
#!/bin/basha="SELINUX=enforcing"b="SELINUX=disabled"
sed-i's/'${a}'/'${b}'/g'/etc/selinux/config
# or
sed-i"s/${a}/${b}/g"/etc/selinux/config
Use a string to locate one or more lines and replace the specified string within the line range
Shell>grepssh/etc/services-n
44:ssh22/tcp# The Secure Shell (SSH) Protocol45:ssh22/udp# The Secure Shell (SSH) Protocol551:x11-ssh-offset6010/tcp# SSH X11 forwarding offset593:ssh22/sctp# SSH1351:sshell614/tcp# SSLshell1352:sshell614/udp# SSLshell1607:netconf-ssh830/tcp# NETCONF over SSH1608:netconf-ssh830/udp# NETCONF over SSH7178:sdo-ssh3897/tcp# Simple Distributed Objects over SSH7179:sdo-ssh3897/udp# Simple Distributed Objects over SSH7791:netconf-ch-ssh4334/tcp# NETCONF Call Home (SSH)8473:snmpssh5161/tcp# SNMP over SSH Transport Model8474:snmpssh-trap5162/tcp# SNMP Notification over SSH Transport Model9126:tl1-ssh6252/tcp# TL1 over SSH9127:tl1-ssh6252/udp# TL1 over SSH10796:ssh-mgmt17235/tcp# SSH Tectia Manager10797:ssh-mgmt17235/udp# SSH Tectia Manager
Shell>sed'/ssh/s/tcp/TCP/gp'-n/etc/services
ssh22/TCP# The Secure Shell (SSH) Protocol
x11-ssh-offset6010/TCP# SSH X11 forwarding offset
sshell614/TCP# SSLshell
netconf-ssh830/TCP# NETCONF over SSH
sdo-ssh3897/TCP# Simple Distributed Objects over SSH
netconf-ch-ssh4334/TCP# NETCONF Call Home (SSH)
snmpssh5161/TCP# SNMP over SSH Transport Model
snmpssh-trap5162/TCP# SNMP Notification over SSH Transport Model
tl1-ssh6252/TCP# TL1 over SSH
ssh-mgmt17235/TCP# SSH Tectia Manager
Shell>cat-n/root/test.txt|sed-n'{p;n}'# or
Shell>cat-n/root/test.txt|sed-n'1~2p'# or
Shell>cat-n/root/test.txt|sed'n;d'1aigairserver21221/tcp# Services for Air Server3ka-sddp31016/tcp# Kollective Agent Secure Distributed Delivery5axio-disc35100/tcp# Axiomatic discovery protocol7pmwebapi44323/tcp# Performance Co-Pilot client HTTP API9cloudcheck45514/tcp# ASSIA CloudCheck WiFi Management System
Print 3n lines
Shell>cat-n/root/test.txt|sed-n'{n;n;p}'# or
Shell>cat-n/root/test.txt|sed-n'3~3p'3ka-sddp31016/tcp# Kollective Agent Secure Distributed Delivery6axio-disc35100/udp# Axiomatic discovery protocol9cloudcheck45514/tcp# ASSIA CloudCheck WiFi Management System
N
Read the first line and append one line after encountering the N command. In this example, the "pattern space" is "1\n2". Finally, execute the q command to exit.
Shell>seq110|sed'N;q'12
Because there is no additional line after line 9, the output is as follows:
Shell>seq19|sed-n'N;p'12345678
When the last line is read, the N command is not executed and the output is as follows:
Shell>seq19|sed-n'$!N;p'123456789
Merge two lines into one line. Replace the "\n" of the "pattern space" with a blank character.
Shell>seq16|sed'N;{s/\n//g}'123456
Ignore case (I)
There seems to be no information about ignoring case in man 1 sed.
Shell>echo-e"abc\nAbc"|sed-n's/a/X/Igp'
Xbc
XBC
Shell>cat/etc/services|sed'/OEM/Ip'-n
oem-agent3872/tcp# OEM Agent
oem-agent3872/udp# OEM Agent
oemcacao-jmxmp11172/tcp# OEM cacao JMX-remoting access point
oemcacao-rmi11174/tcp# OEM cacao rmi registry access point
oemcacao-websvc11175/tcp# OEM cacao web service access point
Shell>cat/etc/services|sed-r'/(TCP)|(UDP)/Id'
Shell>cat/etc/services|sed-r'/(TCP)|(UDP)/Ic TMP'
Gets the total number of lines in a file
Shell>cat/etc/services|sed-n'$='# or
Shell>cat/etc/services|wc-l
11473