sed的应用

[root@192 ~]# cat anaconda-ks.cfg 
#version=RHEL8
# Use graphical install
graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
@container-management
@development
@dotnet-core
@graphical-admin-tools
@guest-agents
@headless-management
@legacy-unix
@network-server
@rpm-development-tools
@scientific
@security-tools
@smart-card
@standard
@system-tools
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=nvme0n1
autopart
# Partition clearing information
clearpart --none --initlabel

# System timezone
timezone Asia/Shanghai --isUtc

# Root password
rootpw --iscrypted $6$CsPp1f.R28BqxvuA$IVFJ92X54POOzH4szFJIRMbirQ71YWJk3N3ZJdq.qEe/i5flr3HEmsOBdsQwMc/uzh7uVjQGBxNHzUsetQMfM/

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@192 ~]# sed '/^graphical/d' anaconda-ks.cfg // 删除anaconda-ks.cfg中以graphical开头的
#version=RHEL8
# Use graphical install

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
@container-management
@development
@dotnet-core
@graphical-admin-tools
@guest-agents
@headless-management
@legacy-unix
@network-server
@rpm-development-tools
@scientific
@security-tools
@smart-card
@standard
@system-tools
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=nvme0n1
autopart
# Partition clearing information
clearpart --none --initlabel

# System timezone
timezone Asia/Shanghai --isUtc

# Root password
rootpw --iscrypted $6$CsPp1f.R28BqxvuA$IVFJ92X54POOzH4szFJIRMbirQ71YWJk3N3ZJdq.qEe/i5flr3HEmsOBdsQwMc/uzh7uVjQGBxNHzUsetQMfM/

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

[root@192 ~]# cat anaconda-ks.cfg -n 显示行数
     1  #version=RHEL8
     2  # Use graphical install
     3  graphical
     4  
     5  repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
     6  
     7  %packages
     8  @^minimal-environment
     9  @container-management
    10  @development
    11  @dotnet-core
    12  @graphical-admin-tools
    13  @guest-agents
    14  @headless-management
    15  @legacy-unix
    16  @network-server
    17  @rpm-development-tools
    18  @scientific
    19  @security-tools
    20  @smart-card
    21  @standard
    22  @system-tools
    23  kexec-tools
    24  
    25  %end
    26  
    27  # Keyboard layouts
    28  keyboard --xlayouts='cn'
    29  # System language
    30  lang zh_CN.UTF-8
    31  
    32  # Network information
    33  network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
    34  network  --hostname=localhost.localdomain
    35  
    36  # Use CDROM installation media
    37  cdrom
    38  
    39  # Run the Setup Agent on first boot
    40  firstboot --enable
    41  
    42  ignoredisk --only-use=nvme0n1
    43  autopart
    44  # Partition clearing information
    45  clearpart --none --initlabel
    46  
    47  # System timezone
    48  timezone Asia/Shanghai --isUtc
    49  
    50  # Root password
    51  rootpw --iscrypted $6$CsPp1f.R28BqxvuA$IVFJ92X54POOzH4szFJIRMbirQ71YWJk3N3ZJdq.qEe/i5flr3HEmsOBdsQwMc/uzh7uVjQGBxNHzUsetQMfM/
    52  
    53  %addon com_redhat_kdump --enable --reserve-mb='auto'
    54  
    55  %end
    56  
    57  %anaconda
    58  pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    59  pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
    60  pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
    61  %end
[root@192 ~]# sed '3d' anaconda-ks.cfg // 删除anaconda-ks.cfg的第四行
#version=RHEL8
# Use graphical install

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%packages
@^minimal-environment
@container-management
@development
@dotnet-core
@graphical-admin-tools
@guest-agents
@headless-management
@legacy-unix
@network-server
@rpm-development-tools
@scientific
@security-tools
@smart-card
@standard
@system-tools
kexec-tools

%end

# Keyboard layouts
keyboard --xlayouts='cn'
# System language
lang zh_CN.UTF-8

# Network information
network  --bootproto=dhcp --device=ens160 --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Use CDROM installation media
cdrom

# Run the Setup Agent on first boot
firstboot --enable

ignoredisk --only-use=nvme0n1
autopart
# Partition clearing information
clearpart --none --initlabel

# System timezone
timezone Asia/Shanghai --isUtc

# Root password
rootpw --iscrypted $6$CsPp1f.R28BqxvuA$IVFJ92X54POOzH4szFJIRMbirQ71YWJk3N3ZJdq.qEe/i5flr3HEmsOBdsQwMc/uzh7uVjQGBxNHzUsetQMfM/

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end


//  \表示为转义符 分隔符可用!和 #代替   
[root@192 ~]# echo '/usr/local/src/' | sed 's/\/usr\/local\/src/\/user\/local\/src/' 
/user/local/src/  
[root@192 ~]# echo '/usr/local/src/' | sed 's!/usr/local/src!/user\/local\/src!'
/user/local/src/
[root@192 ~]# echo '/usr/local/src/' | sed 's#/usr/local/src#/user\/local\/src#'
/user/local/src/

// \t 为tab
[root@192 ~]# cat abc 
olumn1  Column2 column3 column4
[root@192 ~]# sed 's/\t/>/2' abc // 将tab替换成> 
olumn1  Column2>column3 column4
[root@192 ~]# sed 's/\t/\
> /2' abc                        // 将tab替换成\(换行)
olumn1  Column2
column3 column4
[root@192 ~]# sed 's/\t/\n/2' abc // 将tab替换成n(换行)
olumn1  Column2
column3 column4


[root@192 ~]# sed '/^\.An/{    // 匹配开头为.An的
s/\.An */\   // 替换 .An
\           //结果前面加了换行 
@A HEAD = / //替换内容
s/"//g      // 将“替换为空
s/$/\
/
}
' 1


@A HEAD = Major Heading
[root@192 ~]# sed '/^\.An/{s/\.An */\n@A HEAD = /;s/"//g};s/$/\n/' 1

@A HEAD = Major Heading


// &(表示前面匹配到的内容) -r(表示拓展正确表达式)  s(替换) g(替换全部的)
[root@192 ~]# sed -r "s/ORA (.*)/O'Reilly &/g" 1
.An "Major Heading"
O'Reilly ORA Associates, Inc.
[root@192 ~]# sed "s/ORA.*/O'Reilly &/g" 1
.An "Major Heading"
O'Reilly ORA Associates, Inc.
[root@192 ~]# sed "s/ORA.*/& 123/g" 1
.An "Major Heading"
ORA Associates, Inc. 123
[root@192 ~]# sed "s/ORA/& 123/g" 1
.An "Major Heading"
ORA 123 Associates, Inc.

// s(替换) g(替换全部的) \(转义符) &(表示前面匹配到的内容)
[root@192 ~]# cat 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
[root@192 ~]# sed 's/UNIX/\\s-2&\\s0/g' 1 
.An "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System.

// s(替换) g(替换全部的) \(转义符) &(表示前面匹配到的内容)[1-9](表示1到9之间的任意数) *(匹配其前面的任意单个字符任意次) -r(表示拓展正确表达式) 1(表示为第一个括号里匹配到的内容)2(表示为第二个括号里匹配到的内容)
[root@192 ~]# sed 's/See Section [1-9[0-9]*\.[1-9][0-9]*/(&)/g' 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
(See Section 1.4)
(See Section 12.9)
[root@192 ~]# sed 's/[1-9[0-9]*\.[1-9][0-9]*/(&)/g' 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section (1.4)
See Section (12.9)
[root@192 ~]# sed -r 's/(See Section) ([1-9[0-9]*\.[1-9][0-9]*)/\1\\fB\2\\fP/g' 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section\fB1.4\fP
See Section\fB12.9\fP
// s(替换) g(替换全部的) -r(表示拓展正确表达式) 1(表示为第一个括号里匹配到的内容)2(表示为第二个括号里匹配到的内容).*(匹配任意单个字符任意次)
[root@192 ~]# cat 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# sed -r 's/(.*):(.*)/\2:\1/g' 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
two:one

[root@192 ~]# cat 1
.An "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# sed '/^\.An/d' 1 // 删除以.An开头的
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# sed '/^.An/d' 1 // 不加转义符表示单个字符
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# 

// 行数和a(在该行后面添加内容) 匹配所有开头为内容和a(在该行后面添加内容)[1-9](表示1到9之间的任意数) 
[root@192 ~]# sed '1atjf' 1
.An "Major Heading"
tjf
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# sed '1atjf\ tjf' 1  // 添加空格需要用\转义符
.An "Major Heading"
tjf tjf
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# sed '/^\.An/atjf' 1 
.An "Major Heading"
tjf
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
one:two
[root@192 ~]# sed '/^See/atjf' 1 
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
tjf
See Section 12.9
tjf
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '/[1-9][0-9]\.[1-9]/atjf' 1 
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
tjf
See Section 12.5
tjf
one:two


// 行数加i(在该行数前添加内容) //内容开头加i(在该行数前添加内容)
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '1itjf' 1
tjf
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '/^\.An/itjf' 1
tjf
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '/^\.An/itjf\
> tjf' 1
tjf
tjf
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two


// 行数加c(覆盖该行内容) 开头加c(覆盖该行内容)
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '1ctjf' 1
tjf
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '/^\.An/ctjf' 1
tjf
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '/^\.An/,/ORA/ctjf' 1
tjf
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed '1,$ctjf' 1
tjf
[root@192 ~]# 

// y(替换全部内容)
[root@192 ~]# cat 1
.An "Major Heading"
hAn "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
See Section 12.5
one:two
[root@192 ~]# sed 'y/ajo/123/' 1
.An "M123r He1ding"
hAn "M123r He1ding"
ORA Ass3ci1tes, Inc.
3n the UNIX Oper1ting System.
See Secti3n 1.4
See Secti3n 12.9
See Secti3n 12.5
3ne:tw3


[root@192 ~]# cat 2
.An "Comment"
.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# sed '/^\.An/{p;s/"//g;s/^\.An //}' 2  //P为打印
.An "Comment"
Comment
.An "Substitution"
Substitution
.An "Delete"
Delete
.An "Append, Insert and Change"
Append, Insert and Change
.An "List"
List
[root@192 ~]# sed -n '/Comment/{=;p}' 2 // {=;p} 显示行号
1
.An "Comment"
[root@192 ~]# cat 2
.An "Comment"
.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# sed -n '/Comment/{=;p}' 2
1
.An "Comment"
[root@192 ~]# sed -n '/Comment/n;p' 2 // 打印Comment后面的内容
.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# cat 2
.An "Comment"
.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# sed -r '/Comment/{n;s/\.An (.*)/\.An (\1)/g}' 2
.An "Comment"
.An ("Substitution")
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# 
[root@192 ~]# cat 2
.An "Comment"

.An "Comment"

.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# sed '/Comment/{n;/^$/d}' 2
.An "Comment"
.An "Comment"
.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"
[root@192 ~]# sed -n '/Comment/{n;/^$/d};p' 2
.An "Substitution"
.An "Delete"
.An "Append, Insert and Change"
.An "List"

sed高级用法

[root@localhost ~]# cat abc
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.


#改变
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' abc
Consult Section 3.1 in the installation Guide for a description of the tape drives
available on your system.

#拆解
[root@localhost ~]# sed -n '/Operator$/{N;p}' abc  //匹配以Operator结尾的行 N和匹配行的下一行 p打印
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives


[root@localhost ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g ;p}' abc   //将Owner and Operator\nGuide 变成 installation Guide   因为替换包括了\n(转行)  所以打印时会将两条显示为一条
Consult Section 3.1 in the installation Guide for a description of the tape drives  
#原文档
[root@localhost ~]# cat abc
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided inc luding the Owner and
Operator Guide and the User Gui de.
The Owner and Operator Guide is shipped with your system.

#改变
[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g}' abc
Consult Section 3.1 in the installation Guide
for a description of the tape drives
available on your system.
Look in the installation Guide shipped with your system.
Two manuals are provided inc luding the installation Guide
and the User Gui de.
The installation Guide is shipped with your system.




#拆解
[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g' abc   //将Owner and Operator Guide替换为installation Guide
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
Look in the installation Guide shipped with your system.
Two manuals are provided inc luding the Owner and
Operator Guide and the User Gui de.
The installation Guide is shipped with your system.


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/p' abc   //匹配带有Owner的行(此时只有两行)
Consult Section 3.1 in the Owner and Operator
Two manuals are provided inc luding the Owner and


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;p}' abc  //将匹配行的下一行追加到模式空间
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and
Operator Guide and the User Gui de.


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;p}' abc  //将 *\n替换为空格 就变成了两行合并为一行
Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and Operator Guide and the User Gui de.

[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g;p}' abc  //将Owner and Operator Guide * 替换为installation Guide\n 改变了Owner and Operator Guide然后后面的换行
Consult Section 3.1 in the installation Guide
for a description of the tape drives
Two manuals are provided inc luding the installation Guide
and the User Gui de.

//  D   删除模式空间多行的第一行
//  d   删除模式空间的内容
#
原文档
[root@localhost ~]# cat abc
This line is followed by 1 blank line.

This line is followed by 2 blank lines.


This line is followed by 3 blank lines.



This line is followed by 4 b lank lines.




This is the end.


#改变
[root@localhost ~]# sed  '/^$/{N;/^\n$/d}' abc
This line is followed by 1 blank line.

This line is followed by 2 blank lines.
This line is followed by 3 blank lines.

This line is followed by 4 b lank lines.
This is the end.


#拆解
[root@localhost ~]# sed -n '/^$/{N;p}' abc  //匹配空行将匹配行的下一行追加到模式空间

This line is followed by 2 blank lines.





This line is followed by 4 b lank lines.




[root@localhost ~]# sed -n '/^$/{N;/^\n$/d;p}' abc  //以换行符开头或结尾的删除(空格偶数删除)

This line is followed by 2 blank lines.

This line is followed by 4 b lank lines.

#源文件
[root@localhost ~]# cat abc
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.


#改变
[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc
Here are examples of the UNIX Operating 
System. Where UNIX Operating 
System appears,it should be the UNIX
Operating System.


#拆解
[root@localhost ~]# sed -n '/UNIX$/{N;p}' abc  //匹配UNIX结尾的行 将匹配到行的下一行添加到模式空间
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.


[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{p}}' abc  //  /\nSystem/  在匹配转行后为System的行
Here are examples of the UNIX
System. Where UNIX

[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' abc  //添加Operating
Here are examples of the UNIX Operating 
System. Where UNIX


[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P}}' abc  //P打印模式空间第一行
Here are examples of the UNIX Operating 

[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc  //删除模式空间的第一行
Here are examples of the UNIX Operating 
System. Where UNIX Operating 

源文件
[root@localhost ~]# cat abc
1
2
11
22
111
222

#变化
[root@localhost ~]# sed -n '/1/{h;d};/2/{G};p' abc
2
1
22
11
222
111

[root@localhost ~]# sed -n '/1/{h;d};p' abc   //将模式空间的1追加到保持空间 并删除模式空间的1
2
22
222

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值