#!/usr/bin/env perl -l use strict; use warnings; get_clean_ini_data(); for (qw{MySection AnotherSection WhitespaceSection}) { print "Contents of '$_':\n", get_section($_); } { my %section_lines_for; sub get_clean_ini_data { my $current_section; while () { s/^\s*(.*?)\s*$/$1/; next unless length; if (/^\[([^]]+)/) { my $new_section = $1; strip_trailing_comments($current_section); $current_section = $new_section; } else { push @{$section_lines_for{$current_section}}, $_; } } strip_trailing_comments($current_section); } sub strip_trailing_comments { my $section = shift; return unless defined $section; for my $i (reverse 0 .. $#{$section_lines_for{$section}}) { if (0 == index $section_lines_for{$section}[$i], ';') { pop @{$section_lines_for{$section}}; } else { last; } } } sub get_section { join "\n", @{$section_lines_for{$_[0]}} } } __DATA__ [MySection] ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 ; This is the heading for AnotherSection [AnotherSection] ; another comment asfld=69 ; Heading for WhitespaceSection [WhitespaceSection] ; Comment starting with a tab ; Comment starting with a tab and a space ; Comment starting with a space ; Comment ending with a tab ; Comment ending with a tab and a space ; Comment ending with a space ; tab+space+comment+space+tab ; space+tab+comment+tab+space qwe=rty asd=fgh ; trailing 1 ; tab + trailing 2 ; space + trailing 3 ; trailing 4 #### $ pm_1178405_ini_file_clean.pl Contents of 'MySection': ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 Contents of 'AnotherSection': ; another comment asfld=69 Contents of 'WhitespaceSection': ; Comment starting with a tab ; Comment starting with a tab and a space ; Comment starting with a space ; Comment ending with a tab ; Comment ending with a tab and a space ; Comment ending with a space ; tab+space+comment+space+tab ; space+tab+comment+tab+space qwe=rty asd=fgh #### $ cat -vet pm_1178405_ini_file_clean.pl ... __DATA__$ [MySection]$ ; This is a comment line for MySection$ fld1 = 'value of field 1' $ fld2 = 42$ $ ; This is the heading for AnotherSection$ [AnotherSection]$ ; another comment$ asfld=69$ $ ; Heading for WhitespaceSection$ [WhitespaceSection]$ ^I; Comment starting with a tab$ ^I ; Comment starting with a tab and a space$ ; Comment starting with a space$ ; Comment ending with a tab^I$ ; Comment ending with a tab and a space^I $ ; Comment ending with a space $ ^I ; tab+space+comment+space+tab ^I$ ^I; space+tab+comment+tab+space^I $ $ qwe=rty$ asd=fgh$ ; trailing 1$ ^I; tab + trailing 2$ ; space + trailing 3$ ; trailing 4$ #### $ pm_1178405_ini_file_clean.pl | cat -vet Contents of 'MySection':$ ; This is a comment line for MySection$ fld1 = 'value of field 1'$ fld2 = 42$ Contents of 'AnotherSection':$ ; another comment$ asfld=69$ Contents of 'WhitespaceSection':$ ; Comment starting with a tab$ ; Comment starting with a tab and a space$ ; Comment starting with a space$ ; Comment ending with a tab$ ; Comment ending with a tab and a space$ ; Comment ending with a space$ ; tab+space+comment+space+tab$ ; space+tab+comment+tab+space$ qwe=rty$ asd=fgh$