Skip to content

Instantly share code, notes, and snippets.

@Krellan
Created February 25, 2015 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krellan/b34c834e335f9c7e15e4 to your computer and use it in GitHub Desktop.
Save Krellan/b34c834e335f9c7e15e4 to your computer and use it in GitHub Desktop.
Strip numeric prefix from NAME=VALUE file
#!/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