#! /usr/bin/perl use strict; use warnings; # words to search for my @words = qw( wort1 wort2 wort3 wort4 ); # built a regex for the search; each word is to be treated as string my $regex = join '|', map {"\Q${_}\E"} @words; # list of alternative strings $regex = qr{($regex)}; # capture the match my %position; while ( my $line = ) { # save line number ($.) of each occurence in read lines; # use a while loop to catch multiple words in line push @{ $position{$1} }, $. while $line =~ m{$regex}g; } # show result for my $word ( sort keys %position ) { printf "%s Zeile: %s\n", $word,join ", ", @{$position{$word}}; } __DATA__ aaaa, aaaaa, aaaaaa, aaaaaa, wort1, aaaa, aaaa, bbbbb, bbbbb, bbbb, wort2, bbbbb, bbbb, bbbbbb, cccccc, ccccc, cccc, cccccc, wort3, cccc, cccc, ddddd, dddddd, wort1, dddddd, ddddd, dddd, ddd, eee, eee, eeeeee, eeeeeeee, wort4, wort2, eeee,