syedasadali95 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I am trying to divide one huge file into two files using perl. My source file is something like this
######## file ########
1. command:read address:0xA
2. command:write address:0xB
3. writedata:0x12
4. writedata:0x34
5. writedata:0x56
6. writedata:0x78
7. command:read address:0xC
8. command:write address:0xD
9. writedata:0x9A
10. writedata:0xBC
11. writedata:0xDE
12. writedata:0xF0
######## file ########
Taking the above file as input I want to split the content into two different files based on address[0]=0/1. If address[0]=1, I want to print that line to one.txt else print that line to zero.txt no matter whether it is write/read. There is no problem to handle lines of read as it has only one line but the issue here is every write command is followed by next 4 lines of write data. How to print 4 write data lines followed by every write command line to both the output files.
######## code starts ######## while (my $line1 = <IN_FILE>) { if( ($line1 =~ /address/) ) { $line1 =~ /address:([a-z0-9-]+)\s+/; my $address = hex $1; $grep = substr( (sprintf "%b", $address), -1, 1); if($grep) { printf OUT_FILE1 ("$line1"); } else { printf OUT_FILE2 ("$line1"); } } } ######## code ends ########
I am struggling here for printing 4 write data lines because my while loop iterates per line and if I am using any loop inside my while loop to print the $line1 variable 4 times then it's printing the same line for 4 times
|
|---|