sed练习简记
1. 使用多命令选项-e
sed -e ‘command1’ -e ‘command2’ -e ‘command3’
在/etc/passwd文件中搜索root、nobody或mail
[root@sishen ~]# sed -n -e '/^root/ p' -e '/^nobody/ p' -e '/^mail/ p' /etc/passwdroot:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
2. 使用\折行执行多个命令
[root@sishen ~]# sed -n -e '/^root/ p' \
> -e '/^nobody/ p' \
> -e '/^mail/ p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
3. 使用{ }把多个命令组合
如果要执行很多sed命令,可以使用{ } 把他们组合起来执行,如:
[root@sishen ~]# sed -n '{ /^root/ p
/^nobody/ p
/^mail/ p
}' /etc/passwd #注意}’与/etc/passwd之间的空格
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
sed脚本文件
首先建立测试文件
[root@sishen ~]# vim sedcommand.sed
s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g
s/^.*/<&>/
s/Developer/IT Manager/
s/Manager/Directory/
[root@sishen ~]# sed -f sedcommand.sed employee.txt
<John Doe,101, CEO>
<Jason Smith,102, IT Directory>
<Raj Reddy,103, Sysadmin>
<Anand Ram,104, IT Directory>
<Jane Miller,105, Sales Directory>
<Jane Miller,#106, Sales Directory>
<Jane Miller,#107, Sales Directory>
sed注释
[root@sishen ~]# cat sedcommand.sed
#交换第一列和第二列
s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g
#把整行内容放入<>中
s/^.*/<&>/
#把Developer替换为IT Manager
s/Developer/IT Manager/
#把Manager替换为DIrectory
s/Manager/Directory/
注意:如果sed脚本第一列开始的两个字符是#n的话,sed会自动使用-n选项(即不自动打印模式空间的内容)一般来说,不采用这种方法,尤其是在写脚本时定义执行脚本命令(即#!)时,容易造成混乱,产生错误,通常做法是使用参数”-n”
sed练习简记的更多相关文章
- sed简单脚本练习
sed脚本的执行顺序可以简记为:Read,Execute,Print,Repeat(读取,执行,打印,重复)简称REPR 分析脚本的执行顺序: l 读取一行到模式空间(sed内部的一个临时缓存,用于存 ...
- Linux shell基础(五)sed命令
一.sed命令 sed是一种强大的流式编辑器 (stream editor for filtering and transforming text),它能够完美的使用正则表达式,逐行处理文本并把结果显 ...
- docker简记
title: docker学习简记 date: 2019-10-16 15:10:39 tags: docker Docker简记 1:Docker简介 1)出现背景 一款产品从开发到上线,从操作系统 ...
- RangePartitioner 实现简记
摘要: 1.背景 2.rangeBounds 上边界数组源码走读 3.RangePartitioner的sketch 源码走读 4.determineBounds 源码走读 5.关于RangePart ...
- sed的应用
h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...
- 文本处理三剑客之sed命令
第十八章.文本处理三剑客之sed命令 目录 sed介绍 sed命令常用选项 sed常用编辑命令 sed使用示例 sed高级语法 18.1.sed简介 sed全名stream editor,流编辑器,s ...
- 6-2 sed 命令
1. sed : stream editor,流编辑器 是处理纯ASICC纯文本,按行琢行操作的. 编辑器有两种,行编辑器和全屏编辑器 sed:默认不编辑原文件,仅对模式空间中的数据做处理,而后.处理 ...
- 基本shell编程【3】- 常用的工具awk\sed\sort\uniq\od
awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk '' | output 1.首先要知道形式 awk 'command' fi ...
- sed awk grep三剑客常用
sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ...
随机推荐
- ASP.NET Web Pages - 教程
ASP.NET Web Pages - 教程 ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web ...
- Codeforces 344B Simple Molecules
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf("%d%d%d", ...
- iOS iOS8中 问题"registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" 解决方式
问题重述: iOS 8中改变了通知注冊的方式,假设App须要同一时候支持iOS 7 和 8 的话,须要首先检查selector. 解决方式:在Xcode 6中 - (BOOL)application: ...
- vi下对齐代码的操作
时不时会用到,但easy忘,在这里记录一下 1. ctrl + v (选中块) 2. ctrl + f (向前) 或 ctrl +v (向后) 3. 按"=", 把选中的代码对齐
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
当我在运行一个基于scrapy的爬虫时出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordina ...
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...
- jQuery操作Form表单元素
Web开发中常常须要操作表单,form表单元素有select.checkbox.radio.textarea.button.file.text.hidden.password等. 当中checkbox ...
- 2016/05/05 smarty ① 登录 ②主页面 ③删除 ④让缩略信息显示完整 (补:增加 修改 )
共 八个页面 ①login.php <?php include("init.inc.php"); $smarty->display("login.html& ...
- BZOJ 2460: [BeiJing2011]元素 线性基
2460: [BeiJing2011]元素 Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力 ...
- Lightoj 1166 - Old Sorting
Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to s ...