-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltrim.bash
47 lines (47 loc) · 1.59 KB
/
ltrim.bash
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
#!/usr/bin/env bash
#@ Function : trim ltrim rtrim trimv trimall
#@ Desc : Delete leading/trailing blank characters from a string or
#@ : stream.
#@ :
#@ : Blank charaters are space, tab, and new-line.
#@ :
#@ : trim strip string/file of leading+trailing blank chars.
#@ : ltrim strip string/file of leading blank chars.
#@ : rtrim strip string/file of trailing blank chars.
#@ : trimv assign stripped string to variable.
#@ : trimall strip string/file of trailing blank chars and double spaces within string.
#@ :
#@ Synopsis : trim [-e] string|-
#@ : ltrim string|-
#@ : rtrim string|-
#@ : trimv -n varname string|-
#@ : trimall string|-
#@ :
#@ Examples : #0) strip spaces from around 'str'
#@ : str=" 123 "; str=$(trim "$str")
#@ :
#@ : #1) remove all leading+trailing blanks.
#@ : trim <fat.file >thin.file
#@ :
#@ : #2) remove trailing blanks from file.
#@ : rtrim <fat.file >lean.file
#@ :
#@ : #3) remove all leading+trailing blanks from file, scenic route.
#@ : rtrim <fat.file | ltrim >thin.file
#@ :
#@ : #4) Assign stripped string to varname.
#@ : trimv -n myvar " This is a messy string. "
#@ : echo "$myvar"
#@ :
ltrim() {
if(($#)); then
local v="$*"
echo "${v#"${v%%[![:blank:]]*}"}"
else
local REPLY
while IFS= read -r; do
echo "${REPLY#"${REPLY%%[![:blank:]]*}"}"
done
fi
}
declare -fx ltrim