-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc.sh
45 lines (41 loc) · 1.1 KB
/
wc.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
43
44
45
# shellcheck disable=SC2148
################################################################################
# Replace the system `wc` with internal functions
#
# This file has no magic number, and is not executable.
# THIS IS INTENTIONAL as it should never be executed, only sourced.
################################################################################
wc.words() {
local count=0
local words=()
local IFS=$' \t'
while read -ra words; do (( count+=${#words[@]} )); done
(( count+=${#words[@]} )) # if read hits EOF, it may still have read some data
echo "$count"
}
wc.lines() {
local count=0
local line=
local IFS=
# shellcheck disable=SC2034
while read -r line; do (( ++count )); done
echo "$count"
}
wc.chars() {
local count=0
local char=
local IFS=
# shellcheck disable=SC2034
while read -rn1 char; do (( ++count )); done
echo "$count"
}
wc.count() {
local match=$1
local count=0
local char=
local IFS=
while read -d '' -rn1 char; do
[[ "$char" != "$match" ]] || (( ++count ))
done
echo "$count"
}