Created
February 25, 2015 21:47
-
-
Save Krellan/b34c834e335f9c7e15e4 to your computer and use it in GitHub Desktop.
Strip numeric prefix from NAME=VALUE file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
my %keys; | |
while(<>) | |
{ | |
chomp; | |
# Match NAME=VALUE (first equals sign is the separator) | |
if (/^([^=]+)[=](.*)$/) | |
{ | |
my $key = $1; | |
my $value = $2; | |
# Strip any leading numeric digits from key | |
if ($key =~ /^[0-9]+(.*)$/) | |
{ | |
$key = $1; | |
} | |
# Strip any leading underscores, that might have followed those numeric digits | |
if ($key =~ /^[_]+(.*)$/) | |
{ | |
$key = $1; | |
} | |
my $existing = $keys{$key}; | |
if (defined $existing) | |
{ | |
die "Duplicate key $key detected"; | |
} | |
# Remember for duplicate check | |
$keys{$key} = $value; | |
print $key; | |
print "="; | |
print $value; | |
print "\n"; | |
} | |
else | |
{ | |
# Copy verbatim | |
print $_; | |
print "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment