-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_USD.pl
executable file
·94 lines (71 loc) · 1.9 KB
/
convert_to_USD.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/perl
$default_src_cur="czk";
$APPLY=$ARGV[0];
#sub usage { print "Usage: $0 [--apply|-a]\n"; exit }
####################################
sub ccc_my {
my $src=shift;
# src can be
# 29000 =considered BYR
# 29czk =considered CZK
my $date=shift;
my $default_src_cur=shift;
if ($src=~/([\d\.]+)(\D+)/)
{
$src_amo=$1;
$src_cur=$2;
}
elsif ($src=~/([\d\.]+)/)
{
$src_amo=$1;
$src_cur=$default_src_cur;
}
else { print "wrong currency $src\n"; exit }
my $ccc_run="ccc $src_amo $src_cur usd $date";
my $ccc=`$ccc_run`;
chomp $ccc;
#print "===$ccc===\n";
if ($ccc =~ /^[\d\.]+$/ )
{
$ccc=sprintf("%.2f", `$ccc_run`);
return $ccc;
}
else { print "bad result '$ccc' from: \`$ccc_run\`\n"; exit }
}
####################################
sub perl_reg {
my $in=shift;
my $out=shift;
open IN, "<$in";
open OUT, ">$out";
while(<IN>)
{
chomp;
$out=$_;
if (/^DEFAULT_CURRENCY=(\S+)/) {$default_src_cur=$1}
if (/[\d\.]+\$/)
{ # dollar sign detected; no actions
}
else
{
$out=~s@^(\d{4}\-\d{2}\-\d{2})(\s+\S+\s+)([\d\.\S]+)(.*)@"$1"."$2".ccc_my($3,$1,$default_src_cur)."\$"."\t\t[orig: ".$3."]".$4@e
}
print OUT $out,"\n";
}
close IN;
close OUT;
}
####################################
if (! -f "ambar.txt" ) {print "the file being converted, ambar.txt , doesnt exist; exit\n"; exit; }
$TMP="/tmp/a.tmp";
perl_reg("ambar.txt", $TMP);
system ("diff -Naur ambar.txt $TMP");
if ( $APPLY =~/(-a|--apply)/ )
{
system ("mv -v $TMP ambar.txt")
}
else
{
print "\n\npass '-a' arguement to apply changes\n";
unlink ($TMP)
}