Shell Programming: String Length: Text File
Shell Programming: String Length: Text File
${#string}
expr length $string
expr "$string" : '.*'
stringZ=abcABC123ABCabc
echo ${#stringZ}
echo `expr length $stringZ`
echo `expr "$stringZ" : '.*'`
# 15
# 15
# 15
len=${#line}
if [ "$len" -lt "$MINLEN" ]
then echo
# Add a blank line after short line.
fi
done
exit 0
Index
# 8
# 8
# 6
# C position.
# 3
# abcABC123ABCabc
# bcABC123ABCabc
# 23ABCabc
echo ${stringZ:7:3}
# 23A
# Three characters of
substring.
echo ${*:2:3}
second.
# Same as above.
# ab
# ABC
# abcABC1
# abcABC1
# abcABC1
# ABCabc
# ABCabc
Substring Removal
${string#substring}
Strips shortest match of $substring from front of $string.
${string##substring}
Strips longest match of $substring from front of $string.
stringZ=abcABC123ABCabc
#
|----|
#
|----------|
echo ${stringZ#a*C}
# 123ABCabc
# Strip out shortest match between 'a' and 'C'.
echo ${stringZ##a*C}
# abc
# Strip out longest match between 'a' and 'C'.
${string%substring}
Strips shortest match of $substring from back of $string.
${string%%substring}
Strips longest match of $substring from back of $string.
stringZ=abcABC123ABCabc
#
||
#
|------------|
echo ${stringZ%b*c}
# abcABC123ABCa
# Strip out shortest match between 'b' and 'c', from back of $stringZ.
echo ${stringZ%%b*c}
# a
# Strip out longest match between 'b' and 'c', from back of $stringZ.
then
directory=$1
else
directory=$PWD
fi
# Assumes all files in the target directory are MacPaint image files,
#+ with a ".mac" filename suffix.
for file in $directory/*
do
filename=${file%.*c}
# Filename globbing.
}
# Pass all options to getopt_simple().
getopt_simple $*
echo "test is '$test'"
echo "test2 is '$test2'"
exit 0
--sh getopt_example.sh /test=value1 /test2=value2
Parameters are '/test=value1 /test2=value2'
Processing parameter of: '/test=value1'
Parameter: 'test', value: 'value1'
Processing parameter of: '/test2=value2'
Parameter: 'test2', value: 'value2'
test is 'value1'
test2 is 'value2'
Substring Replacement
${string/substring/replacement}
Replace first match of $substring with $replacement.
${string//substring/replacement}
Replace all matches of $substring with $replacement.
stringZ=abcABC123ABCabc
echo ${stringZ/abc/xyz}
'xyz'.
echo ${stringZ//abc/xyz}
# 'xyz'.
${string/#substring/replacement}
# xyzABC123ABCabc
# Replaces first match of 'abc' with
# xyzABC123ABCxyz
# Replaces all matches of 'abc' with
echo ${stringZ/#abc/XYZ}
# XYZABC123ABCabc
# Replaces front-end match of 'abc'
with 'XYZ'.
echo ${stringZ/%abc/XYZ}
# abcABC123ABCXYZ
# Replaces back-end match of 'abc'
with 'XYZ'.