I hope all of you are in good health.
I was working on searching within a CSV file using Bash Scripting. I have a CSV file that contain 5+ columns with the following data:
Col1, Col2, Col3, Col4, Col5
1, FName 1, LName 1, 14:01:2019, EXT_ABC
2, FName 2, LName 2, 15:02:2020, EXT_XYZ
3, FName 1, LName 3, 16:03:2021, PQR_LMN
I want to search within this CSV file on behalf of 2nd column that are names. I have written the following code yet.
read -p "What is your first name?: " fname
if grep -q "$fname" "data.csv"
then
printf "%s\n" "Exist."
else
printf "%s\n" "Sorry, Not Exist."
fi
This code work fine too some extent but It is searching from all the columns. If I search for LName 2, It says Exist. It should only search in 2nd column.
Also, If the record exist how can I get that line from CSV. In this way If there are multiple records exist then an array should be created that contain the lines as string in which that first name exist.
Can we search between 2 columns for example: I want to search the name in 2nd and 3rd column only.
Thanks.
awkfor this instead ofgrep