-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmergeFiles
More file actions
executable file
·57 lines (49 loc) · 1.04 KB
/
mergeFiles
File metadata and controls
executable file
·57 lines (49 loc) · 1.04 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
#!/usr/bin/perl
use strict;
use Getopt::Std;
# usage: takes lines of the form
# 0 24
# 1 85
# 2 88
# ...
# 0 5
# 1 6
# 2 7
#
# and outputs
# 0 24 5
# 1 85 6
# ...
my $usage = "Usage: cat file1 file2 | mergeFiles [options] > resullt\n";
$usage .= " -i character(s) to put in between columns\n";
$usage .= " -e character(s) to put at end\n";
$usage .= "Example:\n";
$usage .= " cat foo/a foo/b | mergeFiles -i '&' -e '\\'";
my %para = ();
getopts('i:e:h', \%para);
if (defined($para{'h'})) {
print "$usage\n";
exit (0);
}
my @entries = ();
while (my $line = <STDIN>) {
if ($line =~ /^\s*(\d+)\s(.*?)$/) {
# print "line match $1 foo $2";
my $index = $1;
my $value = $2;
if (defined($para{'i'})) {
$entries[$index] .= $para{'i'};
}
$entries[$index] .= $value;
} else {
print "no match $line\n";
;
}
}
for (my $i = 0; $i <= $#entries; $i++) {
print "$i $entries[$i]";
if (defined($para{'e'})) {
print "$para{'e'}";
}
print "\n";
}