I have to display the record count of columns matching "CD" from my input csv file. Value CD is appearing in second column in my file input.csv.
I am trying with below command but it is not working as expected.
awk -F',' '$2=/CD/ { count++ } END { print count }' input.csv
My input data:
1234,CD,xyz,abcd
01235,AB,kasdjk,aaaaa,fff
898,CD,laklksas,lsjdjdj,lkjsaj
111,CD,lkakskaks,jjjjjj
3455,00,ksajkjsa,kkkkk
59995,99,asllsal,99898,00,kkk,99
99,00,lkjjjsa,00,99,hhhh
Expected output:
3 if i check the count of "CD"
2 if i check the count of "00"
The operator to match with a regexp is ~
, not =
.
awk -F',' '$2 ~ /CD/ { count++ } END { print count }' input.csv
If you want to do an exact match, not a regular expression, use ==
and a string:
awk -F',' '$2 == "CD" { count++ } END { print count }' input.csv
No comments:
Post a Comment