sed 使用 删除匹配行

“p” command prints the buffer (remember to use -n option with “p”) 
“d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle.

Syntax: 
# sed ‘ADDRESS‘d filename 
# sed /PATTERN/d filename

Let us first creates thegeekstuff.txt file that will be used in all the examples mentioned below. 
# cat thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
10.Windows- Sysadmin, reboot etc.

例子1:删除第n行 
sed ‘Nd’ filename 

As per sed methodology, 
It reads the first line and places in its pattern buffer. 
Check whether supplied command is true for this line, if true, deletes pattern space buffer and starts next cycle. i.e Read next line. 
If supplied command doesnt true, as its normal behaviour it prints the content of the pattern space buffer.

$ sed 3d thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development

10.Windows- Sysadmin, reboot etc.

例2:从第三行开始,每隔一行删除 
$sed 3~2d thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
4. Security (Firewall, Network, Online Security etc) 
6. Cool gadgets and websites 
8. Website Design 
10.Windows- Sysadmin, reboot etc.

$

例3:删除从第4行到第8行 
$sed 4,8d thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
9. Software Development 
10.Windows- Sysadmin, reboot etc. 
$sed ‘4,8d‘ thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
9. Software Development 
10.Windows- Sysadmin, reboot etc. 
$sed ‘4,8‘d thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
9. Software Development

10.Windows- Sysadmin, reboot etc.

例4:删除最后一行 
$sed ‘$‘d thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed ‘$d‘ thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design

9. Software Development

例5:行匹配删除 
$sed /Sysadmin/d thegeekstuff.txt 

2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed ‘/Sysadmin/d‘ thegeekstuff.txt 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
$sed ‘/Sysadmin/‘d thegeekstuff.txt 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development

$

例6:从匹配行到末尾行 
$sed ‘/Website Design/,$d‘ thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
$sed ‘/Website Design/,$‘d thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites

7. Productivity (Too many technologies to explore, not much time available)

例7:删除匹配行和之后两行 
$sed ‘/Storage/,+2d‘ thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
8. Website Design 
9. Software Development 
10.Windows- Sysadmin, reboot etc. 
$sed ‘/Storage/,+2‘d thegeekstuff.txt 
1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
8. Website Design 
9. Software Development

10.Windows- Sysadmin, reboot etc.

例8:删除空行 
$sed ‘/^$/d‘ thegeekstuff.txt 

1. Linux - Sysadmin, Scripting etc. 
2. Databases - Oracle, mySQL etc. 
3. Hardware 
4. Security (Firewall, Network, Online Security etc) 
5. Storage 
6. Cool gadgets and websites 
7. Productivity (Too many technologies to explore, not much time available) 
8. Website Design 
9. Software Development 
10.Windows- Sysadmin, reboot etc.

时间: 2024-10-15 05:19:47

sed 使用 删除匹配行的相关文章

sed命令:删除匹配行和替换

删除以a开头的行 sed -i '/^a.*/d' tmp.txt -i 表示操作在源文件上生效.否则操作内存中数据,并不写入文件中.在分号内的/d表示删除匹配的行 替换匹配行: sed -i 's/^a.*/haha/g' tmp.txt 分号内的s/表示替换/g表示全局替换 原文地址:https://www.cnblogs.com/qiumingcheng/p/12001132.html

删除匹配行之后或者之前的数据

匹配到包含abc的行,然后将abc行及之后的数据删除 sed '/abc/,$d' file 匹配到包含abc的行,然后将abc行之前的数据删除(取巧的办法,希望有大帮解决) sed -n '/abc/,$p' file > new_file

python re删除匹配行

re.compile(strPattern[, flag]): 这个方法是Pattern类的工厂方法,用于将字符串形式的正则表达式编译为Pattern对象. 第二个参数flag是匹配模式,取值可以使用按位或运算符'|'表示同时生效,比如re.I | re.M. 另外,你也可以在regex字符串中指定模式,比如re.compile('pattern', re.I | re.M)与re.compile('(?im)pattern')是等价的. xxx.com文件内容 $TTL 600 ; 1 day

拾遗:Vim 批量删除匹配到的行

删除包含特定字符的行 g/pattern/d (全局删除匹配行) 1,5g/pattern/d (删除第1-5行里的匹配行) 删除不包含指定字符的行 v/pattern/d g!/pattern/d (全部删除) ...

shell之sed命令删除变量中带有“日期时间等内容”的方法

sed命令删除指定等内容可能大家都知道,比如想要删除某个文件中包含"badboy"的那行,直接使用下面的命令就可以: sed -i '/badboy/d' file.txt 就可以把flie.txt文件中包含badboy的内容全部删掉. sed命令删除指定行,如果匹配字符用变量替代,变量中包含"/"符号,使用下面方法处理: 例如: [[email protected]]# vi a.txt /usr/sbin/restart.sh /usr/sbin/control

sed删除文本第一个匹配行

源文本如下,要求删除第一个为happy-123456的行. ----------------------------- aaaaaaa happy-123456 bbbbbb asdasawe happy-123456 dsafsdfsd sagasdfasd happy-123456 ------------------------------ 目标文本: ----------------------------- aaaaaaa bbbbbb asdasawe happy-123456 ds

Linux sed正则匹配删除整行

原文内容: [[email protected] tmp]# more test.log 2019-12-01 09:09:02 Failed 2019-12-01 09:12:02 Failed 2019-12-01 09:15:03 Failed 2019-12-01 09:18:02 Failed 正则匹配Failed,删除整行(直接替换文本内容) [[email protected] tmp] sed -i '/Failed$/'d check.log 原文地址:https://www.

sed系列:行或者模式匹配删除特定行

“p” command prints the buffer (remember to use -n option with “p”) “d” command is just opposite, its for deletion. ‘d’ will delete the pattern space buffer and immediately starts the next cycle. Syntax: # sed 'ADDRESS'd filename # sed /PATTERN/d file

Linux之sed:删除某行以及替换

sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed [-nefri] 'command' 输入文本 常用选项:        -n∶使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上.但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来.        -e∶直接在