【Linux】sed笔记
sed - stream editor for filtering and transforming text(用于过滤和转换文本的SED流编辑器),主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作.
语法
sed [-hnV][-e<script>][-f<script文件>][文本文件]
选项:
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
命令:
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
实例
①以行为单位的替换(注意:sed 后面接的动作,请务必以 '' 两个单引号括住喔!)
yunduo@yunduo-ThinkCentre-XXXX:~/test$ nl abc
1 a
2 b
3 c
4 d
5 e
yunduo@yunduo-ThinkCentre-XXXX:~/test$ nl abc | sed '2,5c replace No 2-5 line'
1 a
replace No 2-5 line
②数据的搜寻并替换
sed 's/要被取代的字串/新的字串/g'
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed 's/a/1/g' ./abc
1
b
c
d
e
③在第三行后添加一行数据"newline"
yunduo@yunduo-ThinkCentre-XXXX:~/test$ cat testfile
sudo find ./ -perm 755 | xargs -i cp {} /usr/bin/
sudo docker rm `docker ps -a|grep Exited|awk '{print $1}'`
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed -e 3a\newline testfile
sudo find ./ -perm 755 | xargs -i cp {} /usr/bin/
sudo docker rm `docker ps -a|grep Exited|awk '{print $1}'`
newline
④在第二行前插入数据"drink tea"
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed '2i drink tea' testfile
sudo find ./ -perm 755 | xargs -i cp {} /usr/bin/
drink tea
sudo docker rm `docker ps -a|grep Exited|awk '{print $1}'`
⑤以行为单位的显示
yunduo@yunduo-ThinkCentre-XXXX:~/test$ nl abc | sed -n '3,5p'
3 c
4 d
5 e
⑥数据的搜寻与显示
yunduo@yunduo-ThinkCentre-XXXX:~/test$ nl abc | sed '/b/p'
1 a
2 b
2 b
3 c
4 d
5 e
⑦以行为单位的删除
yunduo@yunduo-ThinkCentre-XXXX:~/test$ cat abc
a
b
c
d
e
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed '2,4d' abc
a
e
⑧仅显示处理结果
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed '/b/p' abc
a
b
b
c
d
e
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed -n '/b/p' abc
b
⑨多点编辑
yunduo@yunduo-ThinkCentre-XXXX:~/test$ sed -e '3,$d' -e 's/sudo//' testfile
find ./ -perm 755 | xargs -i cp {} /usr/bin/
【Linux】sed笔记的更多相关文章
- Linux内核笔记--内存管理之用户态进程内存分配
内核版本:linux-2.6.11 Linux在加载一个可执行程序的时候做了种种复杂的工作,内存分配是其中非常重要的一环,作为一个linux程序员必然会想要知道这个过程到底是怎么样的,内核源码会告诉你 ...
- Linux 学习笔记
Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...
- linux学习笔记2-linux的常用命令
第一篇博客:linux学习笔记1-ubuntu的安装与基本设置 之中,已经介绍了如何安装linux操作系统,以及一些基本的设置修改. 本篇博客主要介绍linux中的一些常用的终端命令 ======== ...
- Linux学习笔记(一)2015.4.13
研究生由单片机转Linux学习 首先安装VMware虚拟机,用的是VMware 10.0 在VMware 10.0上安装视频上推荐的Red Hat Linux 5 安装后正式进入Linux学习 笔记1 ...
- 跟着鸟哥学Linux系列笔记3-第11章BASH学习
跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 跟着鸟哥学Linux系列笔记2-第10章VIM学习 认识与学习bash 1. ...
- 跟着鸟哥学Linux系列笔记2-第10章VIM学习
跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 常用的文本编辑器:Emacs, pico, nano, joe, vim VI ...
- 跟着鸟哥学Linux系列笔记0-如何解决问题
跟着鸟哥学Linux系列笔记0-扫盲之概念 在发生问题怎么处理: 1. 在自己的主机.网络数据库上查询How-To或FAQ -Linux 自身的文件数据: /usr/share/doc -CLDP中 ...
- 跟着鸟哥学Linux系列笔记1
跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 装完linux之后,接下来一步就是进行相关命令的学习了 第五章:首次登录与在线求助man page 1. X ...
- Linux sed 替换第一次出现的字符串
/********************************************************************************* * Linux sed 替换第一次 ...
- linux sed命令参数及用法详解
linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...
随机推荐
- pycharm 永久注册
pycharm 使用又到期了,找到了破解版亲测(到期日期2099/12/31),绝对简单好用,直接使用步骤: 一,下载pycharm(windows版): https://www.jetbrains ...
- 【To Read】Shortest Palindrome(KMP)
题意:Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...
- linux cat /etc/passwd 说明
通常在Linux系统中,用户的关键信息被存放在系统的/etc/passwd文件中,系统的每一个合法用户账号对应于该文件中的一行记录.这行记录定义了每个用户账号的属性.下面是一个passwd文件的示例( ...
- 51nod 1686 第K大区间【离散化+二分】
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 定义一个区间的值为其众数出现的次数. 现给出n ...
- Spring security获取当前用户
1.如果在jsp页面中获取可以使用spring security的标签 页面引入标签 <%@ taglib prefix="sec" uri="http://www ...
- lattice planner 规划详解
大家好,我是来自百度智能驾驶事业群的许珂诚.今天很高兴能给大家分享Apollo 3.0新发布的Lattice规划算法. Lattice算法隶属于规划模块.规划模块以预测模块.routing模块.高精地 ...
- composer基本使用
一.Composer的安装 1.下载Composer 2.Composer安装 1).Composer安装前请确保已经安装了php:打开命令行窗口输入php -v可以查看php的当前版本号. 3.局部 ...
- hdu 4629 Burning (扫描线)
Problem - 4629 以前写过PSLG模拟的版本,今天写了一下扫描线做这题. 其实这题可以用set存线段来做,类似于判断直线交的做法.不过实现起来有点麻烦,于是我就直接暴力求交点了. 代码如下 ...
- 梯度优化算法Adam
最近读一个代码发现用了一个梯度更新方法, 刚开始还以为是什么奇奇怪怪的梯度下降法, 最后分析一下是用一阶梯度及其二次幂做的梯度更新.网上搜了一下, 果然就是称为Adam的梯度更新算法, 全称是:自适应 ...
- poj 3572 Hanoi Tower
Hanoi Towers Time Limit : 10000/5000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...