aboutsummaryrefslogtreecommitdiff
path: root/bin/tl
blob: a341a3bb01f0133255c67be3b439c05ed639b81f (plain) (blame)
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
#!/usr/bin/env bash
# Tag lines from files or stdin with a string prefix or suffix.
self=tl

# Define usage function
usage() {
    printf 'USAGE: %s [-h] [-p PREFIX] [-s SUFFIX] [--] [FILE1 FILE2 ...]\n' "$self"
}

# Start with empty prefix/suffix, or use the environment variables
prefix=$TL_PREFIX
suffix=$TL_SUFFIX

# Parse options out, give help if necessary
while getopts 'hp:s:' opt ; do
    case $opt in

        # -h: Print help
        h)
            usage
            exit
            ;;

        # -p: Specify prefix
        p)
            prefix=$OPTARG
            ;;

        # -s: Specify suffix
        s)
            suffix=$OPTARG
            ;;

        # Unknown option
        \?)
            usage >&2
            exit 2
            ;;
    esac
done
shift "$((OPTIND-1))"

# Need at least one tag
(($#)) || set /dev/stdin

# Print each line as we read it, prepending the tags, separated by spaces
for file ; do
    while IFS= read -r line ; do
        printf '%s%s%s\n' "$prefix" "$line" "$suffix"
    done < "$file"
done