Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Print multiple lines based on condition

by kcott (Archbishop)
on Mar 13, 2020 at 04:54 UTC ( [id://11114202]=note: print w/replies, xml ) Need Help??


in reply to Print multiple lines based on condition

G'day syedasadali95,

Based on your description here, and your updated data, here's pm_11114156_record_collate.pl:

#!/usr/bin/env perl use strict; use warnings; use autodie; my ($file_in, $file_out_0, $file_out_1) = qw{ pm_11114156_record_collate_in.txt pm_11114156_record_collate_out_0.txt pm_11114156_record_collate_out_1.txt }; my (@out_fhs, $out_fh); my $re = qr{\saddr:(0x[0-9a-f]+)\s}; open my $in_fh, '<', $file_in; open $out_fhs[0], '>', $file_out_0; open $out_fhs[1], '>', $file_out_1; while (<$in_fh>) { if (/$re/) { $out_fh = $out_fhs[hex $1 & 1]; } print $out_fh $_; }

And here's a sample run:

ken@titan ~/tmp $ cat pm_11114156_record_collate_out_0.txt cat: pm_11114156_record_collate_out_0.txt: No such file or directory ken@titan ~/tmp $ cat pm_11114156_record_collate_out_1.txt cat: pm_11114156_record_collate_out_1.txt: No such file or directory ken@titan ~/tmp $ cat pm_11114156_record_collate_in.txt chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca01 addr:0x10000a qospri:0 len:0 +xf noalloc:0 chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca0d addr:0x20000b qospri:0 len:0 +xf noalloc:0 chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca19 addr:0x30000c qospri:0 len:0 +xf 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:0 +xf noalloc:0 chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca31 addr:0x50000e qospri:0 len:0 +xf 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 ken@titan ~/tmp $ ./pm_11114156_record_collate.pl ken@titan ~/tmp $ cat pm_11114156_record_collate_out_0.txt chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca01 addr:0x10000a qospri:0 len:0 +xf noalloc:0 chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca19 addr:0x30000c qospri:0 len:0 +xf 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:0 +xf noalloc:0 ken@titan ~/tmp $ cat pm_11114156_record_collate_out_1.txt chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca0d addr:0x20000b qospri:0 len:0 +xf noalloc:0 chn:req cmd:SDP_CMD_RDBLKL tag:0x3b9aca25 addr:0x40000d qospri:0 len:0 +xf 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 ken@titan ~/tmp $

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found