centos中文本处理工具sed怎么用

55次阅读
没有评论

共计 3306 个字符,预计需要花费 9 分钟才能阅读完成。

丸趣 TV 小编给大家分享一下 centos 中文本处理工具 sed 怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

sed 行编辑器

sed 是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用 sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。然后读入下行,执行下一个循环。如果没有使诸如‘D’  的特殊命令,那会在两个循环之间清空模式空间,但不会清空保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重
定向存储输出。

功能

主要用来自动编辑一个或多个文件, 简化对文件的反复操作, 编写转换程序等

常用选项

-n emsp; emsp; 不输出模式空间内容到屏幕,即不自动打印
-e emsp; emsp; 多点编辑
-f /PATH/SCRIPT_FILE emsp; emsp; 从指定文件中读取编辑脚本
-r emsp; emsp; 支持使用扩展正则表达式
-i.bak emsp; emsp; 备份文件并原处编辑

script emsp; emsp; 地址命令 sed 工具地址定界

(1) 不给地址:对全文进行处理

(2) 单地址:
emsp; emsp;#:指定的行,$:最后一行
emsp; emsp;/pattern/:被此处模式所能够匹配到的每一行

(3) 地址范围
emsp; emsp;#,#
emsp; emsp;#,+#
emsp; emsp;/pat1/,/pat2/
emsp; emsp;#,/pat1/

(4) ~:步进
1~2 奇数行
2~2 偶数行 sed 工具

编辑命令

d emsp; emsp; 删除模式空间匹配的行,并立即启用下一轮循环
p emsp; emsp; 打印当前模式空间内容,追加到默认输出之后
a []text emsp; emsp; 在指定行后面追加文本,支持使用 \n 实现多行追加
i []text emsp; emsp; 在行前面插入文本
c []text emsp; emsp; 替换行为单行或多行文本
w /path/file emsp; emsp; 保存模式匹配的行至指定文件
r /path/file emsp; emsp; 读取指定文件的文本至模式空间中匹配到的行后
= emsp; emsp; 为模式空间中的行打印行号
! emsp; emsp; 模式空间中匹配行取反处理 sed 工具
s/// emsp; emsp; 查找替换, 支持使用其它分隔符,s@@@,s###

替换标记

g emsp; emsp; 行内全局替换
p emsp; emsp; 显示替换成功的行
w emsp; emsp;/PATH/FILE 将替换成功的行保存至文件中

高级编辑命令

P: emsp; emsp; 打印模式空间开端至 \n 内容,并追加到默认输出之前
h: emsp; emsp; 把模式空间中的内容覆盖至保持空间中
H: emsp; emsp; 把模式空间中的内容追加至保持空间中
g: emsp; emsp; 从保持空间取出数据覆盖至模式空间
G: emsp; emsp; 从保持空间取出内容追加至模式空间
x: emsp; emsp; 把模式空间中的内容与保持空间中的内容进行互换
n: emsp; emsp; 读取匹配到的行的下一行覆盖至模式空间
N: emsp; emsp; 读取匹配到的行的下一行追加至模式空间
d: emsp; emsp; 删除模式空间中的行
D: emsp; emsp; 如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间不包含换行符,则会像发出 d 命令那样启动正常的新循环

练习

\1. 找出 3 到 6 行
centos 中文本处理工具 sed 怎么用
\2. 找出 3 到 3 + 2 行
centos 中文本处理工具 sed 怎么用
\3. 在第 2 行加入 new line

centos 中文本处理工具 sed 怎么用
\4. 在第 11 行加入 test line

centos 中文本处理工具 sed 怎么用
\5. 匹配以 alias 开头的文本保存到 alias.log 里
centos 中文本处理工具 sed 怎么用
\6. 在每⾏后增加⼀空⾏?

[root@centos ~]# sed G text.txt

\7. 在匹配 regex 的⾏之后插⼊⼀空⾏?

[root@centos ~]# sed /regex/G text.txt

\8. sed 将⽂件 test 中第 50 ⾏中的 haiwao 改为 haiwai?

[root@centos ~]# sed -n 50p |sed s/haiwao/haiwai/ test

\9. 替换一个文件 /etc/passwd 里的这 root:x:0:0:root:/root:/bin/bash 一行第二个 root 为 test?

[root@centos ~]# cat /etc/passwd| sed /^root/!d |sed s/root/test/2

\10. 打印 /etc/passwd 的奇数⾏?

[root@centos ~]# sed -n 1~2p /etc/passwd

\11. 打印出 05 点到 12 点之间的所有⽇志?打印出 05:30:35 到 22:45:55 之间的所有⽇志?

[root@centos ~]# sed -n /2016\/06\/12 05:00:00/,/2016\/06\/12 12:00:00/p a.log
[root@centos ~]# sed -n /2016\/06\/12 05:30:35/,/2016\/06\/12 22:45:55/p a.log

\12. 删除 centos7 系统 /etc/grub2.cfg ⽂件中所有以空⽩开头的⾏⾏⾸的空⽩字符

[root@centos ~]# sed -r s/^[[:blank:]]+// /etc/grub2.cfg

\13. 删除 /etc/fstab ⽂件中所有以 #开头,后⾯⾄少跟⼀个空⽩字符的⾏的⾏⾸的# 和空⽩字符

[root@centos ~]# sed -r s/^#[[:blank:]]+// g /etc/fstab

\14. 在 centos6 系统 /root/install.log 每⼀⾏⾏⾸增加 #号

[root@centos ~]# sed -r s/.*/# / /root/install.log

\15. 在 /etc/fstab ⽂件中不以 #开头的⾏的⾏⾸增加# 号

[root@centos ~]# sed -r s@^[^#]@# @p /etc/fstab

\16. 处理 /etc/fstab 路径, 使⽤ sed 命令取出其⽬录名和基名
目录名:

[root@centos ~]# echo /etc/fstab |sed -r s@^(./)([^/]+/?)$@\1@
基名:
[root@centos ~]# echo /etc/fstab |sed -r s@^(./)([^/]+/?)$@\2@

\17. 利⽤ sed 取出 ifconfig 命令中本机的 IPv4 地址

[root@centos ~]# ifconfig|sed -n /broadcast/p |sed -r s@inet (.) netmask.@\1@
[root@centos ~]# ifconfig ens33 |sed -r 2!d; s@(.inet)(.)(netmask.*)@\2@

\18. 统计 centos 安装光盘中 Package ⽬录下的所有 rpm ⽂件的以. 分隔倒数第⼆个字段的重复次数

[root@centos ~]# ls .rpm|sed -r s@^..([^.]+).rpm$@\1@ |sort|uniq -c
[root@centos ~]# ls *.rpm|rev|cut -d. -f2|rev|sort|uniq -c

\19. 统计 /etc/init.d/functions ⽂件中每个单词的出现次数,并排序(⽤ grep 和 sed 两种⽅法分别实现)

[root@centos ~]# sed -r s/[^[:alpha:]]/\n/g /etc/init.d/functions| sort | uniq -c | sort -nr
[root@centos ~]# egrep -o [[:alpha:]]+ /etc/init.d/functions| sort | uniq -c | sort -nr

\20. 将⽂本⽂件的 n 和 n + 1 ⾏合并为⼀⾏,n 为奇数⾏

[root@centos ~]# seq 10 | sed 1~2N;s/\n/ /

以上是“centos 中文本处理工具 sed 怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计3306字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)