subnets, search/find and regular expression, txt2xml
-
Hello,
I would like to replace line by line.
e.g. txt file
3.5.140.0/255.255.252.0 15.190.244.0/255.255.252.0 15.230.15.76/255.255.255.254
with the following:
xml file:
<IPHost transactionid=""> <Name>n3.5.140.0</Name> <Description>subnet</Description> <IPFamily>IPv4</IPFamily> <HostType>Network</HostType> <IPAddress>3.5.140.0</IPAddress> <Subnet>255.255.252.0</Subnet> </IPHost> <IPHost transactionid=""> <Name>n15.190.244.0</Name> <Description>subnet</Description> <IPFamily>IPv4</IPFamily> <HostType>Network</HostType> <IPAddress>15.190.244.0</IPAddress> <Subnet>255.255.252.0</Subnet> </IPHost> <IPHost transactionid=""> <Name>n15.230.15.76</Name> <Description>ap-northeast-2</Description> <IPFamily>IPv4</IPFamily> <HostType>Network</HostType> <IPAddress>3.5.140.0</IPAddress> <Subnet>255.255.252.0</Subnet> </IPHost> <IPHost transactionid=""> <Name>15.230.15.76</Name> <Description>subnet</Description> <IPFamily>IPv4</IPFamily> <HostType>Network</HostType> <IPAddress>15.230.15.76</IPAddress> <Subnet>255.255.255.254</Subnet> </IPHost>
How is this possible ? search/find and regular expression?
Best regards, Tryf
—
moderator added code markdown around text; please don’t forget to use the
</>
button to mark example text as “code” so that characters don’t get changed by the forum -
@vasstr said in subnets, search/find and regular expression, txt2xml:
search/find and regular expression?
Yep.
FIND WHAT =
^(\d+\.\d+\.\d+\.\d+)/(\d+\.\d+\.\d+\.\d+)
REPLACE WITH =<IPHost transactionid="">\r\n <Name>n$1</Name>\r\n <Description>subnet</Description>\r\n <IPFamily>IPv4</IPFamily>\r\n <HostType>Network</HostType>\r\n <IPAddress>$1</IPAddress>\r\n <Subnet>$2</Subnet>\r\n</IPHost>
SEARCH MODE = Regular Expression
REPLACE ALLNotes:
^
makes sure the match must start at the beginning of a line- each
\d+\.\d+\.\d+\.\d+
match four dot-separated integers - The parens around those put them in two groups
- The
$1
and$2
in the replacement refer to the values of group1 and group2, respectively - the
\r\n
in the replacement are the standard windows CRLF end-of-line sequence, so that your replacement can go over multiple lines.
----
Useful References