5

I have a CSV file with 4 columns: Itemname,Value,Description and component which is quite huge.

I have to generate a template from the above CSV file that displays only the rows of the specified component(say component='abc' which is the search criterion)

terdon
  • 242,166
Blessy
  • 69
  • Unless you edit your question to make it relevant to *nix, this will probably moved to [so] and/or closed. – Anthon Jun 24 '13 at 04:30
  • @Anthon how is this not relevant to *nix? As Ignacio's answer shows, awk is perfect for this. – terdon Jun 24 '13 at 14:23
  • 2
    @tendon I don't see why awk is perfect for this. How does awk e.g. determine the separating character used in the CSV?. This doesn't have to be the comma, something that e.g. Ignacio assumes). This now seems more a generic programming question that belongs on [so] (if not already answered there), which is why I warned the OP. – Anthon Jun 24 '13 at 15:18
  • @tendon see the top answer...https://unix.stackexchange.com/a/80472/311307 – Nic Scozzaro Oct 14 '20 at 13:22

3 Answers3

10

Assuming there are no embedded commas, awk is perfect for this.

awk -F , '$4 == "abc" { print }' input.csv
7

I used another tool in the csvkit: csvgrep.

$ csvgrep -c 4 -m "abc" data.csv > test.csv

This is the resulting contents of the file test.csv:

Itemname,Value,Description,Component
33,34,35,abc

-c is to designate the column to look in. you can also use the header, just make sure you spell it exactly the same, capitals matter:

$ csvgrep -c Component -m "abc" data.csv > test.csv

Itemname,Value,Description,Component
33,34,35,abc

and -m is match pattern, I'm pretty sure there is a way to use regular expressions if you want to get more in-depth in your matching. then it gets put in a new file named test.csv.

slm
  • 369,824
jkabob
  • 71
0

With the following data.csv:

Itemname,Value,Description,Component
1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16
17,18,19,20
21,22,23,24
25,26,27,28
29,30,31,32
33,34,35,abc
37,38,39,40
41,42,43,44
45,46,47,48
49,50,51,52
53,54,55,56
57,58,59,60
61,62,63,64
65,66,67,68
69,70,71,72
73,74,75,76
77,78,79,80
81,82,83,84
85,86,87,88
89,90,91,92
93,94,95,96
97,98,99,100

Using csvkit:

$ csvsql --query "SELECT * FROM data WHERE Component = 'abc'" data.csv
Itemname,Value,Description,Component
33,34,35,abc
Kusalananda
  • 333,661