0

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.

2
  • 1
    You should be using awk for this instead of grep Commented Apr 29, 2021 at 16:15
  • its not efficient but will do the trick, cut column 2 and 3 using cut on a separate file and then run your script against that file. Commented Apr 29, 2021 at 16:17

1 Answer 1

2

You can use awk to search for specific column.

awk -F ',' -v var="$fname" '$2 ~ var {print $2}' data.csv
-F option specifies the file separator
-v option can specify a shell variable to put it into a variable that awk will understand.
$2 will ensure search in only 2nd column

If you want to search between two columns, you may use logical OR (||) or logical AND (&&) to provide further conditions. Your file contains several spaces in columns, so search with (~) should be used.

Example of search between 2 columns with OR :

awk -F ',' -v var="FName 1" -v var2="Lname 3" '$2 ~ var || $3 ~ var2 {print $0}' data.csv

Here you can substitute FName 1 and LName 3 with shell variable names. $0 will print entire line where the condition matches

To store the output in array, you will have to write the code in 2 lines, something like this :

IFS=$'\n'
my_array+=( $( awk -F ',' -v var="FName 1" -v var1="LName 3" '$2 ~ var || $3 ~ var1 {print $0}' data.csv) )

This will store one line in single index of array.

You can access this array as :

for i in ${my_array[@]}
do
echo $i
done

This will give one line at a time for matched patterns.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you add a line to show me how to do with logical operators and also the most important how can I add the lines in CSV in an array against which the search success.
You can use something like this : awk -F ',' -v var="$fname" '$2 ~ var || $3 ~ var {print $2}' data.csv
what about the array ? returning those lines in which the condition matched.
The answer provided in the comment was only as an example. Since your col 2 and col 3 are different, you will have to store the values in different variables using additional -v option. You can try this command : awk -F ',' -v var="FName 1" -v var2="Lname 3" '$2 ~ var || $3 ~ var2 {print $0}' data.csv You can provide shell variable name instead of the exact name I have given in example.
Please check the modified answer. Hope this helps.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.