使用 shell 脚本将文件中的字符串替换为另一个具有特殊字符的字符串 [英] Using shell script to replace the string in file with another string having special character

查看:238
本文介绍了使用 shell 脚本将文件中的字符串替换为另一个具有特殊字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将以下文件中的字符串替换为另一个具有特殊字符(如 (=,",-) )的字符串.

I try to replace string in the following file with another string which is having special character like(=,",-) .

这是服务文件,我需要自定义并添加命名服务到这个文件重新启动

This is service file , i need to customize and add Naming Service to this file restart again

#!/bin/bash

. /etc/init.d/functions

PIDFILE=/var/run/Naming_Service.pid

PORT=

OPTIONS="-p ${PIDFILE}"

RETVAL=0

prog="Naming_Service"

    start() {
        echo -n $"Starting $prog: "
    
        if [ $UID -ne 0 ]; then
    
            RETVAL=1
    
            failure
    
        else
    
            setsid /usr/local/bin/Naming_Service ${OPTIONS} &
    
            RETVAL=$?
    
    
        fi
    
        echo 
    
        return $RETVAL
    
    }

在这个文件中我想替换第一次出现的

In this file I want to replace first occurrence of

OPTIONS=-p ${PIDFILE}"

OPTIONS="-p ${PIDFILE}"

替换为

OPTIONS_1234=-p ${PIDFILE_1234} -ORBEndpoint iiop://10.12.23.34:1234"

OPTIONS_1234="-p ${PIDFILE_1234} -ORBEndpoint iiop://10.12.23.34:1234"

OPTIONS_1235=-p ${PIDFILE_1235} -ORBEndpoint iiop://10.12.23.34:1235"

OPTIONS_1235="-p ${PIDFILE_1235} -ORBEndpoint iiop://10.12.23.34:1235"

OPTIONS_1236=-p ${PIDFILE_1236} -ORBEndpoint iiop://10.12.23.34:1236"

OPTIONS_1236="-p ${PIDFILE_1236} -ORBEndpoint iiop://10.12.23.34:1236"

我用 sed 命令编写了一个 shell 脚本文件,我遇到了特殊符号的问题.你能为此给出解决方案吗

I wrote one shell script file with sed command , I have issues with special symbols. can you please give solution for this

PORT_NS=13021

PIDFILE=/home/vagrant/Naming_Service_${PORT_NS}.pid

PIDFILE_13016=/home/vagrant/Naming_Service_${PORT_NS}.pid

ORIGINAL="OPTIONS="-p ${PIDFILE}""

REPLACE="OPTIONS_13016="-p ${PIDFILE_13016} -ORBEndpoint iiop://172.31.153.56:13016""

sed -i "0,/$ORIGINAL/s//$REPLACE/" "tao"

推荐答案

试试这个

sed '0,/\PIDFILE=\/var\/run\/Naming_Service.pid/ s//\PIDFILE_1234=\/var\/run\/Naming_Service_1234.pid\n\PIDFILE_1235=\/var\/run\/Naming_Service_1235.pid\n\PIDFILE_1236=\/var\/run\/Naming_Service_1236.pid\n /' script > newscript
sed '0,/\OPTIONS="-p ${PIDFILE}"/ s//\OPTIONS_1234="-p ${PIDFILE_1234} -ORBEndpoint iiop:\/\/10.12.23.34:1234"\n\OPTIONS_1235="-p ${PIDFILE_1235} -ORBEndpoint iiop:\/\/10.12.23.34:1235"\n\OPTIONS_1236="-p ${PIDFILE_1236} -ORBEndpoint iiop:\/\/10.12.23.34:1236"\n /' newscript > script

应该输出你需要的东西.

Should output what you need.

这篇关于使用 shell 脚本将文件中的字符串替换为另一个具有特殊字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆