duanshan5259 2015-11-26 11:14
浏览 299

使用golang替换.inc文件中的特定行

I have a file named aab.inc and i have a line $VP_Name="Some Messages" I wrote a code in golang so that it reads the file aab.inc and Removes any line that has a word VP_Name and in my code i also included that That line(which contains the word VP_Name) is replaced with $VP_Name="Alternate Message" but my problem is it never deletes the line if i give the word as $VP_Name and it replaces the line as ="Alternate Message" in the place if I give the replacing word as $VP_Name="Alternate Message" . But it deletes the line if give the word as VP_Name alone(Without $) and replaces the line as VP_Name="Alternate Message" if the replacing text is given without the $ symbol.

My code is as follows

package main

import "fmt"
import "regexp"
import "os"
import "io/ioutil"
func main() {

    ConfigFile1, err := os.Open("aab.inc")
         if err != nil {
                 fmt.Println("Error opening file:", err)
                 return
         }
        defer ConfigFile1.Close()

    ConfigFiledata1, _ := ioutil.ReadAll(ConfigFile1)
    //Try with $VP_Name
    re := regexp.MustCompile("(?m)[
]+^.*VP_Name.*$")
    // Actual Word is $VP_Name="Alternate Message" 
    res := re.ReplaceAllString(string(ConfigFiledata1), "
VP_Name=\"Alternate Message\"")

    fmt.Println(res)    

    out := []byte(res)
    if err := ioutil.WriteFile("aab_final.inc",out, 0666); err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
}

The new replaced line should come in the next line should not append with any other lines but even i use before the replacing text the result is as below

Output

$VP_Roll="";VP_Name="Alternate Message";
$VP_Failure="Yes";

What is the Problem in including the $ in the code and why the text is not replaced in a new line even was specified.

Thanks in Advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图