Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Test if a value is in a csv file in bash

I have a 3-4M lines csv file (my_csv.csv) with two columns as :

col1,col2
val11,val12
val21,val22
val31,val32
...

and I would like to check if a string test_str is into the col2 values.

In python it would be something as :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd
df = pd.read_csv('my_csv.csv')
test_str = 'val42'

if test_str in df['col2'].to_list():
    # Expected to return true
    # Do the job

But I have no clues how to do it in bash.

>Solution :

awk is most suited amongst the bash utilities to handle csv data:

awk -F, -v val='val22' '$2 == val {print "found a match:", $0}' file

found a match: val21,val22

Ab equivalent bash loop would be like this:

while IFS=',' read -ra arr; do
   if [[ ${arr[1]} == 'val22' ]]; then
      echo "found a match: ${arr[@]}"
   fi
done < file
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading