#!/bin/perl use v5.18; use warnings; my ($g, $f) = ('G', 'F'); my $filename = '808.nc'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { $row =~ s/\r?\n$//; for my $word (split / /, $row){ say ''; given (substr($word, 0, 1)) { when ('N') { } when ('G') { if ($word ne $g) { $g = $word } else { next } } when ('F') { if ($word ne $f) { $f = $word } else { next } } } print " $word"; } }