-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubstitute
More file actions
executable file
·93 lines (72 loc) · 2 KB
/
substitute
File metadata and controls
executable file
·93 lines (72 loc) · 2 KB
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
#!/usr/bin/perl
# $Id: substitute,v 1.5 2006/08/27 13:43:03 jonathan Exp $
use strict;
use Getopt::Std;
# Note: this should be augmented so that you can do subs in more than one column at a time
my $usage = "Usage: substitute [-k column] [-v] datafile -m mapfile\n";
$usage .= " take key values from mapfile and substitute them into column k of datafile\n";
$usage .= " with no column given, all substitutions possible are made\n";
$usage .= " -v instead of \"key value\", read mapfile as \"value key\"\n";
$usage .= " -x if key is not defined in a column, ignore line\n";
$usage .= " -C file is not in columns, just substitute all values in map for anything that matches in the file\n";
my %para = ();
getopts('k:vxCm:', \%para);
my $dataFile = $ARGV[0];
my $mapFile = '';
if (!defined($para{'m'})) {
die ($usage);
}
$mapFile = $para{'m'};
my %map = ();
open MAP, "$mapFile" or die ("Cannot open $mapFile\n");
while (my $line = <MAP>) {
my (@items) = split (/\s+/, $line);
if (!defined($para{'v'})) {
$map{$items[0]} = $items[1];
} else {
$map{$items[1]} = $items[0];
}
}
close MAP;
my $lineCount = 0;
#open DATA, "$dataFile" or die ("Cannot open $dataFile\n");
if (!$para{'C'}) {
while (my $line = <>) {
my (@items) = split (/\s+/, $line);
my $out = '';
my $validLine = 1;
for (my $i = 0; $validLine && $i <= $#items; $i++) {
if ((defined($para{'k'}) && $i == $para{'k'}-1) ||
(!defined($para{'k'}))) {
if (defined ($map{$items[$i]})) {
$out .= $map{$items[$i]};
} else {
if (defined($para{'x'})) {
$validLine = 0;
} else {
$out .= $items[$i];
}
}
} else {
$out .= $items[$i];
}
if ($i != $#items) {
$out .= " ";
}
}
if ($validLine == 1) {
print "$out\n";
}
}
} else {
my $whole = '';
while (my $line = <>) {
# while (my $line = <DATA>) {
$whole .= $line;
}
foreach my $key (keys %map) {
$whole =~ s/$key/$map{$key}/g;
}
print "$whole";
}
#close DATA;