当前位置: 首页 > news >正文

Linux shell编程学习笔记75:sed命令——沧海横流任我行(下)

 

0 前言

 在

Linux shell编程学习笔记73:sed命令——沧海横流任我行(上)-CSDN博客文章浏览阅读684次,点赞32次,收藏24次。在大数据时代,我们要面对大量数据,有时需要对数据进行替换、删除、新增、选取等特定工作。在Linux中提供很多数据处理命令,如果我们要以行为单位进行数据处理,可以使用sed命令。https://blog.csdn.net/Purpleendurer/article/details/141307421?spm=1001.2014.3001.5501中,我们研究了sed的基础知识。

Linux shell编程学习笔记74:sed命令——沧海横流任我行(中)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/Purpleendurer/article/details/141369789?spm=1001.2014.3001.5501中,我们见识了sed删除和替换功能的威力。

现在我们通过一些实例来见识一下sed插入等功能的威力。

1 sed实列

1.1 插入行

1.1.1 前插

我们可以使用i命令(insert)来完成前插。 

1.1.1.1 在第3行、第4行前插入'abc'

命令为:

sed '3,4i\abc'

其中sed命令的参数说明如下:

3,4 :指定第3行、第4行

i:在前插入命令

\abc:要插入的字符串

[purpleendurer @ bash ~]  seq 7 | cat -n1  12  23  34  45  56  67  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed '3,4i\abc'1  12  2
abc3  3
abc4  45  56  67  7
[purpleendurer @ bash ~] 

1.1.1.2 在开头前插入'abc'

命令为:

sed -e '1i\abc'

其中sed命令的参数说明如下:

-e:解释脚本

1 :指定第1行

i:在前插入命令

\abc:要插入的字符串

[purpleendurer @ bash ~]  seq 7 | cat -n1  12  23  34  45  56  67  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed -e '1i\abc'
abc1  12  23  34  45  56  67  7
[purpleendurer @ bash ~] 

 

 

1.1.1.3 在所有包含1的行前插入'abc' 

命令为:

 sed  '/1*/i\abc'

[purpleendurer @ bash ~] seq 11 | cat -n 1  12  23  34  45  56  67  78  89  910  1011  11
[purpleendurer @ bash ~] seq 11 | cat -n | sed  '/1*/i\abc'
abc1  1
abc2  2
abc3  3
abc4  4
abc5  5
abc6  6
abc7  7
abc8  8
abc9  9
abc10  10
abc11  11
[purpleendurer @ bash ~] 

 

 1.1.2 追加(后插)

我们可以使用a命令(append)来完成追加(后插)。 

1.1.2.1在第3行及其后2行后追加'abc'

命令为:

sed '3,+2a\abc'

[purpleendurer @ bash ~]  seq 7 | cat -n1  12  23  34  45  56  67  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed '3,+2a\abc'1  12  23  3
abc4  4
abc5  5
abc6  67  7
[purpleendurer @ bash ~] 

 

1.1.2.2 在末尾追加'abc'

命令为:

sed '$a\abc'

[purpleendurer @ bash ~]  seq 7 | cat -n1  12  23  34  45  56  67  7
[purpleendurer @ bash ~]  seq 7 | cat -n | sed '$a\abc'1  12  23  34  45  56  67  7
abc
[purpleendurer @ bash ~] 

1.1.2.3 在偶数行后追加2行信息,第1行是abc,第2行是def

命令为:

sed  '0~2a\abc\
def
'

[purpleendurer @ bash ~] seq 5 | cat -n1  12  23  34  45  5
[purpleendurer @ bash ~] seq 5 | cat -n | sed  '0~2a\abc\
def'1  12  2
abc
def3  34  4
abc
def5  5
[purpleendurer @ bash ~] 

 

1.1.2.4 在所有包含1的行后面追加abc

命令为:

 sed '/1/aabc'

 

[purpleendurer @ bash ~] seq 10 | cat -n1  12  23  34  45  56  67  78  89  910  10
[purpleendurer @ bash ~] seq 10 | cat -n | sed '/1/aabc'1  1
abc2  23  34  45  56  67  78  89  910  10
abc
[purpleendurer @ bash ~] 

1.1.2.5 在只含有1个1的行后面追加abc 

命令为:

 sed '/1$/aabc'

[purpleendurer @ bash ~] seq 10 | cat -n1  12  23  34  45  56  67  78  89  910  10
[purpleendurer @ bash ~] seq 10 | cat -n | sed '/1$/aabc'1  1
abc2  23  34  45  56  67  78  89  910  10
[purpleendurer @ bash ~] 

 

1.2 打印行号

我们可以使用等号(=)来代表行号。

命令为:

 sed  '='

 

[purpleendurer @ bash ~] echo "aaa" >  t.txt
[purpleendurer @ bash ~] echo "bbb" >> t.txt
[purpleendurer @ bash ~] echo "ccc" >> t.txt
[purpleendurer @ bash ~] cat t.txt
aaa
bbb
ccc
[purpleendurer @ bash ~] sed '=' t.txt
1
aaa
2
bbb
3
ccc
[purpleendurer @ bash ~] 

http://www.lryc.cn/news/430470.html

相关文章:

  • 探索Scratch中的物理世界:碰撞与重力的编程之旅
  • 大模型重塑就医体验:医联MedGPT助力健康中国建设
  • TOMCAT全解
  • UDP+TCP
  • 分页查询面试记录和面试详情
  • Oracle 同义词SYNONYM 的实战使用
  • 实验11-1-8 查找子串
  • Git存储库添加空目录-添加占位文件
  • 基于x86 平台opencv的图像采集和seetaface6的人脸识别功能
  • Git 的基本使用
  • 如何解决 Cloudflare | 使用 Puppeteer 和 Node.JS
  • 笔记redis
  • Django 后端架构开发:手机与邮箱验证码接入、腾讯云短信SDK和网易邮箱
  • RAID 方案比较
  • 零成本搭建个人 APP 和小程序后台
  • LCP 633 平方数之和 [leetcode - 8]
  • c语言试题及答案
  • python实现链表
  • Linux Shell 编程基础入门(一)
  • 大白话讲微服务的灾难性雪崩效应
  • 配置 K3s 访问私有镜像仓库
  • 数学建模之数据分析【七】:对Pandas DataFrame 进行切片
  • 删除 Docker 容器的日志文件
  • DevOps入门(上)
  • 基于Python的网易民谣歌词数据分析的设计与实现
  • 趋动科技联合云轴科技推出GPU云原生超融合解决方案
  • Dooring智图,一款开箱即用的图片海报编辑器
  • 苹果macbook电脑怎么玩《黑神话:悟空》游戏教程来了
  • git 目录提交代码
  • 多商户商品下单限购问题修复