On Wed, Mar 7, 2012 at 1:53 PM, Shrinivasan T <tshriniva...@gmail.com>wrote:

> The same block continues as multiple times in the file, but in records
> are in different order.
>

Is there a fixed set of 'fields' per record? If yes, the solution could be
a lot simpler and possibly done using awk or a combination of shell tools.
If not, try this perl "one liner":

sample input file:

$ cat file
name=ravi
phone=101
email=a@b.c
phone=011
email=c@b.a
name=vira
email=a...@a.com
name=crap
email=s...@s.com
name=supercrap

The solution (which I've formatted here for ease of readability):

$ cat file | perl -MYAML -ni -e '
BEGIN {
    @db = ();
    $rec = {};
    %keycounter = ();
    $next_rec_at = 1;
}
chomp;
($k, $v) = $_ =~ m/^(.*?)\s*=\s*(.*)/gms;
$kcount = ( defined $keycounter{$k} ? $keycounter{$k} : 0 );
if ($kcount >= $next_rec_at) {
    push(@db, $rec);
    $rec = {};
    $new_rec_at++;
}
$rec->{$k} = $v;
$keycounter{$k}++;

END {
push(@db, $rec);
print YAML::Dump(\@db); # ... or do your report generation here
}'

output:

- email: a@b.c
  name: ravi
  phone: 101
- email: c@b.a
  name: vira
  phone: 011
- email: a...@a.com
  name: crap
- email: s...@s.com
  name: supercrap

cheers,

  -Suraj

-- 
Career Gear - Industry Driven Talent Factory
http://careergear.in/
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to