Linux常用基本命令:三剑客命令之-sed
2023-09-11 14:19:51 时间
sed是一个很强大的文件处理工具,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作
格式:sed [option] [command] [file]
常用命令:
a ∶新增
c ∶取代
d ∶删除
i ∶插入
p ∶列印
s ∶取代
选项:
-i∶直接修改读取的档案内容,而不是由萤幕输出。
-n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
1,sed '1d' ghostwu.com d代表删除 d前面的数字代表删除第一行,该命令不会修改文件本身
ghostwu@dev:~/linux/sed$ cat -n ghostwu.txt 1 this is ghostwu 2 how are you 3 hod old are you 4 and you 5 fine thank you 6 come with me!!! ghostwu@dev:~/linux/sed$ sed '1d' ghostwu.txt how are you hod old are you and you fine thank you come with me!!! ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you hod old are you and you fine thank you come with me!!!
2,删除最后一行,$代表最后一行
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you hod old are you and you fine thank you come with me!!! ghostwu@dev:~/linux/sed$ sed '$d' ghostwu.txt this is ghostwu how are you hod old are you and you fine thank you
3,删除第一行到第二行
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you hod old are you and you fine thank you come with me!!! ghostwu@dev:~/linux/sed$ sed '1,2d' ghostwu.txt hod old are you and you fine thank you come with me!!!
4,删除第二行到最后一行
ghostwu@dev:~/linux/sed$ sed '2,$d' ghostwu.txt this is ghostwu
5,查找包含'you'的行, /you/ 这是正则表达式, p是打印,要跟n结合起来用
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you hod old are you and you fine thank you come with me!!! ghostwu@dev:~/linux/sed$ sed -n '/you/p' ghostwu.txt how are you hod old are you and you fine thank you
6,匹配$符号结尾的行
$符号在正则表达式表示行尾,所以要用\ 转义
ghostwu@dev:~/linux/sed$ sed -n '/\$/p' ghostwu.txt 50$
7,在第一行后面,增加一行“你好啊"
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you hod old are you and you fine thank you come with me!!! how much do you have 50$ oh, is it? yes ghostwu@dev:~/linux/sed$ sed '1a 你好啊' ghostwu.txt this is ghostwu 你好啊 how are you hod old are you and you fine thank you come with me!!! how much do you have 50$ oh, is it? yes
8,在第一行和第二行的后面,增加一行
ghostwu@dev:~/linux/sed$ sed '1,2a learning how to use sed command' ghostwu.txt this is ghostwu learning how to use sed command how are you learning how to use sed command fine thank you
9,也可以增加多行
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you ghostwu@dev:~/linux/sed$ sed '1a 你好啊\n很高兴认识你' ghostwu.txt this is ghostwu 你好啊 很高兴认识你 how are you fine thank you
10, c为替换操作,数字的意思,跟上面的a一样,代表行
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you ghostwu@dev:~/linux/sed$ sed '1,2c hey guy' ghostwu.txt hey guy fine thank you ghostwu@dev:~/linux/sed$ sed '1c hey guy' ghostwu.txt hey guy how are you fine thank you
11, s:替换匹配到的内容, s:替换开始 /is/ 匹配包含is的行 g:全部替换
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you ghostwu@dev:~/linux/sed$ sed 's/is/IS/' ghostwu.txt thIS is ghostwu how are you fine thank you ghostwu@dev:~/linux/sed$ sed 's/is/IS/g' ghostwu.txt thIS IS ghostwu how are you fine thank you ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you
12、-i :修改,插入文件,会影响文件的内容,在最后一行,插入bye bye
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you ghostwu@dev:~/linux/sed$ sed -i '$a bye bye' ghostwu.txt ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you bye bye
13,在1-3行,每一行的后面都插入一行数字
ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu how are you fine thank you bye bye ghostwu@dev:~/linux/sed$ sed -i '1,3a 123457' ghostwu.txt ghostwu@dev:~/linux/sed$ cat ghostwu.txt this is ghostwu 123457 how are you 123457 fine thank you 123457 bye bye
相关文章
- Linux操作系统常用的网络状态查询命令
- 启动LINUX系统后,进入图形化界面的命令
- Linux学习之常用权限管理命令(二)
- 常用Linux命令收集
- 【Linux】在linux上java工具jps jstat jinfo等命令找不到怎么办
- 【python】一次执行多个linux命令
- linux 添加链接与删除链接(ln命令的用法)
- linux 命令集合收集(ubuntu)
- Linux网络管理相关命令详解
- Linux echo命令:给文本着色/背景色/文本闪动
- Linux 递归创建目录
- 工作中常用的linux命令(2)
- Linux下常用的编辑文件与保存命令
- Linux shell cut 命令详解
- L79.linux命令每日一练 -- 第11章 Linux系统管理命令 -- sar和chkconfig
- L77.linux命令每日一练 -- 第11章 Linux系统管理命令 -- vmstat和mpstat
- L72.linux命令每日一练 -- 第十章 Linux网络管理命令 -- mail和nslookup
- L66.linux命令每日一练 -- 第十章 Linux网络管理命令 -- arp和ip
- L64.linux命令每日一练 -- 第十章 Linux网络管理命令 -- ifconfig和ifup
- L63.linux命令每日一练 -- 第九章 Linux进程管理命令 -- runlevel、init和service
- L59.linux命令每日一练 -- 第九章 Linux进程管理命令 -- killall和pkill
- L57.linux命令每日一练 -- 第九章 Linux进程管理命令 -- ps和pstree
- L52.linux命令每日一练 -- 第八章 Linux磁盘与文件系统管理命令 -- resize2fs和fsck
- L35.linux命令每日一练 -- 第五章 Linux信息显示与搜索文件命令 -- which和whereis
- L33.linux命令每日一练 -- 第五章 Linux信息显示与搜索文件命令 -- du和date
- linux下常用压缩命令 tar压缩Linux解压 压缩Linux压缩Ubuntu压缩centos压缩解压gz txt.gz
- Linux 常用解压缩命令
- 第一章 linux-shell命令入门
- Linux cat命令详解
- Linux之dirname与basename命令
- 16个Linux服务器监控命令