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 过滤目录文件(给不同类型文件结尾加上不同的符号) ...
随机推荐
- java 日志框架
1.java常用日志框架介绍: https://www.cnblogs.com/chenhongliang/p/5312517.html 2.java各类日志组件汇总: https://blog.cs ...
- 如何将已有的本地Git 库推送到远端仓库?
以Github 为例 step 1. 在Github建立一个空的仓库 Step 2. 建立远端仓库的别名 >$ git remote add origin https://github.com/ ...
- 【算法】实现字典API:有序数组和无序链表
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- Linux下Redis安装使用教程
https://redis.io/download 第一步:安装redis需要在有c语言的编译环境下,执行命令安装c语言环境: yum install gcc-c++ https://blog.csd ...
- SharkApktool 源码攻略
作者:HAI_ 原文来自:https://bbs.ichunqiu.com/thread-43219-1-1.html 0×00 前言 网上的资料对于apktool的源码分析,来来回回就那么几个,而且 ...
- 面试时遇到的题目。正则,replace()
function Fn(str){ this.str = str; } Fn.prototype.format = function(){ var arg = arguments; var dd = ...
- Window安装Erlang环境
最近学习RabbitMQ,下载rabbitmq-server后,安装提示,需要erlang环境,接着又下载erlang安装包. 仅在这里提供下安装文件和环境配置方法. 1.rabiitmq-serve ...
- 25-socket
socket通信流程 #server端 #family参数代表地址家族,可为AF_INET或AF_UNIX.AF_INET家族包括#Internet地址,AF_UNIX家族用于同一台机器上的进程间通信 ...
- [转]KMP算法理解及java实现
这大概是我看的最好懂的KMP算法讲解了,不过我还只弄懂了大概思想,算法实现我到时候用java实现一遍 出处:知乎 https://www.zhihu.com/question/21923021/ans ...
- Linux学习笔记之十一————Linux常用服务器构建之ssh和scp
一.ssh 1.ssh介绍 SSH为Secure Shell的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议. ...