Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Print multiple lines based on condition

by AnomalousMonk (Bishop)
on Mar 12, 2020 at 09:30 UTC ( #11114164=note: print w/replies, xml ) Need Help??


in reply to Print multiple lines based on condition

I don't really understand your requirements, but here's something that might serve to begin to narrow the range of possibilities. Note that I have used your example input file as is, including blank lines.

Source write_to_2_files_1.pl:

use strict; use warnings; use constant USAGE => <<"EOT"; usage: perl $0 file_in file_zero file_one where: file_in input file name file_zero output file name - address[0] bit == 0 file_one output file name - address[0] bit == 1 EOT die USAGE if @ARGV != 3; my ($file_in, $file_zero, $file_one) = @ARGV; open my $fh_in, '<', $file_in or die "opening '$file_in': $!"; open my $fh_0, '>', $file_zero or die "opening '$file_zero': $!"; open my $fh_1, '>', $file_one or die "opening '$file_one': $!"; my $fh_current; LINE: while (my $line = <$fh_in>) { next LINE unless $line =~ m{ \S }xms; # ignore blank lines my $got_command_addr = my ($rw_hex_addr) = $line =~ m{ \A \d+ [.] \s+ command: (?: read | write) \s+ address:0x ([[:xdigit:]]+) \s* \Z }xms; if ($got_command_addr) { $fh_current = 0x1 & hex $rw_hex_addr ? $fh_1 : $fh_0; } die "no command read/write address seen" unless $fh_current; print $fh_current $line; } close $fh_in or die "closing '$file_in': $!"; close $fh_0 or die "closing '$file_zero': $!"; close $fh_1 or die "closing '$file_one': $!"; exit; # subroutines ###################################################### # none for now
Invocation:
c:\@Work\Perl\monks\syedasadali95>perl write_to_2_files_1.pl file_in.t +xt zero.txt one.txt
Output zero.txt:
1. command:read address:0xA 7. command:read address:0xC
Output one.txt:
2. command:write address:0xB 3. writedata:0x12 4. writedata:0x34 5. writedata:0x56 6. writedata:0x78 8. command:write address:0xD 9. writedata:0x9A 10. writedata:0xBC 11. writedata:0xDE 12. writedata:0xF0

Update: Please note that it might have been helpful if you had provided expected output files for the OPed example input file. Also, please post input/output files and data, command lines and error messages as well as code within  <code> ... </code> tags. (Update: Please also see the update of this post by haukex which already touched on these points and included several very informative links.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Print multiple lines based on condition
by syedasadali95 (Acolyte) on Mar 12, 2020 at 11:58 UTC

    Sorry for not being clear with my question. My mistake. Here is the sample of input data I am operating on:

    ######################### Input file #############################

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca01 addr:0x10000a qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca0d addr:0x20000b qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca19 addr:0x30000c qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_WRSIZEDFULL tag:0x3b9acac1 addr:0xc10000a qospri:0 len:0xf noalloc:0

    chn:odat tag:0x3b9acac1 dat:0x3f80 be:0xffffffff

    chn:odat tag:0x3b9acac1 dat:0x3f81 be:0xffffffff

    chn:odat tag:0x3b9acac1 dat:0x3f82 be:0xffffffff

    chn:odat tag:0x3b9acac1 dat:0x3f83 be:0xffffffff

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca25 addr:0x40000d qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca31 addr:0x50000e qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_WRSIZEDFULL tag:0x3b9acacd addr:0xc20000b qospri:0 len:0xf noalloc:0

    chn:odat tag:0x3b9acacd dat:0x4f83 be:0xffffffff

    chn:odat tag:0x3b9acacd dat:0x9f85 be:0xffffffff

    chn:odat tag:0x3b9acacd dat:0x7f88 be:0xffffffff

    chn:odat tag:0x3b9acacd dat:0x5f87 be:0xffffffff

    Note: Please consider that above file doesn't have any blank lines and also not required in the output files.

    ########################### Output file1 ##########################

    Required output one.txt

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca0d addr:0x20000b qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca25 addr:0x40000d qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_WRSIZEDFULL tag:0x3b9acacd addr:0xc20000b qospri:0 len:0xf noalloc:0

    chn:odat tag:0x3b9acacd dat:0x4f83 be:0xffffffff

    chn:odat tag:0x3b9acacd dat:0x9f85 be:0xffffffff

    chn:odat tag:0x3b9acacd dat:0x7f88 be:0xffffffff

    chn:odat tag:0x3b9acacd dat:0x5f87 be:0xffffffff

    ############################ Output file2 ##########################

    Required output zero.txt

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca01 addr:0x10000a qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca19 addr:0x30000c qospri:0 len:0xf noalloc:0

    chn:req cmd:SDP_CMD_WRSIZEDFULL tag:0x3b9acac1 addr:0xc10000a qospri:0 len:0xf noalloc:0

    chn:odat tag:0x3b9acac1 dat:0x3f80 be:0xffffffff

    chn:odat tag:0x3b9acac1 dat:0x3f81 be:0xffffffff

    chn:odat tag:0x3b9acac1 dat:0x3f82 be:0xffffffff

    chn:odat tag:0x3b9acac1 dat:0x3f83 be:0xffffffff

    chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca31 addr:0x50000e qospri:0 len:0xf noalloc:0

    ######## Code starts ########

    while (my $line1 = <IN_FILE>) { if( ($line1 =~ /addr/) ) { $line1 =~ /addr:([a-z0-9-]+)\s+/; my $address = hex $1; if($address & 1 ) { printf OUT_FILE1 ("$line1"); } else { printf OUT_FILE2 ("$line1"); } } }

    ######## Code ends ########

    I was thinking to use the tag field to print the 4 write data lines just after the write command line since the tag field for write command and write data is unique and constant for every write transaction. RDBLKL = read, WRSIZEDFULL = write. Please ignore the blank lines in the input and output files I provided. I am no able to go to next line with out closing a paragraph in the writeup. Thanks in advance for the help.

      I am no able to go to next line with out closing a paragraph in the writeup.

      You are already using <code> tags for your code - use them also for your data. This is the second tip in Writeup Formatting Tips.

      Try this. Note that:

      • I'm using your example input and output files with blank lines already removed, but I think the code should work with blank lines in an input file (untested);
      • I'm comparing the generated output files to your example output files with the Windows fc (file compare) utility and there are no differences;
      • I'm using autodie to reduce the  ... or die ... noise, but this module only became part of core with Perl version 5.10, so you may have to put the noise back in;
      • This script is invoked in the same way as the previous one;
      • Tested under Perl version 5.8.9.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11114164]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2020-06-01 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (5 votes). Check out past polls.

    Notices?