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 过滤目录文件(给不同类型文件结尾加上不同的符号) ...
随机推荐
- Vuejs——(12)组件——动态组件
版权声明:出处http://blog.csdn.net/qq20004604 目录(?)[+] 本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...
- Android一些日常的错误
一.加载.so出现的一些问题 1. so文件 放进了优先级低的ABI目录 问题:如果你的项目中,有其他优先级更好的ABI目录,但是你把ABI文件方法放到了优先级低的目录,最后导致你的ABI文件无法被加 ...
- String的substring方法
string.substring(beginIndex, endIndex) 左闭右开. 测试 public static void main(String[] args) { String a = ...
- Javascript高级编程学习笔记(9)—— 执行环境
今天主要讲一下,JS底层的一些东西,这些东西不太好举例(应该是我水平不够) 望大家多多海涵,比心心 执行环境 执行环境(执行上下文,全文使用执行环境 )是JS中最为重要的一个概念,执行环境决定了,变量 ...
- [CocoaPods]使用Trunk进行设置
CocoaPods Trunk CocoaPods Trunk是一种身份验证和CocoaPods API服务.要将新的或更新的库发布到CocoaPods以进行公开发布,您需要在Trunk中注册并在当前 ...
- LabVIEW(十二):VI本地化-控件标题内容的修改
一.对于一般LabVIEW的学习,很少遇到本地化的问题但是我们经常会遇到界面控件标题的显示问题.由于各个技术领域的专业性,往往用户对VI界面的显示有自己的要求,其中就包括控件的标题问题,这可以理解成本 ...
- spring boot -thymeleaf-url
绝对路径格式:th:href="@{http://www.baidu.com}" <a th:href="@{http://www.baidu.com}" ...
- @pathvariable和@RequestParam的区别
@PathVariable 获取的是请求路径url中的值: (http://xxx.xxx.com/get_10.html,侧重于请求的URL路径里面的{xx}变量 ) //获取url中某部分的值 @ ...
- oracle中查询用户表/索引/视图创建语句
不多说,直接上干货 1.查询当前用户下表的创建语句 select dbms_metadata.get_ddl('TABLE','ux_future') from dual; 2.查询其他用户下表的创建 ...
- PyCharm的基本快捷键和配置简介
快捷键 1.编辑(Editing)Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctr ...