|
Answer» Hi all
I have the following file ( router access list)
ip access-list extended access-1-1 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.5 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.6 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.4 permit ip host 172.16.8.6 host 192.168.1.10 deny ip any any log-input ip access-list extended access-1-2 permit tcp host 192.168.1.4 172.16.2.0 0.0.0.255 eq telnet permit tcp host 192.168.1.5 172.16.2.0 0.0.0.255 eq telnet permit tcp host 192.168.1.6 172.16.2.0 0.0.0.255 eq telnet permit tcp host 192.168.1.10 eq 443 host 172.16.8.6 permit tcp host 192.168.1.10 eq 443 host 172.16.9.142 deny ip any any log-input ip access-list extended access-2-1 permit ip host 172.16.12.4 host 192.168.1.11 permit ip host 172.16.12.4 host 192.168.1.13 permit ip host 172.16.12.4 host 192.168.1.14 deny ip any any log-input ip access-list extended access-2-2 permit tcp host 192.168.1.11 eq tacacs host 172.16.12.4 permit udp host 192.168.1.13 eq syslog host 172.16.12.4 permit tcp host 192.168.1.13 host 172.16.12.4 eq telnet permit tcp host 192.168.1.4 host 172.16.12.4 eq telnet deny ip any any log-input
I NEED to SEPARATE it into 4 files .txt . Each begins with the phrase "IP access-list extended" and then the name (access-1-1, 1-2 ... and so on).
all end with "deny ip any any log input"
i.e File access-1-1.txt
ip access-list extended access-1-1 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.5 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.6 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.4 permit ip host 172.16.8.6 host 192.168.1.10 deny ip any any log-input
any idea about a script that can do this??
Please help. Thanks.This looks possible with a FOR loop...if you can download Gawk for windows (see my SIG), here's an alternative. Code: [Select]C:\test>gawk "BEGIN{d=1}/deny/{print>d\".txt\";d++;next}{print>d\".txt\"}" file.txt
output: Code: [Select]C:\test>more 1.txt ip access-list extended access-1-1 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.5 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.6 permit ip 172.16.2.0 0.0.0.255 host 192.168.1.4 permit ip host 172.16.8.6 host 192.168.1.10 deny ip any any log-input
C:\test>more 2.txt ip access-list extended access-1-2 permit tcp host 192.168.1.4 172.16.2.0 0.0.0.255 eq telnet permit tcp host 192.168.1.5 172.16.2.0 0.0.0.255 eq telnet permit tcp host 192.168.1.6 172.16.2.0 0.0.0.255 eq telnet permit tcp host 192.168.1.10 eq 443 host 172.16.8.6 permit tcp host 192.168.1.10 eq 443 host 172.16.9.142 deny ip any any log-input
C:\test>more 3.txt ip access-list extended access-2-1 permit ip host 172.16.12.4 host 192.168.1.11 permit ip host 172.16.12.4 host 192.168.1.13 permit ip host 172.16.12.4 host 192.168.1.14 deny ip any any log-input
C:\test>more 4.txt ip access-list extended access-2-2 permit tcp host 192.168.1.11 eq tacacs host 172.16.12.4 permit udp host 192.168.1.13 eq syslog host 172.16.12.4 permit tcp host 192.168.1.13 host 172.16.12.4 eq telnet permit tcp host 192.168.1.4 host 172.16.12.4 eq telnet deny ip any any log-input
batch version: Code: [Select]@echo off & setlocal enabledelayedexpansion
set n=0 for /f "tokens=*" %%a in (ip.txt) do ( set a=%%a if "!a:~,2!"=="ip" set/a n+=1 & cd.>"File access-1-!n!.txt" >>"File access-1-!n!.txt" echo %%a )
type "File access-1*.txt"Quote from: gh0std0g74 on April 07, 2009, 06:37:13 PM if you can download Gawk for windows (see my sig), here's an alternative.
Bloody *censored*! Have you got shares in Gawk, Inc? (or a word that rhymes?) Quote from: Dias de verano on April 08, 2009, 12:58:22 AMBloody <censored>! Have you got shares in Gawk, Inc? (or a word that rhymes?)
you should just go fly a kite. Code: [Select]@echo off setlocal enabledelayedexpansion for /f "tokens=1-4* delims= " %%a in (input.txt) do ( echo %%b | find "access-list">NUL && ( set filename=%%d.txt echo Creating !filename! if exist "!filename!" del "!filename!" echo %%a %%b %%c %%d %%e>>"!filename!" ) echo %%b | find "access-list">nul || ( echo %%a %%b %%c %%d %%e>>"!filename!" ) ) Quote from: Dias de verano on April 08, 2009, 01:52:21 AMCode: [Select]@echo off setlocal enabledelayedexpansion for /f "tokens=1-4* delims= " %%a in (input.txt) do ( echo %%b | find "access-list">nul && ( set filename=%%d.txt echo Creating !filename! if exist "!filename!" del "!filename!" echo %%a %%b %%c %%d %%e>>"!filename!" ) echo %%b | find "access-list">nul || ( echo %%a %%b %%c %%d %%e>>"!filename!" ) )
dias, good eyes, i just notice that the filename is from the contents of txt file. i need a pair of good eyes too
you can actually combine the echo|find && || , INSTEAD of 2 seperate check. multiple call of echo|find does a quick job, but i find the trick is somehow slow for large files.
Quote from: Reno on April 08, 2009, 02:04:40 AMi find the trick is somehow slow for large files. indeed.
however...
Code: [Select]@echo off setlocal enabledelayedexpansion set true=1 set false=0 for /f "tokens=1-4* delims= " %%a in (input.txt) do ( set firstline=%false% echo %%b | find "access-list">nul && set firstline=%true% if "!firstline!"=="%true%" ( set filename=%%d.txt echo Creating !filename! if exist "!filename!" del "!filename!" echo %%a %%b %%c %%d %%e>>"!filename!" ) else ( echo %%a %%b %%c %%d %%e>>"!filename!" ) )
thank you all .... The scripts worked perfectly well Thanks again
|