From: Nicolas Boisselier Date: Sat, 17 Dec 2022 15:21:02 +0000 (+0100) Subject: bin/tel-replace-contact created X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=e4d983f2a2cdca403fedf725b70a6f806f3a553c;p=nb.git bin/tel-replace-contact created --- diff --git a/bin/tel-replace-contact b/bin/tel-replace-contact new file mode 100755 index 00000000..df6fa37f --- /dev/null +++ b/bin/tel-replace-contact @@ -0,0 +1,213 @@ +#!/usr/bin/env perl +use strict; +use warnings; +################################################################################# +# +# VERSION +# +################################################################################# +my $VERSION = '0.0.1'; +# NB 17.12.22 +# - create script: + +################################################################################# +# +# GLOBALS +# +################################################################################# +my ($NAME) = $0 =~ m,([^/]+)$,; +my $DATA = "$ENV{HOME}/data/contact-tel.txt"; + +################################################################################# +# +# ARGS +# +################################################################################# +my $VERBOSE = $main::VERBOSE = 1; +my $DEBUG = $main::DEBUG = 0; + +my %Opt = ( +); +get_options(\%Opt); +help() unless @ARGV; +$main::_DATA_ = undef; + +################################################################################# +# +# BEGIN +# +################################################################################# +use Data::Dumper; + +my %tel = (); +my $htel; +open($htel,'<'.$DATA) or die "$!"; +while (my $line = <$htel>) { + chomp($line); + my @rec = split(/\t/,$line); + $rec[1] =~ s/[^\d\+]//g; + $tel{$rec[1]} = $rec[0]; +} +close $htel; +#die Dumper(\%tel); + +my $line; +while ($line = <>) { + if ($line =~ /(\s+)([\+0]\d{9,12})/) { + my $blank = $1; + my $tel = $2; + my $tel2 = $tel; $tel2 =~ s/^0/+33/; + + if (exists $tel{$tel}) { + $line =~ s/\Q$tel\E/$tel{$tel} ($tel)/g; + + } elsif (exists $tel{$tel2}) { + $line =~ s/\Q$tel\E/$tel{$tel2} ($tel)/g; + + } + } + print "$line\n"; + +} + +################################################################################# +# +# END +# +################################################################################# +exit 0; + +################################################################################# +# +# Functions +# +################################################################################# +sub help { +#------------------------------------------------------------------------------ +# Print help and exit +#------------------------------------------------------------------------------ + + require 'Pod/Usage.pm' unless $INC{'Pod/Usage.pm'}; + + # Substitutions + sub pod_env { + my $v = ''; + eval '$v = ref(\\'.$_[0].') eq "ARRAY" ? join(" ",'.$_[0].') : '.$_[0].'; return defined $v ? $v : qq|UNDEF|;'; + return $v; + } + + $main::_DATA_ =~ s/([@\$][A-Z_a-z\{\}]+)/pod_env($1)/eg; + + my $in; + open($in,'<',\$main::_DATA_); + + open(STDOUT,"|perl -pe 's/\.$$//g'".(($ENV{PAGER}||'') eq 'less' ? "|less -FRi" : "")); + my $opts = { + -input => $in, + -ouput => \*STDOUT, + -exitval => 'noexit', + -sections => [qw(SYNOPSIS DESCRIPTION OPTIONS)], + -verbose => ($Opt{'help'} ? 99 : 3), + }; + + Pod::Usage::pod2usage($opts); + close $in; + close STDOUT; + + exit 0; +} + +#------------------------------------------------------------------------------ +# Print version and exit +#------------------------------------------------------------------------------ +sub version { print "$NAME: version [$VERSION]\n"; exit 0; } + +#------------------------------------------------------------------------------ +# Get options from pod +#------------------------------------------------------------------------------ +sub get_options { + + use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); + + my @Opt; + + sub pod_opt { + local $_; + my $o = shift; + $o =~ s/(=.|[\+\-\!]$)//; + $o = join(", ",map{"-$_"} split(/[\|,:;]/,$o)); + return "$o"; + } + + while () { + s/option\[([^\]]+)\]/push(@Opt,$1) and pod_opt($1)/eg; + $main::_DATA_ .= $_; + } + + GetOptions($_[0],@Opt) || exit -1; + + help() if $_[0]{'help'} or $_[0]{'man'}; + version() if $_[0]{'version'}; + + $main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'}; + $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; + +} + +__DATA__ + +=head1 NAME + +$NAME - Replace content having phone number with contact's name + +=head1 SYNOPSIS + +Quick usage! + +=over + +=item $NAME --verbose + +=item $NAME --help + +=back + +=head1 DESCRIPTION + +Replace content having phone number with contact's name + +=head1 OPTIONS + + -option[verbose|v+] Verbose mode: increase the verbosity level. + -option[debug+] Debug mode: increase the verbosity level. + -option[version|V] Print version (default: $VERSION) + -option[help|h|?] Print a brief help message and exits. + -option[man] Print the manual page and exits. + +=cut + +=head1 EXAMPLES + +... + +=head1 REQUIRES + +Getopt::Std, Pod::Usage + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2022 Nicolas Boisselier + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +See . + +=head1 SEE ALSO + +perl(1), http://perldoc.perl.org/perlpodstyle.html + +=head1 AUTHOR + +Nicolas Boisselier + +=cut