Saturday, February 5, 2022

[SOLVED] Parsing key value in an csv file using shell script

Issue

Given csv input file

Id Name Address Phone
---------------------

100 Abc NewYork     1234567890  
101 Def San Antonio 9876543210  
102 ghi Chicago     7412589630  
103 GHJ Los Angeles 7896541259

How do we grep/command for the value using the key?

if Key 100, expected output is NewYork


Solution

You can try this:

grep 100 filename.csv | cut -d, -f3

Output:
New York

This will search the whole file for the value 100, and return all the values in the 3rd column of the matching rows.



Answered By - Joseph K.
Answer Checked By - Senaida (WPSolving Volunteer)