官术网_书友最值得收藏!

  • Bash Cookbook
  • Ron Brash Ganesh Naik
  • 576字
  • 2021-07-23 19:17:36

How to do it...

Let's begin our activity:

  1. 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
  1. 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.
  2. 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 garbageremove 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
  1. 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.
主站蜘蛛池模板: 禄丰县| 交口县| 育儿| 太仆寺旗| 永吉县| 新竹县| 嘉黎县| 九江县| 新建县| 余庆县| 泰顺县| 长阳| 临安市| 子长县| 临泉县| 黄石市| 闽侯县| 白城市| 金门县| 神农架林区| 长垣县| 梁平县| 突泉县| 青阳县| 固始县| 卓尼县| 松江区| 田阳县| 洛宁县| 民权县| 天柱县| 来宾市| 板桥市| 神木县| 五峰| 汉源县| 西充县| 赤水市| 商水县| 昆山市| 石林|