Linux 文本三剑客 - sed

mervyn 2021年10月10日14:13:04LinuxLinux 文本三剑客 - sed已关闭评论2732

目录

sedstream editor 它是一种流编辑器,一次处理一行内容 。文章源自编程技术分享-https://mervyn.life/49e440ee.html

基本用法如下:文章源自编程技术分享-https://mervyn.life/49e440ee.html

# sed --help sed [OPTION]... {script-only-if-no-other-script} [input-file]...  # sed [参数] '范围 操作'  文件

例:文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -n '1,4 p' demo.txt  # 打印demo.txt文件1~4行的内容

选项参数常用

  • -n 使用安静(silent)模式。加上-n参数结束后,只能通过 sed 特殊处理行(或command)它将被列出。
  • -i 直接编辑原文件
  • -i.bak 直接编辑原文件,生成一个.bak备份文件。建议这样使用。防止误操作。
  • -f -f filename 可以执行filename内的 sed 命令
  • -e 多点编辑

范围选择

sed 可灵活查找相应范围的内容,常见范围选择如下:文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • 2 选择第二行
  • $ 选择最后一行
  • 2,5 选择 2~5行
  • 1~2 奇数行
  • 2~2 偶数行
  • 1,$ 第一行到文件的最后一行

另外 sed 例如:文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • /name/ 出现name 字符的行文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • /name/, 3 出现 name 后三行字符的行内容文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • /^xxx/ 选择已 xxx 开头的行文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • /xxx/,/yyy/ 出现 xxx 行及出现 yyy 行间数据文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • 常见的正则匹配表达式如下:文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • ^ 开始匹配行。 如: /^sed/ 以sed开头的行文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • $ 比如: /sed$/ 以sed结尾的行文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • . 单个字符文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • * 0或多个匹配文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • 一个或多个匹配文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • ? 0或1匹配文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • x\{m,n\} 重复连续字符x,m-n次数文章源自编程技术分享-https://mervyn.life/49e440ee.html

    • x\{m\} # 重复字符x,m二、如:/0{5}/匹配包含5个0(连续)行。文章源自编程技术分享-https://mervyn.life/49e440ee.html

常见操作

  • p 打印匹配的内容文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • d 参数与匹配的内容。 如需编辑原文件, 需要结合 -i-i.bak 参数文章源自编程技术分享-https://mervyn.life/49e440ee.html

  • w 将匹配的内容写入其他文件。 例:文章源自编程技术分享-https://mervyn.life/49e440ee.html

    sed -n '/xxx/ w output.txt' demo.txt  # 将demo.txt 文件中包含 xxx 写入行内容 output.txt

常用技巧

显示第一行的内容文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -n '1'p filename

显示第三行到最后一行的内容文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -n '3,$'p filename

显示匹配 name 关键的行文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -n '/name/'p

打印出现 <command> 行及出现 </command> 行间数据文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -n '/<command>/,/<\/command>/ p'  demo.txt

打印长度不小于5个字符的行文章源自编程技术分享-https://mervyn.life/49e440ee.html

 sed -n  '/^.\{5\}/ p' demo.txt

删除所有空行文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -e '/^$/ d' demo.txt

删除行首的空间文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -e 's/^[ \t]*//g' demo.txt

删除行尾空间文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -e 's/[ \t]*$//g' demo.txt

用双引号包围文件的每一行文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed  -e 's/.*/"&"/g' demo.txt

用单行代替文件的内容,同时每行过滤掉收尾空间,用双引号包围文章源自编程技术分享-https://mervyn.life/49e440ee.html

例如:文章源自编程技术分享-https://mervyn.life/49e440ee.html

abc文章源自编程技术分享-https://mervyn.life/49e440ee.html

123文章源自编程技术分享-https://mervyn.life/49e440ee.html

转为以下格式:文章源自编程技术分享-https://mervyn.life/49e440ee.html

"abc","123"文章源自编程技术分享-https://mervyn.life/49e440ee.html

sed -e 's/[ \t]*$//g' -e  's/^[ \t]*//g' -e '/^$/ d' -e 's/.*/"&"/g' demo.txt |tr "\n" "," |sed -e 's/,$//g'  # 还有一个相对简单的命令可以达到同样的目的,但是,当行内字符串中间有空格或换行符时,结果可能会有所不同 echo '"'`awk '{print $1}' demo.txt |xargs |sed 's/ /","/g'`'"'
文章源自编程技术分享-https://mervyn.life/49e440ee.html
weinxin
我的微信公众号
微信扫一扫
mervyn
Linux命令系列 - expect Linux

Linux命令系列 - expect

Expect是一个用来处理交互的命令。借助 expect,我们可以将交互过程写在一个脚本上,使之自动化完成。形象的说,ssh登录,ftp登录等都符合交互的定义。下文我们首先提出一个问题,然后介绍基础知...