Sed学习笔记1

2019-04-24  本文已影响0人  dming1024

sed有以下几种常用的工作模式:
-n,静态处理命令,不涉及的行不打印;
-e,默认选项,把sed后的输入当做命令来执行;
-i,会直接修改文件;
-r,扩展正则表达式。
直接讲述实例:

cat sedExample.txt#输出文件查看一下内容,第一行是空行

today is May
today date is 24th May
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

打印第2行

 cat sedExample.txt |sed -n '2p'
 today is May

打印指定的行(含有May字符的行)

cat sedExample.txt |sed -n '/May/p'
today is May
today date is 24th May

打印有day单词的行

 cat sedExample.txt |sed -n '/\<day\>/p'
what day is it today
it is the last day of the word

打印有day字符的行

cat sedExample.txt |sed -n '/day/p'
today is May
today date is 24th May
today is Wednesday
what day is it today
it is the last day of the word

打印空行(^$代表空行)

cat sedExample.txt |sed -n '/^$/p'

打印非空行(!代表反选)

cat sedExample.txt |sed -n '/^$/!p'
today is May
today date is 24th May
today is Wednesday
what day is it today
what a beautiful
it is the last day of the word

打印有day单词的行,以及行号(=显示行号)

cat sedExample.txt |sed -n '/\<day\>/{=;p}'
5
what day is it today
7
it is the last day of the word

打印以today开头的行

cat sedExample.txt |sed -n '/^today/p'
today is May
today date is 24th May
today is Wednesday

打印以May结尾的行

cat sedExample.txt |sed -n '/May$/p'
today is May
today date is 24th May

sed结合正则表达式使用,会有非常强大的文本处理功能

上一篇下一篇

猜你喜欢

热点阅读