[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Please help parsing file [sed, awk, fortran, bash]



lina wrote:
On Friday 31,August,2012 08:35 AM, Richard Owlett wrote:
daniel jimenez apparently described a *HOMEWORK* problem:
Hello all,

I need some help fixing the format of some pretty strangely
compressed data files. An example would be like this:

2883
452
07
16
2
4
6
107

Parsing rules:
The first two lines should be ignored.
The first column is the 'index', the second column being the
'counter'.
If there is no second number (ex. index=2), then the second
number should be set to '1'.
If there the index skips (ex. from index=2 to index=4), then
the indexes which where skipped should be set to '0'
Max index is 1024.

Here is:

$ more test
2883
452
0 7
1 6
2
4
6
10 7



#!/usr/bin/env perl

use strict;
use warnings;
use autodie qw(open close);

my $fn="test";

open my $fh, '<', $fn;

my %h;
my %full_h;

while(<$fh>){
	next;next;  ## to skip the first two lines
	my @pairs=split " ",$_;
	
	$h{$pairs[0]}=$pairs[1] if(scalar (@pairs)>1);
	$h{$pairs[0]}=1 if(scalar (@pairs) == 1);
	}

for (my $count=1; $count<=1024; $count++){
	$full_h{$count}=$h{$count} if exists $h{$count};
	$full_h{$count}=0 if(!exists $h{$count});
}

print map "$_ $full_h{$_}\n", sort keys %full_h;

__OUTPUT__

$ ./try
1 0
10 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0

I am not good at programming, so perhaps someone else will provide some
smart codes.


Best regards,


But should OP not do his own homework?

Or b i 1/2 century too olde?



Reply to: