I have tab delim file as follows
#file1
#version1.1
#columns with the information as follows
state1 class1 report_version_1.1 9428 4567 . . cal
+l=1;times=5;toss=head->tail;sno=A1B1;effect=positive
state1 class1 report_version_1.1 3862 4877 . . cal
+l=1;times=5;toss=head->tail;sno=A1B2;effect=negative
state1 class1 report_version_1.1 2376 4567 . . cal
+l=1;times=5;toss=head->tail;sno=;effect=positive
state2 class1 report_version_1.1 4378 2345 . . cal
+l=1;times=5;toss=tail->tail;sno=A1B3;effect=positive,negative, both
state2 class1 report_version_1.1 1289 4835 . . cal
+l=1;times=5;toss=head->tail;sno=;effect=positive
Note: There are no column headers in the file just the three top comments.
I am trying to parse out the 8 column(starting from call=1) which is basically a string separated by semi colons. I need to remove all the entries that have part1: no sno (value/name) (for eg: for 3 row sno=; i.e there is no record) and also part2: those that have same toss results i.e toss=tail->tail is not needed since both are tail.
Since I am learning perl I try and divide such things in parts and hence Here is what I have come up with so far for the part1: sno…
#!usr/bin/perl
use warnings;
#inputfile
my $input_file = "/Users/myfolder/myfile.txt";
die "Cannot open $input_file \n" unless (open(IN, $input_file));
#open file
my @LINES =<IN>;
#output file
#Open output file and write the needed results
die "output1.txt" unless(open( OUT,"> output1.txt"));
my @infos;
while(<IN>){
my @fields = split ';', $_;
my $state = $fields[1];
my $class1 = $fields[2];
my $report_version_1.1 = $fields[3];
my $value1 = $fields[4];
my $value2 = $fields[5];
my $dot1 = $fields[6];
my $dot2 = $fields[7];
my $info = $fields[8];
if ( $fields[8] =~ /^[sno]/ =~ /^[sno]/ ) {
push @infos, $_;
print OUT " $state\ $class\ $report_version_1.1\ $value1\ $value2\ $do
+t1 \ $dot2 \ $info\n";
}
}
exit;
Any other better ways to solve this for both sno and toss parts together?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.