简体   繁体   English

如何使用 groovy 脚本查找和替换字符串

[英]How to find and replace string using groovy script

I need to search some text from file and replace with other string using Groovy script.我需要从文件中搜索一些文本并使用 Groovy 脚本替换为其他字符串。 I am explaining my file below.我在下面解释我的文件。

test.yml:测试.yml:

devices:
  test-server:
    type: test1
    os: test
    tacacs:
      username: admin
    passwords:
      tacacs: admin
    connections:
      defaults:
        class: unicon.Unicon
      cli:
        protocol: ssh
        ip: 1.1.1.1
        port: 2024
      rest:
        protocol: http
        ip: 1.1.1.1
        port: 8080
        username: admin
        password: admin
  RFS1:
    type: test
    os: test
    tacacs:
      username: admin
    passwords:
      tacacs: admin
    connections:
      defaults:
        class: unicon.Unicon
      cli:
        protocol: ssh
        ip: 1.1.1.1
        port: 2024
      rest:
        protocol: http
        ip: 4.4.4.4
        port: 8080
        username: admin
        password: admin
  RFS2:
    type: test
    os: test
    tacacs:
      username: admin
    passwords:
      tacacs: admin
    connections:
      defaults:
        class: unicon.Unicon
      cli:
        protocol: ssh
        ip: 1.1.1.1
        port: 2024
      rest:
        protocol: http
        ip: 6.6.6.6
        port: 8080
        username: admin
        password: admin

Here I need to search the IP which is under devices:/test-server:/connections:/cli:/ip: 1.1.1.1 with some new charcter like ip:10.10.10.10 using groovy script. Here I need to search the IP which is under devices:/test-server:/connections:/cli:/ip: 1.1.1.1 with some new charcter like ip:10.10.10.10 using groovy script. I am using below code.我正在使用下面的代码。

def myFile = new File("test.yml") 
def fileText = myFile.text
fileText = (fileText =~ /ip:1.1.1.1/).replaceFirst("ip:10.10.10.10")
myFile.write(fileText)

Here my issue is its replacing the required string in whole file where ip:1.1.1.1 is present but I need to replace under devices:/test-server:/connections:/cli:/ip: 1.1.1.1 .在这里,我的问题是它在ip:1.1.1.1存在的整个文件中替换所需的字符串,但我需要在devices:/test-server:/connections:/cli:/ip: 1.1.1.1下替换。 Please help me to resolve this issue.请帮我解决这个问题。

A better way to do this is to simply do YAML parsing, manipulating the object, and saving back to the file.更好的方法是简单地进行 YAML 解析,操作 object,然后保存回文件。

Here's an example using Jackson:这是使用 Jackson 的示例:

@Grab(group='com.fasterxml.jackson.dataformat', 
      module='jackson-dataformat-yaml', 
      version='2.12.2')

def myFile = new File("test.yml")
def om = new com.fasterxml.jackson.databind.ObjectMapper(
             new com.fasterxml.jackson.dataformat.yaml.YAMLFactory());
def value = om.readValue(myFile, Map)
value['devices']['test-server']['connections']['cli']['ip'] = '10.10.10.10'

That replaces the value in the in-memory object.这将替换内存中 object 中的值。 You can then just save that back to a file, with something like:然后,您可以将其保存回文件,例如:

om.writeValue(myFile, value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM