#!/usr/bin/perl -w use strict; open(FH, "comments.txt") or die "cant open file"; while (my $line = ) { chomp $line; next if $line =~ /^$/; $line = s/\s*\#.*//; print "|$line|\n"; } close(FH);