1
<?xml version="1.0" encoding="UTF-8"?>
<Servers>
    <Putty>
        <Node Type="1">
            <SavedSession>Default Settings</SavedSession>
            <DisplayName>SAMPLE</DisplayName>
            <ServerName>SAMPLE</ServerName>
            <PuttyConType>4</PuttyConType>
            <Port>22</Port>
            <UserName>root</UserName>
            <Password>whatever</Password>
            <PasswordDelay>0</PasswordDelay>
            <CLParams>SAMPLE -ssh -P 22 -l root</CLParams>
            <ScriptDelay>0</ScriptDelay>
        </Node>
    </Putty>
</Servers>

This is the output from MTPuTTy in XML format.

I have a list of IPs in txt format one per line, what I want to is replace SAMPLE with each line so e.g. the list is like that

198.168.1.1
198.168.1.2
198.168.1.3

I want the output to be like that

<Node Type="1">
    <DisplayName>198.168.1.1</DisplayName>
    <ServerName>198.168.1.1</ServerName>
    <CLParams>198.168.1.1 -ssh -P 22 -l root</CLParams>
</Node>

<Node Type="2">
    <DisplayName>198.168.1.2</DisplayName>
    <ServerName>198.168.1.2</ServerName>
    <CLParams>198.168.1.2 -ssh -P 22 -l root</CLParams>
</Node>

<Node Type="3">
    <DisplayName>198.168.1.3</DisplayName>
    <ServerName>198.168.1.3</ServerName>
    <CLParams>198.168.1.3 -ssh -P 22 -l root</CLParams>
</Node>

I have omitted the lines that haven't changed for simplification.

How can I automate that?

1

You can use Regex Search and Replace.

Search for

^([0-9.]+\.([0-9]+))$

Replace with

<Node Type="$2">
    <DisplayName>$1</DisplayName>
    <ServerName>$1</ServerName>
    <CLParams>$1 -ssh -P 22 -l root</CLParams>
</Node>

Use Ctrl-Enter to enter newline.

  • I think this regex is better: ([1-9]{1,3}\.){3}[1-9] – John Stoneman Aug 18 '17 at 13:14
  • How is it better? How will you use it for substitution? What if one of the IPs is 192.168.1.33? – NetMage Aug 18 '17 at 17:09
  • Your one didn't work at all when I tested it. I meant ([1-9]{1,3}\.){3}[1-9]{1,3} – John Stoneman Aug 18 '17 at 17:29
  • I ran it in Sublime Text 3 build 3126 and it worked fine. Did you set Regex mode in the Find/Replace? – NetMage Aug 18 '17 at 17:33
  • 1
    I don't find most online systems a good way to test Regex especially when multiple lines are involved unless you are very aware of the nuances of various Regex engines. Since Sublime Text 3 is how the question is tagged, I tested in that. In regexr, for example, you need to turn on multiline and put in \n for the newlines in the replacement text: regexr.com/3gj6i – NetMage Aug 18 '17 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.