linux三剑客之一sed

本文详细介绍了Linux sed命令的基础用法,包括如何处理行、选择特定行进行操作、替换、插入和删除等实例,以及如何结合正则表达式实现高效文本编辑。通过一系列实例演示了如何在文本文件中查找、替换和添加内容,是Linux文本处理的重要工具。
摘要由CSDN通过智能技术生成

linux三剑客之一sed

1. 基本用法

sed 是一种新型的,非交互式的编辑器。它能执行与编辑器 vi 和 ex 相同的编辑任务。sed 编辑器没有提供交互式使用方式,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。 sed 编辑器没有破坏性,它不会修改文件,除非使用 shell 重定向来保存输出结果。默认情况下,所有的输出行都被打印到屏幕上。

命令格式:
sed 命令行格式为: sed [选项] 'command' 输入文本

sed 处理行
Sed 命令在没有给定的位置时,默认会处理所有行;
sed 支持几种地址类型:
1. first~step
 first 指起始匹配行, step 指步长,例如: sed -n 2~5p 含义:从第二行开始匹配,隔 5 行匹配一次,即 2,7,12.......
2. $ 匹配最后一行
3. /REGEXP/
这个是表示匹配正则那一行,通过//之间的正则来匹配
4. addr1, add2
定址 addr1, add2 决定用于对哪些行进行编辑。地址的形式可以是数字、正则表达式或二者的结合。如果没有指定地址, sed 将处理输入文件中的所有行。如果定址是一个数字,则这个数字代表行号,如果是逗号分隔的两个行号,那么需要处理的定址就是两行之间的范围(包括两行在内)。范围可以是数字,正则或二者组合。
5. addr1, +N
从 addr1 这行到往下 N 行匹配,总共匹配 N+1 行

2. 实例

[root@Gin scripts]# cat ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13

1. 
[root@Gin scripts]# sed '/north/p' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
默认情况下, sed 把所有输入行都打印在标准输出上。如果在某一行匹配到 north, sed就把该行另外打印一遍。
2.[root@Gin scripts]# sed -n '/north/p' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
默认情况下, sed 打印当前缓存区中的输入行。命令 p 指示 sed 将再次打印该行。选项-n 取消 sed 取消默认打印操作。选线-n 和命令配合使用,模式缓冲区内的输入行,只被打印一次。如果不指定-n 选项, sed 就会像上例中那样,打印出重复的行。如果指定了-n,则sed 只打印包含模式 north 的行。

3.
[root@Gin scripts]# sed '3d' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
删除第 34.[root@Gin scripts]# sed '3,$d' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
删除从第三行到最后一行内容,剩余各行被打印。地址范围是开始第 3 行,结束最后一行
5.[root@Gin scripts]# sed 's/west/north/g' ceshi.txt
northnorth      NW      Charles Main    3.0     .98     3       34
northern                WE      Sharon Gray     5.3     .97     5       23
southnorth      SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
s 命令用于替换。命令末端的 g 表示在行内全局替换;也就是说如果每一行里出现多个west,所有的 west 都会被替换为 north。如果没有 g 命令,则只将每一行的第一 west 替换为 north

6.[root@Gin scripts]# sed -n 's/Hemenway/Jones/gp' ceshi.txt
southeast       SE      Patricia Jones  4.0     .7      4       17
[root@Gin scripts]# sed -n 's/Hemenway/Jones/gp' ceshi.txt
southeast       SE      Patricia Jones  4.0     .7      4       17
7.[root@Gin scripts]# sed '/^north/a Hello world!' ceshi.txt 
northwest       NW      Charles Main    3.0     .98     3       34
Hello world!
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
Hello world!
north           NO      Margot Weber    4.5     .89     5       9
Hello world!
central         CT      Ann Stephens    5.7     .94     5       13
命令 a 用于追加。字符串 Hello, World!被加在以 north 开头的各行之后。如果要追加的内容超过一行,则除最后一行外,其他各行都必须以反斜杠结尾.
8.[root@Gin scripts]# sed '/eastern/i Hello,world!\
> -----------------------' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
Hello,world!
-----------------------
eastern         EA      TB Savage       4.4     .84     5       20
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
命令 i 是插入命令。如果在某一行匹配到模式 eastern,i 命令就在该行的上方插入命令中插入反斜杠后面后的文本。除了最后一行.
9.[root@Gin scripts]# sed '/eastern/c Hello,world! \
> ------------------' ceshi.txt
northwest       NW      Charles Main    3.0     .98     3       34
western         WE      Sharon Gray     5.3     .97     5       23
southwest       SW      Lewis Dalsass   2.7     .8      2       18
southern        SO      Suan Chin       5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
Hello,world!
------------------
northeast       NE      AM Main Jr.     5.1     .94     3       13
north           NO      Margot Weber    4.5     .89     5       9
central         CT      Ann Stephens    5.7     .94     5       13
c 命令是修改命令。该命令将完整地修改在模式缓冲区行的当前行。如果模式 eastern被匹配, c 命令将其后的文本替换包含 eastern 的行。
10.[root@Gin scripts]# cat sed.txt
1today is nice day
2you can walk out on the street
3it will be import to you
[root@Gin scripts]# sed 's/^[0-9][0-9]*//g' sed.txt
today is nice day
you can walk out on the street
it will be import to you

配置文件往往都带有数字,现在需要删除所有行的首数字.

[转载链接](linux命令总结sed命令详解 - 琴酒网络 - 博客园 (cnblogs.com))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值