File: trim.awk

package info (click to toggle)
runawk 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 712 kB
  • sloc: awk: 1,127; ansic: 736; sh: 420; makefile: 103
file content (73 lines) | stat: -rw-r--r-- 1,334 bytes parent folder | download | duplicates (4)
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
# Written by Aleksey Cheusov <vle@gmx.net>, public domain
#
# This awk module is a part of RunAWK distribution,
#        http://sourceforge.net/projects/runawk
#
############################################################

# =head2 trim.awk
#
# =over 2
#
# =item I<trim_l(STRING)>
#
# Removes leading Tab and Space characters from STRING and returns
# the result.
#
# =item I<trim_r(STRING)>
#
# Removes Tab and Space characters at the end of STRING and returns
# the result.
# 
# =item I<trim_c(STRING, REPL)>
#
# Replaces sequences of Tab and Space characters in STRING with REPL
# and returns the result. If REPL is not specified, it defaults to
# single Space character.
#
# =item I<trim_lr(STRING)>
#
# Equal to trim_l(trim_r(STRING))
#
# =item I<trim_lrc(STRING, REPL)>
#
# Equal to trim_l(trim_r(trim_c(STRING, REPL)))
#
# =back
#
# See example/demo_trim for the sample of usage
#

function trim_l (s){
	sub(/^[ \t]+/, "", s)
	return s
}

function trim_r (s){
	sub(/[ \t]+$/, "", s)
	return s
}

function trim_c (s, repl){
	if (repl == "")
		repl = " "

	gsub(/[ \t][ \t]+/, repl, s)
	return s
}

function trim_lr (s){
	sub(/[ \t]+$/, "", s)
	sub(/^[ \t]+/, "", s)
	return s
}

function trim_lrc (s, repl){
	if (repl == "")
		repl = " "

	gsub(/[ \t][ \t]+/, repl, s)
	sub(/[ \t]+$/, "", s)
	sub(/^[ \t]+/, "", s)
	return s
}