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

  • 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.
主站蜘蛛池模板: 林芝县| 通海县| 浦城县| 开封市| 仲巴县| 宽甸| 沭阳县| 英德市| 南陵县| 昌邑市| 江西省| 阿拉善右旗| 易门县| 英德市| 德化县| 突泉县| 贵德县| 临沭县| 漳平市| 屏东市| 临清市| 凌云县| 长乐市| 苍山县| 巧家县| 资源县| 天津市| 永福县| 通化市| 常德市| 离岛区| 平武县| 布拖县| 普兰县| 大英县| 奉化市| 广德县| 海伦市| 珲春市| 乐清市| 阿拉善左旗|