Jump to content

generate a script which search and replace a string from the file


Recommended Posts

Hi guys !

I'm having two text files: A.txt and B.txt.

In A.txt file I'm having string1 which must be replaced with string2 from the B.txt file.

Also the string2 from A will be replaced with a string from B in array, and so on.

The script must find in the B file corresponding string for A to be replaced.

I want to build such a script which will create another C file with the result.

Can you help me with example or a code for this ?

Thank you for the answer

Link to comment
Share on other sites

First you need to read in the files to an array using FileRead + StringSplit or _FileReadToArray.

Next you create a new array for the 3rd file.

From there you can use FileOpen + FileWriteLine or _FileWriteFromArray to output the new file.

Link to comment
Share on other sites

And how do I search in the file ?

To be more specific : I have one file containing list of IP's and the other file containing assigned MAC addresses for each IP. I need the script to generate the 3rd file in the same format as first file (with IP's) but instead of IP to have the MAC.

Here is a fragment from the file with IPs:

HA#show ip mobile binding
Mobility Binding List:
Total 60
172.18.6.15: 
    Care-of Addr 172.18.3.21, Src Addr 172.18.3.21
    Lifetime granted 01:00:00 (3600), remaining 00:48:39
    Flags sbdmG-T-, Identification CC0F35FE.5F756C16
    Tunnel1 src 172.18.3.5 dest 172.18.3.21 reverse-allowed
    Routing Options - (G)GRE (T)Reverse-tunnel
172.18.6.14: 
    Care-of Addr 172.18.3.21, Src Addr 172.18.3.21
    Lifetime granted 01:00:00 (3600), remaining 00:51:16
    Flags sbdmG-T-, Identification CC0F369B.F7C3BE6B
    Tunnel1 src 172.18.3.5 dest 172.18.3.21 reverse-allowed
    Routing Options - (G)GRE (T)Reverse-tunnel
172.18.6.12: 
    Care-of Addr 172.18.3.21, Src Addr 172.18.3.21
    Lifetime granted 01:00:00 (3600), remaining 00:48:27
    Flags sbdmG-T-, Identification CC0F35F2.BE3DD5C0
    Tunnel1 src 172.18.3.5 dest 172.18.3.21 reverse-allowed

A fragment from the file with which MAC for is for IP:

host MSS11 {
hardware ethernet 00:19:CB:70:24:B5;
fixed-address 172.18.6.11;
}

host MSS12 {
hardware ethernet 00:19:CB:25:42:11;
fixed-address 172.18.6.12;
}

host MSS13 {
hardware ethernet 00:19:CB:3A:71:83;
fixed-address 172.18.6.13;
}

host MSS14 {
hardware ethernet 00:19:CB:14:92:B1;
fixed-address 172.18.6.14;
}

host MSS15 {
Link to comment
Share on other sites

;Read in file containing only IP addresses
$file1 = FileRead("file1.txt")

;Read in file containing IP addresses and associated MAC addresses
$file2 = FileRead("file2.txt")

$file3 = $file1

;Retrieve all IP addresses from File 1
$array1 = StringRegExp($file1, "\d+\.\d+\.\d+\.\d+",3)
For $X = 0 to Ubound($array1)-1
    ConsoleWrite("["&$X&"]: " & $array1[$X] & @CRLF)
    
    ;Retrieve MAC address associated with IP address
    $match = "hardware ethernet (.*);" & @CRLF & "fixed-address " & $array1[$X] & ";"
    $result = StringRegExp($file2, $match,3)
    If NOT @ERROR Then
        For $Y = 0 to Ubound($result)-1
            ConsoleWrite("["&$Y&"]: " & $result[$Y] & @CRLF)
        Next
        
        ;Perform replacement
        $file3 = StringReplace($file3, $array1[$X], $result[0])
    Else
        ConsoleWrite("No Match" & @CRLF)
    EndIf
Next

$handle = FileOpen("file3.txt", 2)
FileWrite($handle,$file3)
FileClose($handle)

Link to comment
Share on other sites

Thank you very much. It is a long process of execution - due to my long txt files, but it looks that is working.

For once, at the end I will do a double-check.

It probably will be slow with large files. You can cut back some time by removing:

$file3 = $file1

Change:

$file3 = StringReplace($file3, $array1[$X], $result[0])

to

$file1 = StringReplace($file1, $array1[$X], $result[0])

Change:

FileWrite($handle,$file3)

to

FileWrite($handle,$file1)

Link to comment
Share on other sites

I want to delete from file3.txt the following lines, of this kind :

Care-of Addr 172.18.3.21, Src Addr 172.18.3.21

Lifetime granted 01:00:00 (3600), remaining 00:48:39

Flags sbdmG-T-, Identification CC0F35FE.5F756C16

Tunnel1 src 172.18.3.5 dest 172.18.3.21 reverse-allowed

Routing Options - (G)GRE (T)Reverse-tunnel

Shall I use _FileWriteToLine ? Or how would you do that ?

Edited by mentosan
Link to comment
Share on other sites

MR. WeaponX, can you help me with this last wish ?

Wouldn't that just leave you with:

HA#show ip mobile binding
Mobility Binding List:
Total 60
00:19:CB:25:42:XX:
00:19:CB:14:92:B1:
00:19:CB:25:42:11:
Link to comment
Share on other sites

This is untested but should work:

;Read in file containing only IP addresses
$file1 = FileRead("file1.txt")

;Read in file containing IP addresses and associated MAC addresses
$file2 = FileRead("file2.txt")

$handle = FileOpen("file3.txt", 2)

;Retrieve all IP addresses from File 1
$array1 = StringRegExp($file1, "\d+\.\d+\.\d+\.\d+",3)
For $X = 0 to Ubound($array1)-1
    ConsoleWrite("["&$X&"]: " & $array1[$X] & @CRLF)
   
    ;Retrieve MAC address associated with IP address
    $match = "hardware ethernet (.*);" & @CRLF & "fixed-address " & $array1[$X] & ";"
    $result = StringRegExp($file2, $match,3)
    If NOT @ERROR Then
      
        ;Write MAC Address to file
        FileWriteLine($handle, $result[0])
    Else
        ConsoleWrite("No Match" & @CRLF)
    EndIf
Next
Link to comment
Share on other sites

It will work with this result :

00:19:CB:25:42:XX:
00:19:CB:14:92:B1:
00:19:CB:25:42:11:

I want to include the "header" as well :

HA#show ip mobile binding

Mobility Binding List:

Total 60

Edited by mentosan
Link to comment
Share on other sites

I have found a useful tool which does exactly what WeaponX posted, with very fast execution time - less than a minute. It is called usfutlight. Still, I prefer to use the code sent by weaponX if there is a solution not to cut the header (as mentioned in previous post)

Link to comment
Share on other sites

We don't really have a metric to define the header.

Is it always the same?:

HA#show ip mobile binding

Mobility Binding List:

Total 60

Is it always three lines? If it is you can do this:

;Read in file containing only IP addresses
$file1 = FileRead("file1.txt")

;Read in file containing IP addresses and associated MAC addresses
$file2 = FileRead("file2.txt")

;Open file for writing
$handle = FileOpen("file3.txt", 2)

;Grab first three lines (header)
$result = StringRegExp($file1, "\A(.*\r\n.*\r\n.*\r\n)", 3)
#cs
For $Y = 0 to Ubound($result)-1
    ConsoleWrite("["&$Y&"]: " & $result[$Y] & @CRLF)
Next
#ce

;Write header to file
FileWrite($handle,$result[0])

;Retrieve all IP addresses from File 1
$array1 = StringRegExp($file1, "\d+\.\d+\.\d+\.\d+",3)
For $X = 0 to Ubound($array1)-1
    ConsoleWrite("["&$X&"]: " & $array1[$X] & @CRLF)
   
    ;Retrieve MAC address associated with IP address
    $match = "hardware ethernet (.*);" & @CRLF & "fixed-address " & $array1[$X] & ";"
    $result = StringRegExp($file2, $match,3)
    If NOT @ERROR Then
        #cs
        For $Y = 0 to Ubound($result)-1
            ConsoleWrite("["&$Y&"]: " & $result[$Y] & @CRLF)
        Next
        #ce
        
        ;Write MAC address to file
        FileWriteLine($handle,$result[0])
    Else
        ConsoleWrite("No Match" & @CRLF)
    EndIf
Next

FileClose($handle)
Edited by weaponx
Link to comment
Share on other sites

In fact there are five lines, always. But it must be shown with every iteration, because the time is displayed.

So it should look like this :

HA#show calendar
12:00:07 EET Fri Jun 27 2008
HA#show ip mobile binding
Mobility Binding List:
Total 3
00:19:CB:25:42:XX:
00:19:CB:14:92:B1:
00:19:CB:25:42:11:

HA#show calendar
12:01:07 EET Fri Jun 27 2008
HA#show ip mobile binding
Mobility Binding List:
Total 3
00:19:CB:25:XX:ZZ:
00:19:CB:14:YY:YY:
00:19:CB:25:ZZ:ZZ:

not :

HA#show calendar
12:00:07 EET Fri Jun 27 2008
HA#show ip mobile binding
00:19:CB:25:42:XX:
00:19:CB:14:92:B1:
00:19:CB:25:42:11:
00:19:CB:25:XX:ZZ:
00:19:CB:14:YY:YY:
00:19:CB:25:ZZ:ZZ:
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...