Linux常用基本命令:三剑客命令之-sed
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
this is ghostwu
how are you
hod old are you
and you
fine thank you
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
$
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
$
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
$
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 how are you fine thank you bye bye
Linux常用基本命令:三剑客命令之-sed的更多相关文章
- linux常用文本编缉命令(strings/sed/awk/cut)
一.strings strings--读出文件中的所有字符串 二.sed--文本编缉 类型 命令 命令说明 字符串替换 sed -i 's/str_reg/str_rep/' filename 将文件 ...
- Linux常用基本命令(less)
转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...
- Linux 常用基本命令及应用技巧
需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...
- ## 本篇文章对linux常用的一些命令做一下总结,如有需要补充以及不懂得地方,请在下方留言 适合于linux初学者,以及对命令掌握不牢的用来备忘
本篇文章对linux常用的一些命令做一下总结,如有需要补充以及不懂得地方,请在下方留言 适合于linux初学者,以及对命令掌握不牢的用来备忘一,磁盘管理1.显示当前目录位置 pwd2.切换目录 cd ...
- Linux 常用的压缩命令有 gzip 和 zip
Linux 常用的压缩命令有 gzip 和 zip,两种压缩包的结尾不同:zip 压缩的后文件是 *.zip ,而 gzip 压缩后的文件 *.gz 相应的解压缩命令则是 gunzip 和 unzip ...
- Linux常用基本命令:三剑客命令之-awk基础用法
awk是一个超级强大的文本格式化处理工具,他与grep, sed命令被成为linux 三剑客命令 三剑客命令的特点: grep:只要用来匹配和查找文本 sed: 编辑匹配到文本 awk: 格式化文本, ...
- linux 三剑客命令(grep,sed ,awk)
grep 命令 :强大的文本’搜索’工具 1.grep -n 'word' file_name 在file_name文件中找到word所在的所有行并显示.-n 为显示行号. 2 ...
- LINUX常用配置及命令
一. Fedora系统配置 1. [设置网卡IP] 步骤如下: 1) 用root用户登陆,打开/etc/sysconfig/network-scripts/ifcfg-eth0文 ...
- Linux常用的基础命令总结
man 查看英文命令帮助 可以看作--help 拷贝目录的命令cp -a 包含所有 ls -a 显示所有文件包括隐藏文件 -ld ls -F 过滤目录文件(给不同类型文件结尾加上不同的符号) ...
随机推荐
- CentOS6最佳实践
一 安装常用软件 常用目录结构 源文件目录 /application,原包文件及解压文件 如 /application/Python-3.6.0.tgz 软件配置目录 /usr/local/ 如 ...
- 常用string格式化
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- [转]kaldi特征和模型空间转换
转:http://blog.csdn.net/shmilyforyq/article/details/76807431 博主话:这篇博客是对kaldi官网中Feature and model-spac ...
- 42_并发编程-JionableQueue
一.生产者消费者模型 在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程.在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生 ...
- iOS 不能加载电子签名
问题: 1:使用WKWebView在iOS12.0以上的系统中,可以显示PDF中的电子图章,签名.在iOS12.0以下的系统中不能显示电子签名,图章. 2: 解决方案,使用PDF.js加载. pdf ...
- Objective-C优缺点
优点: 1:Category,使用category可以在不改变原来类的同时为类增加新的方法或者重写原来类的方法实现(使用runtime方法还可以在分类中实现方法交换和添加属性操作) 2:运行时 动态识 ...
- Android开发工程师文集-相关控件的讲解,五大布局
前言 大家好,给大家带来Android开发工程师文集-相关控件的讲解,五大布局的概述,希望你们喜欢 TextView控件 TextView控件有哪些属性: android:id->控件的id a ...
- 卖给高通之后的CSR的现状和未来
转眼之间,CSR已经嫁给高通两年了,养在深宫大院大小妾的CSR,到底过的怎么样呢? 从高通官网上查看的结果显示,CSR产品被分成了三类: A 传统的用在耳机音响的CSR86XX系列,这部分改动不大,就 ...
- odoo开发笔记 -- 传递上下文实现列表视图按照指定条件过滤显示
按钮传递上下文: <xpath expr="//div[@name='dec_work_sheet_id']" position="after"> ...
- 从零开始学 Web 之 CSS3(四)边框图片,过渡
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...