美文网首页
sed命令使用(2)

sed命令使用(2)

作者: 冰舞one | 来源:发表于2018-12-09 09:13 被阅读0次

1、sed 命令的后项要引用取IP

[root@localhost scripts]# ifconfig enp0s3|grep 'inet '|sed -r 's#.*inet (.*) netmask.*$#\1#g'

192.168.0.3

[root@localhost scripts]#

[root@localhost scripts]# ifconfig enp0s3

enp0s3: flags=4163  mtu 1500

        inet192.168.0.3netmask 255.255.255.0  broadcast 192.168.0.255

.*inet  (.*)    \1               netmask.*$

    sed -r 's#.*inet (.*) netmask.*$#\1#g;上述颜色相同的代表内容统一;(.*)括号的内容就是选定的内容用 \1表示;若有两个从左向右的方向排序\1 \2……

-r 支持正则表达式

2、sed 查找内容并执行命令

查找/etc/passwd,找到root对应的行,执行后面花括号中的一组命令,每个命令之间用分号分隔,这里把bash替换为blueshell,再输出这行:

[root@localhost scripts]# sed -n '/root/{s/bash/blueshell/;p}' /etc/passwd

root:x:0:0:root:/root:/bin/blueshell

operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost scripts]#

3、sed 命令打印行

[root@localhost scripts]# sed   "=" color.sh

1

#!/bin/sh

2

RED_COLOR='\E[1;31m'

3

GREEN_COLOR='\E[1;32m'

4

YELLOW_COLOR='\E[1;33m'

5

4、sed 修改文件加备份加

-i:后跟备份成的文件名;注意:如果sed 跟多个参数进行文件备份 -i必须放到参数位的最后如果放到前面文件会备份成这样:

[root@localhost scripts]# sed -ir.2016.bak 's#^sed#s1#g' b.log #失败的案例 

[root@localhost scripts]# ls

b.log       b.logr.2016.bak

[root@localhost scripts]# cat b.log.2018.bak

11

root@localhost scripts]# sed -i.2018.bak 's#11#sedcmd#g' b.log

[root@localhost scripts]# ls b.*

b.log  b.log.2018.bak

[root@localhost scripts]# cat b.log.2018.bak

11

[root@localhost scripts]# cat b.log

sedcmd

相关文章

  • 六.Linux之sed命令

    sed使用方式 1.在Shell中输入: 脚本格式: 2.用sed -f命令调用sed脚本文件 3.直接执行sed...

  • sed命令使用(2)

    1、sed 命令的后项要引用取IP [root@localhost scripts]# ifconfig enp0...

  • LINUX sed命令的使用

    LINUX sed命令的使用 命令格式 sed常用命令 sed替换标记 sed元字符集 已匹配字符串标记& ⼦串匹...

  • sed命令引入变量的两种方法

    1.sed命令使用单引号的情况下,可以使用'"$var"'引用(单引号,然后双引号,变量): sed -i '2s...

  • Linux命令之sed批量替换字符串操作

    Linux中sed命令功能强大,本文将详细介绍如何使用sed命令进行字符串替换。一、基本的替换命令格式1:sed ...

  • 2019-11-15 sed删除:d命令

    删除:d命令 sed '2, sed ' sed '/test/'d example-----删除example文...

  • 070-sed

    想要编辑和修改文本内容。使用sed命令可以完美做到。非常有必要写一遍记录及查看。 sed 命令行格式为: sed ...

  • 2019-07-24 课堂笔记sed命令

    昨天回顾 使用grep命令取IP地址 今日总结 sed执行原理sed执行原理.jpg sed [参数] '条件指令...

  • Mac sed命令invalid command code错误

    问题 Mac上使用sed命令时,报出sed: 1: "1.txt": invalid command code ....

  • sed命令使用

    sed基本用法:sed: stream EDitor 行编辑器,(全屏编辑器:vi)。 sed: 模式空间 默...

网友评论

      本文标题:sed命令使用(2)

      本文链接:https://www.haomeiwen.com/subject/pezqhqtx.html