linux shell大文件操作

查找字符串所在行 : grep -n “待查找字符串” “文件名”

显示指定行信息:sed -n '1,5p' “指定文件”  表示显示指定文件第一至五行的信息

--------------------------------------------------------------------------

sed关键Options介绍:

  • -n : 安静模式。一般sed用法中,所有来自STDIN的数据都会被输出到屏幕上,使用-n只有被sed处理的行才会列出来。如果不使用-n,使用sed打印时,会把输入流和处理的信息都打印一遍
  • a:append,追加文本
  • i:insert,插入文本
  • d:delete,删除文本
  • s: 模式匹配替换
  • p:打印文本

sed使用示例

  1. 在指定行插入或追加: 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
  2. 删除: d 
    a. 删除test.txt第二行: sed ‘2d’ test.txt 
    b. 删除test.txt符合正则表达式/fish的行: sed ‘/fish/d’ test.txt
  3. 修改文本:s 
    a. 将text.txt中love替换为like: sed “s/love/like/g” test.txt (/g表示全局匹配)
  4. 打印文本: p 
    a. 输出test.txt的第5-7行:sed -n ‘5,7p’ test.txt (-n的作用就显示出来了,可以去除-n查看效果)

猜你喜欢

转载自my.oschina.net/u/3732258/blog/1927797