wc Command Reference for CI Scripts
wc counts lines, words, and bytes of its input.
wc is the go-to for counting matches or sizing output in a pipeline. The subtle CI issue is the leading whitespace it prints, which can break a numeric comparison.
Common flags/usage
- -l: count lines (newlines)
- -w: count words
- -c: count bytes
- -m: count characters (multibyte aware)
- -L: length of the longest line
Example
shell
COUNT=$(git diff --name-only | wc -l | tr -d ' ')
if [ "${COUNT}" -gt 100 ]; then echo "Large PR" ; fi
grep -c TODO src/*.ts | awk -F: '{s+=$2} END{print s}'In CI
On BSD/macOS wc pads its output with leading spaces, so COUNT=$(... | wc -l) may hold " 42" and break [ "$COUNT" -eq 42 ]; strip it with tr -d ' ' or use grep -c. Note wc -c counts bytes while wc -m counts characters, which differ for UTF-8 text.
Key takeaways
- wc -l counts lines; pipe through tr -d " " to strip padding for comparisons.
- grep -c often replaces grep | wc -l for counting matches.
- wc -c counts bytes, wc -m counts characters; they differ for multibyte text.
Related guides
grep Command Reference for CI Scriptsgrep searches text for matching lines in CI. Reference for -r, -E, -i, -q, and -v, plus the exit-code-1-means…
uniq Command Reference for CI Scriptsuniq filters or counts adjacent duplicate lines in CI. Reference for -c, -d, -u, and -i, plus the must-sort-f…
awk Command Reference for CI Scriptsawk is a pattern-scanning text processor for CI. Reference for -F field separator, -v variables, and $1/$NF f…
tr Command Reference for CI Scriptstr translates, squeezes, or deletes characters from stdin in CI. Reference for -d, -s, -c, and ranges, plus t…