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.

I have a scenario that a csv file has the values of :

abcd FAILURE 7

abcd SUCCESS 12

efgh SUCCESS 3

efgh FAILURE 5

mnop SUCCESS 6

mnop SUCCESS 4

abcd FAILURE 5

efgh SUCCESS 2

mnop SUCCESS 1

abcd FAILURE 7

mnop SUCCESS 3

abcd FAILURE 5

Initial grep with abcd/efgh/mnop with failure or success condition then i need a value of the matching conditions addition

i need the required output like this please help me

for example :

abcd FAILURE 7

abcd FAILURE 5

abcd FAILURE 7

abcd FAILURE 5

--------------------

abcd FAILURE **24**

abcd SUCCESS 12

------------------

abcd SUCCESS **12**

but i dont want to give any static string in condition.

thanks in advance.

share|improve this question
    
i dont know which string consist in the csv file so cannot give the static string in the grep –  manu2711 Feb 6 at 11:42

2 Answers 2

Using grep -c option to count occurences of a search-string. echo outputs the text and uses a sub-shell for the grep count.

echo "abcd FAILURE $(grep -c "abcd FAILURE" filename)" 
echo "abcd SUCCESS $(grep -c "abcd SUCCESS" filename)" 
share|improve this answer

May be you want something like this:

awk '
BEGIN {
    fail = suc = 0
}
$2 == "FAILURE" {
    fail += $3
    next
}
$2 == "SUCCESS" {
    suc += $3
}
END {
    printf "%s:%s\n%s:%s\n", "SUCCESS",suc,"FAILURE",fail
}' inputFile

Output with you file:

SUCCESS:31
FAILURE:29
share|improve this answer

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.