- Bash Cookbook
- Ron Brash Ganesh Naik
- 388字
- 2021-07-23 19:17:37
How to do it...
As with the exercise using Bash only, we are going to perform a similar recipe as follows:
Create a script called some-strs.sh with the following content and open a new terminal:
#!/bin/bash
STR="1234567890asdfghjkl"
echo -n "First character "; sed 's/.//2g' <<< $STR # where N = 2 (N +1)
echo -n "First three characters "; sed 's/.//4g' <<< $STR
echo -n "Third character onwards "; sed -r 's/.{3}//' <<< $STR
echo -n "Forth to sixth character "; sed -r 's/.{3}//;s/.//4g' <<< $STR
echo -n "Last character by itself "; sed 's/.*\(.$\)/\1/' <<< $STR
echo -n "Remove last character only "; sed 's/.$//' <<< $STR
Execute the script and review the results.
Create another script called more-strsng.sh and then execute it:
#!/bin/sh
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[@]}"
do
:
ARR[$INC]=$(echo $i | sed 's/ //g')
echo "${ARR[$INC]}"
INC=$[$INC+1]
done
# Remove the last character and make ALL upper case
INC=0
for i in "${ARR[@]}"
do
:
ARR[$INC]=$(echo $i | sed 's/.$//' | sed -e 's/.*/\U&/' )
echo "${ARR[$INC]}"
INC=$[$INC+1]
done
We want to add a # at the beginning of each line and we will also use the sed tool on a per file basis. We just want to strip Bob out and change his name to Robert by manipulating the file in-place:
set IFS=,
set oldIFS = $IFS
readarray -t ARR < ${EM_CSV}
INC=0
for i in "${ARR[@]}"
do
:
ARR[$INC]=$(sed -e 's/^/#/' <<< $i )
echo "${ARR[$INC]}"
INC=$[$INC+1]
done
sed -i 's/Bob/Robert/' ${EM_CSV}
sed -i 's/^/#/' ${EM_CSV} # In place, instead of on the data in the array
cat ${EM_CSV}
# Now lets remove the birthdate field from the files
# Starts to get more complex, but is done without a loop or using cut
awk 'BEGIN { FS=","; OFS="," } {$5="";gsub(",+",",",$0)}1' OFS=, ${EM_CSV}
Examine the results—was it simpler to get the results to the recipes that leverage only bash built-in constructs? Likely yes in many situations, IF they are available.
推薦閱讀
- 微服務(wù)設(shè)計(jì)(第2版)
- JBoss Weld CDI for Java Platform
- 高手是如何做產(chǎn)品設(shè)計(jì)的(全2冊)
- Game Programming Using Qt Beginner's Guide
- 碼上行動:零基礎(chǔ)學(xué)會Python編程(ChatGPT版)
- Silverlight魔幻銀燈
- Web全棧工程師的自我修養(yǎng)
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- Java程序設(shè)計(jì):原理與范例
- 微信小程序項(xiàng)目開發(fā)實(shí)戰(zhàn)
- 大數(shù)據(jù)時(shí)代的企業(yè)升級之道(全3冊)
- Getting Started with React VR
- Scrapy網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)
- DB2SQL性能調(diào)優(yōu)秘笈
- Python趣味創(chuàng)意編程