linux 之 sed
一: sed 是什么?
sed 行编辑工具. sed , awk , grep 被称作文本三剑客.
二:sed 的命令语法
sed [option]... '地址 编辑命令' FILE...
optin 的选项有:
-n: 静默模式,不显示模式空间中的内容 -r: 支持使用扩展正则表达式 -i: 修改原文件; -e: sed -e "" -e "" -e "", sed "{COM1;COM2;COM3}"
填写地址方式:
行范围: start_line[, end_line] # sed -n '1,5p' test.txt #从第一行到第五行打印出来 # sed -n '1,$p' test.txt #从第一行到最后一行打印出来/patten1/,/patten2/: 第一次被pattern1匹配到的行开始,到第一次被pattern2匹配到的行结束之间的所有行; # sed -n '/\<and\>/,/\<or\>/p' test.txt 特定行: /pattern/ : 符合条件的特定行. # sed -n '/all/p' test.pp无地址:全文
编辑命令
编辑命令:命令可在之前加!取反 p:打印 d: 删除 i \text: 行上方,text即为插入的内容 a \text: 行下方,text即为插入的内容 r /path/from/some_file: 读入文件 w /path/to/some_file: 把符合条件的行保存至指定的文件中 =: 显示符合条件行的行号 s///: s@@@ g:为全局替换 s///g
一些常见的几个命令
‘s / \ . $ / / g’ 删除以句点结尾的. ‘-e /abcd/d’ 删除包含a b c d的行. ‘s / [ ][ ]* / [ ] / g’ 删除一个以上空格,用一个空格代替 此处的[] 指空格代替 ‘s / ^ [ ] [ ] * / / g’ 删除行首空格 ‘s / \ . [ ] [ ] * / [ ] / g’ 删除句点后跟一个或更多空格,代之以一个空格 ‘/ ^ $ / d’ 删除空行 ‘s / ^ . / / g’ 删除第一个字符 ‘s / ^ \ / / / g’ 从路径中删除第一个/ ‘S / ^ [ ] / / g’ 删除行首所有t a b键 ‘s / [ ] * / / g’ 删除所有t a b键
三:增删换显几个练习
例:cat 1.txt
cat 1.txt12332###DISO###45.12^M 00332###LPSO###23.14^M 01299###USPD###34.46^M把所有是#的内容替换掉 sed 's/##*//' 1.txt12332DISO45.12^M 00332LPSO23.14^M 01299USPD34.46^M显示第2行内容sed -n '2p' 1.txt 00332###LPSO###23.14^M删除所有行首是0 的行内容 sed /^00*/d 1.txt 12332###DISO###45.12^M替换行首是0的内容为1 sed 's/^0/1/g' 1.txt 12332###DISO###45.12^M 10332###LPSO###23.14^M 11299###USPD###34.46^M替换行尾是特殊字符^M sed 's/\^M$//g' 1.txt 12332###DISO###45.12 00332###LPSO###23.14 01299###USPD###34.46操作集合 cat 1.txt |sed 's/##*//g' | sed 's/\^M//g' | sed '2d' | sed '/^0/i hello' | sed '/^0/a word' | sed '1p' 12332DISO45.12 12332DISO45.12 hello 01299USPD34.46 word看到前两行是同样的内容输出,如果我们想要输出匹配的一行. 用-n 参数; 如果想要让上面的内容生效用-i 参数
四:打印行号
例 :cat 2.txt
cat 2.txt The honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance.sed -e '/was/=' 2.txtThe honeysuckle band played all night long for only $90. 2 It was an evening of splendid art and company. The office Dibble art played well. 4 The local nurse music P.Neave was in attendance.如果只关心行号 把-e换成-n只显示匹配到的行和行号 sed -n -e '/was/p' -e '/was/=' 2.txtIt was an evening of splendid art and company. 2 The local nurse music P.Neave was in attendance. 4
五:读入文件内容,写入文件内容
cat 1.txt 12332###DISO###45.12^M 00332###LPSO###23.14^M 01299###USPD###34.46^Mcat 2.txtThe honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance.把2.txt中的内容读入1.txt第一行下面 sed '1r 2.txt' 1.txt12332###DISO###45.12^MThe honeysuckle band played all night long for only $90. It was an evening of splendid art and company. The office Dibble art played well. The local nurse music P.Neave was in attendance. 00332###LPSO###23.14^M 01299###USPD###34.46^M把1.txt第1行写入test.txt中 sed '1w test.txt' 1.txtcat test.txt 12332###DISO###45.12^M
六:在内容后面插入内容
例 :cat 3.txt
cat 3.txt AC456 AC492169 AC9967 AC88345在数字最后插入 passwdsed 's/[0-9][0-9]*/& passwd/g' 3.txt sed 's/[0-9][0-9]*/& passwd/g' 3.txtAC456 passwd AC492169 passwd AC9967 passwd AC88345 passwd
转载于:https://blog.51cto.com/slayer/1664248
linux 之 sed相关推荐
- linux shell sed awk 命令(2)-awk
linux shell sed awk 命令(2)-awk awk语法格式: awk [选项] -f program-file [ -- ] file ... 选项: -F fs, --field-s ...
- linux中往sed命令,Linux中Sed命令怎么用?
Linux中Sed命令怎么用? 发布时间:2020-05-26 17:14:39 来源:亿速云 阅读:245 作者:鸽子 Sed介绍:sed是文本处理工具,读取文本内容,根据指定的条件进行处理如删除. ...
- Linux下sed命令替换配置文件中某个变量的值(改变包含字符的一行的值)之二——只改变第一出现的那一行
一.背景 在之前的文章中有介绍过<Linux下sed命令替换配置文件中某个变量的值(改变包含字符的一行的值)> 但是这种方法存在一定的问题,就是假如某个变量在一个文件中出现两次,却只想更改 ...
- sed是linux命令吗,Linux命令 sed
一. 以行为单位进行操作. d:删除 $ nl passwd | sed '2,5d' # 删除第2~5行 $ nl passwd | sed '2d' # 删除第2行 $ nl passwd | ...
- 批量修改linux换行格式,linux中sed命令批量修改
sed命令下批量替换文件内容 格式: sed -i "s/查找字段/替换字段/g" `grep 查找字段 -rl 路径` 文件名 -i 表示inplace edit,就地修改文件 ...
- linux中sed和find,Linux运维知识之Linux 之 sed 与 find 命令结合使用
本文主要向大家介绍了Linux运维知识之Linux 之 sed 与 find 命令结合使用,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. sed 与 find 命令结合使用 目 ...
- linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号
linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号 实现的功能是吧换行符换成逗号了,自己试验过. 求解释,:t N b t 都是什么意思??? :t 定义la ...
- linux sed替换文件,linux的sed命令替换文件
linux下的sed是一个强大的编辑器工具,下面由学习啦小编为大家整理了linux的sed命令替换文件的相关知识,希望对大家有帮助! linux的sed命令替换文件 sed在Linux下是个强大的工具 ...
- Linux 使用 sed 整行(列)刪除
Linux 使用 sed 整行(列)刪除 Posted on 2009 年 03 月 26 日 by Tsung 垂直刪除有 Vim, cut 和 awk 可以用, 橫列刪除的倒是比較少用, 可 ...
- 【Linux脚本-sed命令在文本首行和尾行插入空行】
@[TOC]Linux脚本-sed命令在文本首行和尾行插入空行 Linux脚本-sed命令在文本首行和尾行插入空行 演示如下: 首先创建需要操作的文本文件 //create a file contai ...
最新文章
- CIO时代学院院长姚乐:传统行业遇上大数据 拥抱智能化未来
- 物联网趋势下的边缘计算发展解析
- each 数据获取attr_调用高德POI数据,带你玩转长沙
- python3.82版本安装_CentOS7下安装Python3和Python2并存
- php微服务架构设计模式,《微服务架构设计模式》读书笔记---第十一章:开发面向生产环境的微服务应用...
- 模糊pid控制的温度系统matlab源代码_变风量空调模糊 PID 控制系统的仿真研究
- 安卓开发设置全屏隐藏标题栏
- JavaScript语言核心(五)-- 异步 async/await
- 微服务与架构师的个人见解
- Windbg是一款非常好用的经典windows系统蓝屏分析修复工具
- Meshing Tutorials(网格划分教程)
- citespace三天写论文!图表调整实战
- 自学软件测试怎么学?【史上最详细学习路线】(附全套资料)
- imx6ul can调试(代码)
- 解决U盘使用DVD镜像安装Fedora15需要联网的问题
- 如何用将top命令的结果写入txt?
- jsBlob数据转为图片
- 【Cover Letter 】SCI 投稿加分必备,手把手教你写 投稿Cover Letter
- 仿真4. 仿真平台架构设计
- 打补丁的日子,比写代码的日子难熬多了