#!/bin/perl use strict; use warnings; my $filename = '808.nc'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; my $g="G"; my $f="F"; while (my $row = <$fh>) { $row =~ s/\r?\n$//; my @words = split / /, $row; foreach (@words){ my $Word=$_; my $char=substr($_, 0, 1); if ($char eq "N"){ print "$Word" } elsif ($char eq "G"){ if($Word ne $g){ $g=$Word; print " $Word" } } elsif ($char eq "F"){ if($Word ne $f){ $f=$Word; print " $Word" } } else {print " $Word"} } print "\n"; }