aboutsummaryrefslogtreecommitdiff
path: root/basic.sh
blob: 65b44e529a3beadd44319f1ab140378e90d2fb74 (plain)
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
#!/bin/bash

function init
{
	tmp_d="$(mktemp -d)"
	tmp="$tmp_d/a"
	tmp_="$tmp_d/b"
}

function cleanup
{
	rm -r "$tmp_d"
}
trap cleanup EXIT

# $1: string to replace: regular expression
# $2: string to replace with: sed replacement string
function replace
{
	sed -i "s/${1}/${2}/g" "$tmp"
}

# $1 line to replace: regular expression
# $2 text to replace with: (newline terminated) sed replacement string
# $3 removal type: (rm-plus-one|normal)
function replace_line
{
	if test -n "$2"
	then
		cat <(sed "/${1}/,\$ d" < "$tmp") \
			<(echo -n "$2") \
			<(sed "0,/${1}/ d" < "$tmp") \
			> "$tmp_"
		mv "$tmp_" "$tmp"
	else
		if [[ "$3" == rm-plus-one ]]
		then
			sed -i "/${1}/,+1 d" "$tmp"
		else
			sed -i "/${1}/ d" "$tmp"
		fi
	fi
}

# $1 line to delete: regular expression
function delete_line
{
	sed -i "/${1}/ d" "$tmp"
}