Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sample.csv

DSN1,abc,FAILURE,12,24,45
DSN1,def,FAILURE,12,78,65
DSN1,abc,FAILURE,12,24,45
DSN1,abc,FAILURE,12,24,45
DSN1,abc,FAILURE,12,24,45
DSN1,def,FAILURE,12,78,65
DSN1,abc,FAILURE,12,24,45

i need the count of failures in the above sample.csv with response as

abc 5
def 2

but i dont have mention the abc/def in the script. because i have given a sample scenario in my case many of the string like abc are there so i need that string and count with failure.

please suggest me

Thanks in advance

share|improve this question
    
That sample CSV excerpt has different number of fields on each line; are you sure that's how the file is formatted? –  DopeGhoti Jan 23 at 3:17
    
lack of proper markup –  Ricky Beam Jan 23 at 3:19

3 Answers 3

A simple solution is to use the following pipe:

<Sample.csv grep '^[^,]*,[^,]*,FAILURE' | cut -d, -f2 | sort | uniq -c
  • grep will extract lines with FAILURE in the third column
  • cut will extract the column (separator , column number 2)
  • sort will sort the extracted column (The same values will be next to each other.)
  • uniq will remove repeated values, the -c option will show counts of every unique value

You can also insert other filters into the pipe as needed. (for example grep at the beginning).

share|improve this answer

Ricky's comment is how I would do it, but if you want a solution specific to grep you could do the following:

$ for i in {abc,def}; do echo -n "$i: "; grep -c $i input.txt; done;

this will output the expected:

abc:5
def:2

Update

If you do not want to include the search keys in the for loop I don't see how to do it simply with just grep.

You could do it with awk though.

awk 'BEGIN{FS=","}//{a[$2]++}END{for(x in a) print x,a[x]}' test.txt

Explanation:

FS="," -- set record seperator to comma

// -- match all lines

We create an associate array called 'a'

a[$2]++ -- each pattern matched we take 2nd column and increment count

END { .. } -- this block is run when done with all matching. we iterate over all elements printing key and count.

share|improve this answer
    
in for loop i dont want to mention any strings. string should be more then 2 i need dynamic string with the count. –  manu2711 Jan 23 at 5:29
    
You should add FAILURE as a condition for the count, e.g.: $3 == "FAILURE" { a[$2]++ } .... –  Thor Jan 23 at 10:41

please find the same:

awk < Sample.txt -F',' '{print $2}' | sort |uniq -c| grep "FAILURE"

Thanks for your answers :)

share|improve this answer
1  
Is this supposed to answer your own question? If so, it would be nice if you could add a short explanation. –  Oliver Salzburg Jan 24 at 10:04
    
This will not work as the string FAILURE does not appear in the second column in your example. (grep could be at the pipe beginning though) You should also add some explanation. –  pabouk Jan 24 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.