下文所有 1111  ,  2222 均为字符串

sed -i '/1111/i\2222' a.txt   在a.txt中找到所有符合1111得 前面加上2222

sed -i '/1111/a\2222' a.txt   在a.txt中找到所有符合1111得 后面加上2222

eg:下面得意思是  从iptables中查找有没有8181这个端口  如果没有找到COMMIT在COMMIT前一行加上8181这个端口

 

 grep "8181" /etc/sysconfig/iptables >/dev/null
		if [ $? != 0 ]; then
			 sed -i '/COMMIT/i\-A INPUT -m state --state NEW -m tcp -p tcp --dport 8181 -j ACCEPT' /etc/sysconfig/iptables
			echo "iptables 8181  create success!"
		else
	 	   echo "iptables 8181 already exist!"
		fi

 

文本中替换字符串 用 2222 替换文本中得1111

sed -i 's/1111/2222/g' a.txt

 

 

  

 

相关文章:

  • 2022-01-01
  • 2022-12-23
  • 2022-01-07
  • 2021-12-11
  • 2021-09-12
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-02-09
  • 2021-11-18
  • 2022-01-28
  • 2022-12-23
相关资源
相似解决方案