- Bash Cookbook
- Ron Brash Ganesh Naik
- 576字
- 2021-07-23 19:17:36
How to do it...
Let's begin our activity:
- Open a new terminal and a new file with your preferred editor. Add in the following contents into the new script and save it as builtin-str.sh:
#!/bin/bash
# Let's play with variable arrays first using Bash's equivalent of substr
STR="1234567890asdfghjkl"
echo "first character ${STR:0:1}"
echo "first three characters ${STR:0:3}"
echo "third character onwards ${STR: 3}"
echo "forth to sixth character ${STR: 3: 3}"
echo "last character ${STR: -1}"
# Next, can we compare the alphabeticalness of strings?
STR2="abc"
STR3="bcd"
STR4="Bcd"
if [[ $STR2 < $STR3 ]]; then
echo "STR2 is less than STR3"
else
echo "STR3 is greater than STR2"
fi
# Does case have an effect? Yes, b is less than B
if [[ $STR3 < $STR4 ]]; then
echo "STR3 is less than STR4"
else
echo "STR4 is greater than STR3"
fi
- Execute the script with bash builtin-str.sh and notice how we were able to strip the last character from a string and even compare strings.
- Again, open a new file called builtin-strng.sh and add the following contents into it:
#!/bin/bash
GB_CSV="testdata/garbage.csv"
EM_CSV="testdata/employees.csv"
# Let's strip the garbage out of the last lines in the CSV called garbage.csv
# Notice the forloop; there is a caveat
set IFS=,
set oldIFS = $IFS
readarray -t ARR < ${GB_CSV}
# How many rows do we have?
ARRY_ELEM=${#ARR[@]}
echo "We have ${ARRY_ELEM} rows in ${GB_CSV}"
Let's strip the garbage—remove spaces:
INC=0
for i in "${ARR[@]}"for i in "${ARR[@]}"
do
:
res="${i//[^ ]}"
TMP_CNT="${#res}"
while [ ${TMP_CNT} -gt 0 ]; do
i=${i/, /,}
TMP_CNT=$[$TMP_CNT-1]
done
ARR[$INC]=$i
INC=$[$INC+1]
done
Let's remove the last character from each line:
INC=0
for i in "${ARR[@]}"
do
:
ARR[$INC]=${i::-1}
INC=$[$INC+1]
done
Now, let's turn all of the characters into uppercase!
INC=0for i in "${ARR[@]}"
do
:
ARR[$INC]=${i^^}
printf "%s" "${ARR[$INC]}"
INC=$[$INC+1]
echo
done
# In employees.csv update the first field to be prepended with a # character
set IFS=,
set oldIFS = $IFS
readarray -t ARR < ${EM_CSV}
# How many rows do we have?
ARRY_ELEM=${#ARR[@]}
echo;echo "We have ${ARRY_ELEM} rows in ${EM_CSV}"
# Let's add a # at the start of each line
INC=0
for i in "${ARR[@]}"
do
:
ARR[$INC]="#${i}"
printf "%s" "${ARR[$INC]}"
INC=$[$INC+1]
echo
done
# Bob had a name change, he wants to go by the name Robert - replace it!
echo
echo "Let's make Bob, Robert!"
INC=0
for i in "${ARR[@]}"
do
:
# We need to iterate through Bobs first
ARR[$INC]=${i/Bob/Robert}
printf "%s" "${ARR[$INC]}"
INC=$[$INC+1]
echo
done
We will delete the day in the birth column. The field to remove is 5 (but this is really -4):
echo;
echo "Lets remove the column: birthday (1-31)"
INC=0
COLUM_TO_REM=4
for i in "${ARR[@]}"
do
:
# Prepare to also parse the ARR element into another ARR for
# string manipulation
TMP_CNT=0
STR=""
IFS=',' read -ra ELEM_ARR <<< "$i"
for field in "${ELEM_ARR[@]}"
do
# Notice the multiple argument in an if statement
# AND that we catch the start of it once
if [ $TMP_CNT -ne 0 ] && [ $TMP_CNT -ne $COLUM_TO_REM ]; then
STR="${STR},${field}"
elif [ $TMP_CNT -eq 0 ]
then
STR="${STR}${field}"
fi
TMP_CNT=$[$TMP_CNT+1]
done
ARR[$INC]=$STR
echo "${ARR[$INC]}"
INC=$[$INC+1]
done
- Execute the script with bash builtin-strng.sh and review the output.
Did you notice all of the opportunities to re-direct input or output? Imagine the possibilities! Furthermore, much of the previous script can be performed using another tool called AWK instead.
推薦閱讀
- Django+Vue.js商城項目實戰
- Learn to Create WordPress Themes by Building 5 Projects
- Java完全自學教程
- Vue.js入門與商城開發實戰
- Python機器學習實戰
- SQL基礎教程(視頻教學版)
- 劍指MySQL:架構、調優與運維
- Windows內核編程
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- Learning Apache Karaf
- ASP.NET程序開發范例寶典
- Oracle Data Guard 11gR2 Administration Beginner's Guide
- 深度學習入門:基于Python的理論與實現
- Java EE 8 and Angular
- Mastering Bootstrap 4