#!/usr/bin/perl -w use strict; while (my $line =) { chomp $line; if ($line =~ m/^([^\s]+)\s*(.*)$/) { my $label = $1; my @features = split(' ', $2); print "$label"; my $l1_norm = 0; foreach my $feature (@features) { my ($term, $weight) = split(/:/, $feature); $l1_norm += $weight; } foreach my $feature (@features) { my ($term, $weight) = split(/:/, $feature); my $normalized_weight = ($weight/$l1_norm); print " $term:$normalized_weight"; } print "\n"; } }