I am told I used awk in a wrong approach in the below code, but I am dumbfounded as to how to improve my code so that it is more simpler to read.
read -r bookName
read -r authorName
if grep -iqx "$bookName:$authorName" cutText.txt
then
lineNum=`awk -v bookName="$bookName" -v authorName="$authorName" '$0 ~ bookName ":" authorName {print NR} BEGIN{IGNORECASE=1}' BookDB.txt`
echo "Enter a new title"
read -r newTitle
awk -F":" -v bookName="$bookName" -v newTitle="$newTitle" -v lineNum="$lineNum" 'NR==lineNum{gsub(bookName, newTitle)}1' cutText.txt > temp2.txt
mv -f temp2.txt cutText.txt
else
echo "Error"
fi
My cutText.txt contains content as shown below:
Hairy Potter:Rihanna
MARY IS A LITTLE LAMB:Kenny
Sing along:May
This program basically update a new title in cutText.txt. If a user wants to change MARY IS A LITTLE LAMB
to Mary is not a lamb
, he will enter the new title and cutText.txt
will replace the original title with Mary is not a lamb
.
A problem arises now that if a user enter "Mary is a little lamb" for $newTitle
, this code of works just doesn't work, because it does take the case into account.
It will only work is user types "MARY IS A LITTLE LAMB". I came to be aware that BEGIN{IGNORECASE=1}
is gawk-sepcific, therefore it cannot be used in awk.
How can I script this better so I can ignore case in user input? Thank you!