0.sed 常用内部命令

a   在匹配后面添加
i 在匹配前面添加
p 打印
d 删除
s 查找替换
c 更改
y 转换 N D P

下面用a来演示

1.sed 'a 追加内容' 文件

# sed 'a newaddcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
newaddcontent
2 the quick brown fox jumps over the lazy dog.
newaddcontent
3 the quick brown fox jumps over the lazy dog.
newaddcontent

如果执行在第3行追加,则可

# sed '3a newaddcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
newaddcontent

如果要在第1行到第3行 追加则

# sed '1,3a newaddcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
newaddcontent
2 the quick brown fox jumps over the lazy dog.
newaddcontent
3 the quick brown fox jumps over the lazy dog.
newaddcontent
4 the quick brown fox jumps over the lazy dog.

但实际的使用中我们并不知道行号,此时可以使用匹配模式

sed '/搜索内容/ a 追加内容' t.txt

# sed '/2 the/ a newcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
newcontent

2.关于 d  删除命令

sed -r '/^#/d' ~/nginx.conf  #删除配置文件中以#开头的行号 -r为使用正则,//为匹配模式

sed -r '/(^#|#|^$)/d' ~/nginx.conf  #增强版 不仅仅是匹配 #开头 还匹配了 包含# 及 空格

3.替换

# sed 's dog cat ' t.txt
1 the quick brown fox jumps over the lazy cat.

# sed '/2 the/s/dog/cat/' t.txt   #匹配模式 找到2 the 这一行 然后替换
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.

4.整行替换

# sed 'c zhenghang' t.txt  #全部替换
zhenghang
zhenghang

# sed '2c zhenghang' t.txt  #仅替换第二行
1 the quick brown fox jumps over the lazy dog.
zhenghang
3 the quick brown fox jumps over the lazy dog.

# sed '2,3c zhenghang' t.txt  #替换第二到第三行
1 the quick brown fox jumps over the lazy dog.
zhenghang
4 the quick brown fox jumps over the lazy dog.

sed '/3 the/c zhenghang' t.txt  #匹配模式替换整行
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
zhenghang
4 the quick brown fox jumps over the lazy dog.

5.转换

# sed 'y abcdefg ABCDEFG ' t.txt  #将文中的前面的内容替换为后面的内容
1 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.

6.打印

# sed '2p' t.txt  #将第二行多打印一次 ,其余用法和上面类似
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.

7.标志位

  数字

  g

  p 

  w filename

如果要替换的内容有多个,默认会替换第一个,如果要指定替换某一个,则可以在后面加上指定的数字

# sed 's/dog/cat/' t2.txt    #默认替换第一个dog
1 the quick brown fox jumps over the lazy cat dog.

# sed 's/dog/cat/2' t2.txt  #在后面加上2则替换第二个dog
1 the quick brown fox jumps over the lazy dog cat.

# sed 's/dog/cat/g' t2.txt  #使用g参数则替换所有
1 the quick brown fox jumps over the lazy cat cat.

# sed '2s/dog/cat/w newfile' t.txt   #仅修改第二行并仅将第二行保存到新文件 newfile中
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy dog.

# cat newfile   #可见在newfile中只有第二行,原文件未受影响
2 the quick brown fox jumps over the lazy cat.

8.命令选项

-n 抑制输出

# sed '3s/dog/cat/' t.txt  默认会显示文本中的所有行,但是我们只想显示第3行
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.

# sed -n '3s/dog/cat/p' t.txt   #可配合p使用,达到仅显示要显示的内容

3 the quick brown fox jumps over the lazy cat.

-e 执行多条命令

# sed -e 's/dog/cat/;s/brown/green/' t.txt
1 the quick green fox jumps over the lazy cat.

-f 指定命令文件位置

可以向上面那样将书写一条命令完成任务,也可以将多条命令存放在一个文件中,再通过使用 -f 指定命令文件位置,来达到同样的效果

# cat mingling.txt
s/brown/green/
s/dog/cat/

# sed -f mingling.txt t.txt
1 the quick green fox jumps over the lazy cat.

-i 修改源文件

注意!!! 此操作不可逆,要慎重使用

如果不加-i 则仅仅是将文件读到内存中显示,并不会写回原文件中,所以需要通过使用-i参数来写回原文件

# sed -ie 's/dog/cat/;s/brown/green/' t.txt   #-i 保存到原文件 -s执行多条命令,此处为混合使用
# cat t.txt
1 the quick green fox jumps over the lazy cat.

可以在使用-i时,后面加上 -i.bak 在修改前先备份原文件为i.bak

sed -i.bak -e 's/dog/cat/;s/brown/green/' t.txt

此时命令执行结束后,会在当前目录生成一个文件名.bak的文件备份

9.使用管道符

# echo "tom is cool" | sed 's/tom/life/'
life is cool

10.小技巧

# sed -n '$=' t.txt  统计行数
5

# sed '=' t.txt  给每行加行号

1
1 the quick green fox jumps over the lazy cat.
2
2 the quick green fox jumps over the lazy cat.
3
3 the quick green fox jumps over the lazy cat.
4
4 the quick green fox jumps over the lazy cat.
5
5 the quick green fox jumps over the lazy cat.

# sed -n -r '/^(root)(.*)(bash)$/p' /etc/passwd      #使用正则表达式 -r ,显示passwd文件中root开头 bash结尾的信息
root:x:0:0:root:/root:/bin/bash

案例一:DNS监测WEB服务状态,并根据其状态实现高可用解析,场景:通过DNS进行单域名多条A记录解析做负载均衡。

#!/bin/bash
CP1=0
CP2=0
while :
do
#tong
ping -c1 192.168.18.240 > /dev/null
if [ $? -eq 1 ] && [ $CP1 -eq 0 ]
then
sed -i '/192.168.18.240/s/^/;/' /var/named/baidu.zone
/etc/init.d/named reload
CP1=1
fi
#butong
ping -c1 192.168.18.240 > /dev/null
if [ $? -eq 0 ] && [ $CP1 -eq 1 ]
then
sed -i '/192.168.18.240/s/;//' /var/named/baidu.zone
/etc/init.d/named reload
CP1=0
fi ping -c1 192.168.18.241 > /dev/null
if [ $? -eq 1 ] && [ $CP2 -eq 0 ]
then
sed -i '/192.168.18.241/s/^/;/' /var/named/baidu.zone
/etc/init.d/named reload
CP2=1
fi
ping -c1 192.168.18.241 > /dev/null
if [ $? -eq 0 ] && [ $CP2 -eq 1 ]
then
sed -i '/192.168.18.241/s/;//' /var/named/baidu.zone
/etc/init.d/named reload
CP2=0
fi sleep 5
done

记录 shell学习过程(10 ) shell 对文件的操作的更多相关文章

  1. shell脚本,在指定目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件。

    [root@localhost wyb]# cat test10.sh #!/bin/bash #使用for循环在/test10目录下通过随机小写10个字母加固定字符串oldboy批量创建10个htm ...

  2. [ Shell ] 通过 Shell 脚本导出 GDSII/OASIS 文件

    https://www.cnblogs.com/yeungchie/ 常见的集成电路版图数据库文件格式有 GDSII 和 OASIS,virtuoso 提供了下面两个工具用来在 Shell 中导出版图 ...

  3. linux 学习10 shell 基础

    10.1 Shell概述 .Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一 ...

  4. 如何使用shell脚本快速排序和去重文件数据

    前面写过一篇通过shell脚本去重10G数据的文章,见<用几条shell命令快速去重10G数据>.然而今天又碰到另外一个业务,业务复杂度比上次的单纯去重要复杂很多.找了很久没有找到相应的办 ...

  5. Linux 学习记录 四(Bash 和 Shell scirpt).

    一.什么是 Shell? 狭义的shell指的是指令列方面的软件,包括基本的Linux操作窗口Bash等,广义的shell则包括 图形接口的软件,因为图形接口其实也可以操作各种驱动程序来呼叫核心进行工 ...

  6. Centos7下crontab+shell脚本定期自动删除文件

    问题描述: 最近有个需求,就是rsync每次同步的数据量很多,但是需要保留的数据库bak文件 保留7天就够了,所以需要自动清理文件夹内的bak文件 解决方案: 利用shell脚本来定期删除文件夹内的任 ...

  7. Shell学习——子shell操作记录转储

    概述 主要介绍子shell历史操作记录的保存以及解析,比如python, scala等,用于(准)实时监控用户行为. 背景 一级shell的历史操作记录已由系统实现,当用户从开始登录shell(这里指 ...

  8. Shell语言系列之一:文件处理

    前言 &nbsp 标准输入/输出可能是软件工具设计原则里最基本的观念了.有很多UNIX程序都遵循这一设计历练.默认情况下,他们会读取标准输入,写入标准输出,并将错误信息传递给标准错误输出. & ...

  9. Linux Shell编程第5章——文件的排序、合并和分割

    目录 sort命令 sort命令的基本用法 uniq命令 join命令 cut命令 paste命令 split命令 tr命令 tar命令 sort命令 sort命令是Linux系统一种排序工具,它将输 ...

  10. Shell脚本使用汇总整理——文件夹及子文件备份脚本

    Shell脚本使用汇总整理——文件夹及子文件备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/9223477.html ...

随机推荐

  1. HUE下载HDFS文件时报ERR_CONNECTION_TIMED_OUT错误的解决办法

    1.故障描述 这是运行在公有云上的一套Hadoop集群,有一个公网IP将部分服务的端口映射出来供办公室访问. 数据分析师报告说:在HUE上面浏览HDFS文件,点击"download" ...

  2. 苹果Mac电脑永久路由的添加 & Mac 校园网连接教程

    学校校园网面向全校师生开放,无奈Windows用户基数大,学校只为Windows平台制作了内网连接工具,Mac平台资源较少,本人查阅相关资料后,总结整理出以下步骤,方便本校学生连接校园网.有永久路由添 ...

  3. Python学习零基础<入门必学>

    1. 注释注释 是任何存在于 # 号右侧的文字,其主要用作写给程序读者看的笔记. 2. 字面常量一个字面常量(Literal Constants)的例子是诸如 5.1.23 这样的数字,或者是如 这是 ...

  4. C语言关键词——register

    register修饰符暗示编译程序相应的变量将被频繁使用,如果可能的话,应将其保存在CPU的寄存器中,以加快其存储速度 例如:memcpy(des,src,i) { register char * d ...

  5. Socket通讯探索(二)-socket集群

    前面我们在章节“Socket通讯探索(一)”中如何实现一个tcp连接,但是这仅仅是一个最初级的BIO实现,且没有添加线程池,实际应用中很少采用这种方式,因为不得不考虑当大量的Tcp连接建立的时候,服务 ...

  6. 【剑指Offer】58:二叉树的下一个结点

    题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 题解一:递归 //既然给了二叉树的某个结点,且二叉树存 ...

  7. qt 带箭头的直线 (类似viso)

    2020.02.27 本来上传到CSDN,后来想想,我要放弃csdn了.csdn已经跟我分享的精神背道而驰了.想要代码,留邮箱吧. 近来Qt开发时可能遇到这样的需求:两个(或多个)矩形,要用直线将它们 ...

  8. Python的基本语法和数据类型(简明教程)

    声明:借鉴Python 简明教程 一.注释 注释: 就是对代码的解释 方便大家阅读python代码,在编辑器中快捷键:  notepad :ctrl + q    pycharm: ctrl + / ...

  9. demo 基于html css 实现小米官网部分内容搭建

    文件夹目录 mi-site-----------主文件夹 css------------css文件文件夹 fonts.css---矢量图标css index.css---主页面样式css reset. ...

  10. webpack4搭建vue多页面环境

    总结一下webpack4配置vue开发环境,本文不具体介绍webpack的基本概念和用途,如有不了解的请参见https://www.webpackjs.com/concepts/官网 一.webpac ...