查找字符串所在行 : grep -n “待查找字符串” “文件名”
显示指定行信息:sed -n '1,5p' “指定文件” 表示显示指定文件第一至五行的信息
--------------------------------------------------------------------------
sed关键Options介绍:
-n
: 安静模式。一般sed用法中,所有来自STDIN的数据都会被输出到屏幕上,使用-n只有被sed处理的行才会列出来。如果不使用-n,使用sed打印时,会把输入流和处理的信息都打印一遍a
:append,追加文本i
:insert,插入文本d
:delete,删除文本s
: 模式匹配替换p
:打印文本
sed使用示例
- 在指定行插入或追加: a, i
a. 在test.txt第一行前插入:sed “1 i This is a test file” test.txt
b. 在test.txt最后一行追加:sed “$ a This is the end of file” test.txt - 删除: d
a. 删除test.txt第二行: sed ‘2d’ test.txt
b. 删除test.txt符合正则表达式/fish
的行: sed ‘/fish/d’ test.txt - 修改文本:s
a. 将text.txt中love替换为like: sed “s/love/like/g” test.txt (/g表示全局匹配) - 打印文本: p
a. 输出test.txt的第5-7行:sed -n ‘5,7p’ test.txt (-n的作用就显示出来了,可以去除-n查看效果)