However, I want to be able to search only one word anywhere on the string (case insensitive), and it will still be able to return me the results.
Example as follows:
Enter fruit name: sour
Enter brand: [null]
Found 2 matches:
Sour Grapes, Sunshine Ltd, $0.95, 200
Sour Apples, Moonlight Ltd, $0.30, 100
---------------------------------------
Enter fruit name: sour
Enter brand: moonlight
Found 1 match:
Sour Apples, Moonlight Ltd, $0.30, 100
The easiest way might be to make the t variable a regex: -vt="$Fruit.*$Brand" It will find the lines with both strings present (Fruit before Brand!), or, if one is missing, all the lines having the other value.
However, I just tried to search in lowercase, but it didn't return any results.
Also, if one search term is missing, but the other is present, the result will be "Please fill in the necessary information.".
if test "$Fruit" = "" || test "$Brand" = ""; then
echo "Please fill in the necessary information."
else
awk -F: -vOFS=", " -vt="$Fruit.*$Brand" '$0~t{$3="$"$3;print}' Fruit.txt
fi
awk works case sensitively. If you want to match upper and lower case chars, special action has to be taken, like applying the toupper/tolower functions. Or modify the regex accordingly (hint: [aA]).