sed中在对内容进行修改时,有时候需要引用外部变量的值或者获取一个shell命令执行的结果,以便达到更加可观的输出结果

1、sed中使用变量替换
1)sed命令使用双引号的情况下,使用$var直接引用
[rooot@192 ~]$ cat test.txt
192.168.53.128/contiv/name
[rooot@192 ~]$ ip=192.168.53.77
[rooot@192 ~]$ sed -i "s/192.168.53.128/$ip/g" test.txt
[rooot@192 ~]$ cat test.txt
192.168.53.77/contiv/name
[rooot@192 ~]$
如果替换的变量内容中含有/符号则会提示错误,原因是从语法上看,没有任何问题;但由于变量中包含有“/”作为分隔符,这会和sed的替换操作的分隔符“/”引起混淆;所以,只要不使用“/”做分隔符就可以解决这个问题,如果使用“%”而不是“/”来作为sed的替换操作的分隔符,就不会出错。其实使用#或%或;作为分隔符也是可以的,只要不会与替换中有相同的而且不是元字符的特殊符号都是可以的
[rooot@192 chenwei]$ path=/home/root
[rooot@192 chenwei]$ cat test.txt
192.168.53.77/contiv/name
[rooot@192 chenwei]$ sed -i "s%192.168.53.77%$path%g" test.txt
[rooot@192 chenwei]$ cat test.txt
/home/root/contiv/name
[rooot@192 chenwei]$ sed -i "s#/home/root#192.168.53.77#g" test.txt
[rooot@192 chenwei]$ cat test.txt
192.168.53.77/contiv/name
[rooot@192 chenwei]$
2)sed命令使用单引号的情况下,使用'"$var"'引用,即变量用双引号括起来,外面再加上单引号
[rooot@192 chenwei]$ ip=192.168.0.34
[rooot@192 chenwei]$ cat test.txt
192.168.53.77/contiv/name
[rooot@192 chenwei]$ sed -i 's/192.168.53.77/'"$ip"'/g' test.txt
[rooot@192 chenwei]$ cat test.txt
192.168.0.34/contiv/name
[rooot@192 chenwei]$

2、sed中执行外部命令
1)sed命令使用单引号的情况下使用'`shell command`'或者'$(shell command)'引用命令执行的结果
[rooot@192 chenwei]$ cat test.txt
192.168.0.34/contiv/name
[rooot@192 chenwei]$ ip=192.168.0.56
[rooot@192 chenwei]$ sed -i 's/192.168.0.34/'`echo $ip`'/g' test.txt
[rooot@192 chenwei]$ cat test.txt
192.168.0.56/contiv/name
[rooot@192 chenwei]$
或者使用新式的命令
[rooot@192 chenwei]$ cat test.txt
192.168.0.56/contiv/name
[rooot@192 chenwei]$ ip=192.168.0.68
[rooot@192 chenwei]$ sed -i 's/192.168.0.56/'$(echo $ip)'/g' test.txt
[rooot@192 chenwei]$ cat test.txt
192.168.0.68/contiv/name
[rooot@192 chenwei]$
2.sed命令使用双引号的情况下直接`shell command`或者$(shell command)引用命令执行的结果
[rooot@192 chenwei]$ cat test.txt
192.168.0.68/contiv/name
[rooot@192 chenwei]$ ip=192.168.0.56
[rooot@192 chenwei]$ sed -i "s/192.168.0.68/$(echo $ip)/g" test.txt
[rooot@192 chenwei]$ cat test.txt
192.168.0.56/contiv/name

在sed语句里面,变量替换或者执行shell命令,双引号比单引号少绕一些弯子

3、一些小技巧

在每行的头添加字符,比如"HEAD",命令如下:
sed 's/^/HEAD&/g' test.file
在每行的行尾添加字符,比如“TAIL”,命令如下:
sed 's/$/&TAIL/g' test.file
1)"^"代表行首,"$"代表行尾
2)'s/$/&TAIL/g'中的字符g代表每行出现的字符全部替换,否则只会替换每行第一个,而不继续往后找了

4、直接修改文件的内容

直接编辑文件选项-i,会匹配test.txt文件中每一行的第一个This替换为this:
sed -i 's/This/this/' test.txt

5、shell变量的写法

${var} 变量var的值, 与$var相同

echo ${s1}${s2}   # 当然这样写 $s1$s2 也行,但最好加上大括号

6、shell支持逻辑与或的写法

[[]] 表达式
[root@localhost ~]# [ 1 -eq 1 ] && echo 'ok'
ok
[root@localhost ~]$ [[ 2 < 3 ]] && echo 'ok'
ok

[root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'
ok
注意:[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符:|| &&

shell脚本知识点汇总的更多相关文章

  1. Shell脚本使用汇总整理——达梦数据库备份脚本

    Shell脚本使用汇总整理——达梦数据库备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/9223477.html 脚本 ...

  2. Shell脚本使用汇总整理——mysql数据库5.7.8以前备份脚本

    Shell脚本使用汇总整理——mysql数据库5.7.8以前备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/92234 ...

  3. Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本

    Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本 Shell脚本使用的基本知识点汇总详情见连接: https://www.cnblogs.com/lsy-blogs/p/92234 ...

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

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

  5. Shell脚本使用汇总整理

    Shell脚本使用汇总整理 一.Shell脚本常用的头部格式: 头部的作用就是告知linux此脚本的类型: 常用的头部格式如下:(/bin/bash,是bash的路径,如果不知道路径可以通过which ...

  6. Shell脚本命令汇总中

    一.换行 echo -e 可以通过\n编译换行 echo -n不换行,如果加入了\n,则会打出“\n”字符 #!/bin/bash echo -e "O\nK\n!" echo & ...

  7. shell脚本语法基础汇总

    shell脚本语法基础汇总 将命令的输出读入一个变量中,可以将它放入双引号中,即可保留空格和换行符(\n) out=$(cat text.txt) 输出1 2 3 out="$(cat te ...

  8. shell脚本基本知识点

    Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.用户通过这个界面访问Linux操作系统内核的服务.Shell既是一种命令语言,又是一种程序设计语言. 1.Shell 环境 Shel ...

  9. shell脚本常用命令汇总

    一.shell脚本概述和入门 (1)shell脚本是一个命令行解释器,它接收应用程序/用户命令,然后调用操作系统内核 (2)shell脚本的常用执行方式: 第一种:采用bash或sh+脚本的相对路径或 ...

随机推荐

  1. Fireworks(whole page)

    <!DOCTYPE HTML> <html> <head> <title>Canvas 实现放烟花特效</title> <meta c ...

  2. C++(八)— 死锁原因及解决方法

    1.死锁原因 死锁问题被认为是线程/进程间切换消耗系统性能的一种极端情况.在死锁时,线程/进程间相互等待资源,而又不释放自身的资源,导致无穷无尽的等待,其结果是任务永远无法执行完成. 打个比方,假设有 ...

  3. struts2--Basic(一)

    Struts是流行和成熟的基于MVC设计模式的WEB应用程序框架. 帮助我们减少在运用MVC设计模式来开发Web应用的时间. 1.下载添加jar包 2. 准备配置文件 web.xml <filt ...

  4. 基于zepto使用swipe.js制作轮播图demo

    在移动web开发中,由于手机界面较小,为了能展示更多的图片经常使用轮播图并且还需要考虑到手机流量的问题,通过请教他人以及百度,个人感觉swipe.js比较好用 它是一个纯javascript工具,不需 ...

  5. luogu1801 黑匣子

    惊了呀 Splay Treap 这都什么玩意 两个优先队列搞定 #include <bits/stdc++.h> using namespace std; #define LL long ...

  6. BZOJ3680:吊打XXX

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...

  7. 查看linux上所有用户

    1.查看所有用户名 cat /etc/passwd |cut -f 1 -d #是1不是L的小写 2.显示用户信息 whoami 查看当前登录用户名. id username 查看用户的uid,gid ...

  8. redis安装及启动及设置

    1. 安装 1.1 下载解压包,直接解压到任意路径下即可 windows下载地址:ttps://github.com/MSOpenTech/redis/releases 2.启动 2.1 启动要先开启 ...

  9. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

  10. java流类

    总结:new FileInputStream package com.ds; import java.io.*; import com.da.fgbv; public class rter { pub ...