-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailmerge.sh
executable file
·42 lines (31 loc) · 964 Bytes
/
mailmerge.sh
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
#!/bin/tcsh
# Perform a mail merge on a form-letter.
# Copyright (c) 2021, Michael Pascale.
# Set a template file and a tab-separated replacements table.
set template = template.txt
set table = replacements.tsv
# Set the index of the name column.
set n = 1
set names = `tail -n+2 $table | cut -f$n`
# Extract the column names from the replacements table.
set fields = `head -n1 $table`
# Create the output directory.
mkdir -p output
# For each row in the replacement table.
@ i = `cat $table | wc -l` - 1
while ($i > 0)
# Copy the template.
set out = "output/$names[$i]_$template"
cp $template $out
# Loop through each column.
set j = 1
while ($j <= $#fields)
# Read the field and its replacement.
set field = $fields[$j]
set values = "`tail -n+2 $table | cut -f$j`"
# Replace the field with the specified value.
sed -i "s/$field/$values[$i]/g" $out
@ j++
end
@ i--
end