标签:注意 文件合并 ges 单词 xfs kernel mon nta -name
1.功能说明Sed是Stream Editor(流编辑器)缩写,是操作、过滤和转换文本内容的强大工具。常用功能有增删改查,过滤,取行。
[root@gumt ~]#sed --version
sed (GNU sed) 4.2.2
SYNOPSIS
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
说明:
(1)注意sed和后面的选项之间至少有一个空格。
(2)为了避免混淆,本文称呼sed为sed软件。sed-commands(sed命令)是sed软件内置的一些命令选项,为了和前面的options(选项)区分,故称为sed命令。
(3)sed-commands既可以是单个sed命令,也可以是多个sed命令组合。
(4)input-file(输入文件)是可选项,sed还能够从标准输入如管道获取输入。
常用选项-[OPTION]:
-n:不输出模式空间中的内容至屏幕;
-e script, --expression=script:多点编辑
[root@gumt ~]#sed -e ‘s@^#[[:space:]]*@@‘ -e ‘/^UUID/d‘ /etc/fstab
/etc/fstab
Created by anaconda on Mon Mar 11 20:02:33 2019
Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos_gumt-root / xfs defaults 0 0
/dev/mapper/centos_gumt-home /home xfs defaults 0 0
/dev/mapper/centos_gumt-swap swap swap defaults 0 0
-f /PATH/TO/SED_SCRIPT_FILE:每一行一个编辑命令
-r, --regexp-extended:支持使用正则表达式
-i[SUFFIX], --in-place[=SUFFIX]:直接编辑源文件
地址定界:
(1)空地址:对全文进行处理
(2)单地址:
#:指定行
/pattern/:被此模式所匹配到的每一行
(3)地址范围
#,#:
#,+#:
#,/part1/
$:最后一行
(4)步进:~
1~2:所有奇数行
2~2:所有偶数行
编辑命令:
d:删除
[root@gumt ~]#sed ‘11,12d‘ /etc/fstab
#/etc/fstab
#Created by anaconda on Mon Mar 11 20:02:33 2019
#Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos_gumt-root / xfs defaults 0 0
UUID=7f9112ef-49d5-44b9-ab48-f78c34d70ee0 /boot xfs defaults 0 0
p:显示模式空间中的内容
[root@gumt ~]#sed ‘11,12p‘ /etc/fstab
#/etc/fstab
#Created by anaconda on Mon Mar 11 20:02:33 2019
#Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos_gumt-root / xfs defaults 0 0
UUID=7f9112ef-49d5-44b9-ab48-f78c34d70ee0 /boot xfs defaults 0 0
*/dev/mapper/centosgumt-home /home xfs defaults 0 0
/dev/mapper/centosgumt-home /home xfs defaults 0 0
/dev/mapper/centosgumt-swap swap swap defaults 0 0
/dev/mapper/centosgumt-swap swap swap defaults 0 0
a \text:在行后面追加文本“text”,支持使用\n实现多行追加
[root@gumt ~]#sed ‘3a \new line‘ /etc/fstab
#/etc/fstab
** new line**
#Created by anaconda on Mon Mar 11 20:02:33 2019
#Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos_gumt-root / xfs defaults 0 0
UUID=7f9112ef-49d5-44b9-ab48-f78c34d70ee0 /boot xfs defaults 0 0
/dev/mapper/centos_gumt-home /home xfs defaults 0 0
/dev/mapper/centos_gumt-swap swap swap defaults 0 0
l \text:在行前面追加文本“text”,支持使用\n实现多行插入
c \text:把匹配的行替换为此处指定的文本“text”
w /PATH/TO/SOMEFILE:保存模式空间匹配到的行至指定的文件中
[root@gumt ~]#sed ‘/^[^#]/w /tmp/fstab.bak‘ /etc/fstab
#/etc/fstab
#Created by anaconda on Mon Mar 11 20:02:33 2019
#Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos_gumt-root / xfs defaults 0 0
UUID=7f9112ef-49d5-44b9-ab48-f78c34d70ee0 /boot xfs defaults 0 0
/dev/mapper/centos_gumt-home /home xfs defaults 0 0
/dev/mapper/centos_gumt-swap swap swap defaults 0 0
[root@gumt ~]#cat /tmp/fstab.bak
/dev/mapper/centos_gumt-root / xfs defaults 0 0
UUID=7f9112ef-49d5-44b9-ab48-f78c34d70ee0 /boot xfs defaults 0 0
/dev/mapper/centos_gumt-home /home xfs defaults 0 0
/dev/mapper/centos_gumt-swap swap swap defaults 0 0
r /PATH/TO/SOMEFILE:读取指定文件的内容至当前文件被莫忽视匹配到的行后面;文件合并
[root@gumt ~]#sed ‘3r /etc/issue‘ /etc/fstab
#/etc/fstab
\S
Kernel \r on an \m
#Created by anaconda on Mon Mar 11 20:02:33 2019
#Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos_gumt-root / xfs defaults 0 0
UUID=7f9112ef-49d5-44b9-ab48-f78c34d70ee0 /boot xfs defaults 0 0
/dev/mapper/centos_gumt-home /home xfs defaults 0 0
/dev/mapper/centos_gumt-swap swap swap defaults 0 0
= :为模式空间匹配到的行打印行号
!:条件取反;
地址定界!编辑命令
[root@gumt ~]#sed ‘/^#/!d‘ /etc/fstab
#/etc/fstab
#Created by anaconda on Mon Mar 11 20:02:33 2019
#Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
s///:查找替换,其分割符课可自行指定
替换标记:
g:全局替换
w:/PATH/TO/SOMEFILE:将替换成功的结果保存至指定文件中
p:显示替换成功的行
输出长度不小于50个字符的行
sed -n ‘/^.{50}/p‘
统计文件中有每个单词出现了多少次
sed ‘s/ /\n/g‘ file | sort | uniq -c
查找目录中的py文件,删掉所有行级注释
find ./ -name "*.py" | xargs sed -i.bak ‘/^[ ]*#/d‘
查看第5-7行和10-13行
sed -n -e ‘5,7p‘ -e ‘10,13p‘ file
仅输出ip地址
ip route show | sed -n ‘/src/p‘ | sed -e ‘s/ */ /g‘ | cut -d‘ ‘ -f9
标签:注意 文件合并 ges 单词 xfs kernel mon nta -name
原文地址:https://blog.51cto.com/13369003/2424321