Linux脚本中利用sed修改文件内容的多种技巧

Linux脚本中利用sed修改文件内容

常用命令 功能
 a 插入,在当前行后添加一行或多行。多行时需要在每行末尾需用“\”续行(最后不需要)
 c 替换,用此符号后的新文本替换当前行中的文本。多行时需要在每行末尾需用“\”续行(最后不需要)
 d 删除,删除行
 i 插入,在当前行之前插入文本。多行时需要在每行末尾需用“\”续行(最后不需要)
 h 拷贝模板块的内容到内存中的缓冲区
 g 获得内存缓冲区的内容,并替代当前模板块中的文本
 l 列出非打印字符
 n 读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理
 p 打印行
 q 结束或退出sed
 r 从文件中读取输入行
 ! 对所选行以外的所有行应用命令
 s 替换,用一个字符串替换另一个。通常和正则表达式搭配使用:1,$ s/old/new/g
 g 在行内进行全局替换
 w 将所选的行写入文件
 x 交换暂存缓冲区与模式空间的内容
 y 将字符替换为另一字符(不能对正则表达式使用y命令)

常用选项 功能
 -e 进行多项编辑,即对输入行应用多条sed命令时使用
 -n 取消默认的输出,使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到屏幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来
 -f 指定sed脚本的文件名
 -i  直接修改读取的文件内容,而不是由屏幕输出 
 -r  sed的动作支持的是延伸型正则表达式的语法,想少用\  那你就用-r


直接上一个例子,说明方法:

[root@localhost test]# ls
test.sh
[root@localhost test]# vim test.sh 
#!/bin/sh


file="a.conf"
newip=$1

if [ $# != 1 ]; then
  echo "please use: $0 <ip>"
  exit
fi

content="
#config file

IPADDR:$newip
MASK:24
PRO:IPv4
"
#modify ip addr
if [ -e "$file" ]; then
  #file already exist, modify
  sed -i "s/^IPADDR:.*$/IPADDR:$newip/g" $file
else
  #file not found, new file
  echo "$content" > $file
fi

#delete all lines contain "AAA_add_info_into_line2"
sed -i '/AAA_add_info_into_line2/d' $file

#append info "AAA_add_info_into_line2" into line 2
sed -i '2a AAA_add_info_into_line2' $file

#append info "BBB_add_info_into_the_last_line" into the last line
sed -i '$a BBB_add_info_into_the_last_line' $file

#append info "CCC_add_info_into_file_after_the_fix_line" after the line start with "MASK:24"
sed -i '/^MASK:24/a CCC_add_info_into_file_after_the_fix_line' $file

#find all lines start with "MASK:24" and join with " #or 255.255.255.0 is valid."
sed -i 's/^MASK:24/& #or 255.255.255.0 is valid./' $file
~                                                                                                                                                                          

[root@localhost test]# ./test.sh 
please use: ./test.sh <ip>
[root@localhost test]# ./test.sh  1.1.1.1
[root@localhost test]# cat a.conf 

#config file
AAA_add_info_into_line2

IPADDR:1.1.1.1
MASK:24 #or 255.255.255.0 is valid.
CCC_add_info_into_file_after_the_fix_line
PRO:IPv4

BBB_add_info_into_the_last_line
[root@localhost test]# 
[root@localhost test]# ./test.sh 2.2.2.2
[root@localhost test]# 
[root@localhost test]# cat a.conf 

#config file
AAA_add_info_into_line2

IPADDR:2.2.2.2
MASK:24 #or 255.255.255.0 is valid. #or 255.255.255.0 is valid.
CCC_add_info_into_file_after_the_fix_line
CCC_add_info_into_file_after_the_fix_line
PRO:IPv4

BBB_add_info_into_the_last_line
BBB_add_info_into_the_last_line
[root@localhost test]# 


如果要替换一行中的某个字符串:

sed

[root@localhost test]# cat test.sh 
#!/bin/sh

file="a.conf"

sed -i "/bin\/mysqld\ \-\-initialize/s/\-\-basedir=[^ ]*\ /\-\-basedir=\/usr\/local\/mysql\ /g" $file

[root@localhost test]# 
[root@localhost test]# cat a.conf 
bin/mysqld --initialize  --user=mysql  --basedir=/abc/def  --datadir=/usr/xxx/yyy   >> /home/a.txt

tomcat  --basedir=/opt/tomcat/ xxxx
[root@localhost test]# 
[root@localhost test]# ./test.sh 
[root@localhost test]# 
[root@localhost test]# cat a.conf 
bin/mysqld --initialize  --user=mysql  --basedir=/usr/local/mysql  --datadir=/usr/xxx/yyy   >> /home/a.txt


tomcat  --basedir=/opt/tomcat/ xxxx
[root@localhost test]# 


替换一行中的一部分(非整行)
sed 's/被替换的字符串/新的字符串/g'
表达式支持正则

替换匹配行中的某个字符串
sed -i '/匹配字符串/s/替换源字符串/替换目标字符串/g' $file
# sed -i "/bin\/mysqld\ \-\-initialize/s/\-\-basedir=[^ ]*\ /\-\-basedir=\/usr\/local\/mysql\ /g" $file

# sed -n '/abc/p' $file | sed 's/abc/newABC/g'
# sed -n '/abc/p' $file | sed 's/abc//g'
直接在文件末尾插入一行内容"END"
# sed -i '$a END' $file

替换单引号的几种方法:
sed 's/'"'"'//g'
or
sed 's/'\''//g'
or
sed s/\'//g


  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值