Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to count the number of words and print the matched pattern lines which matches exactly with following pattern:

abc-ERROR:

The input File contains:

# abc-ERROR: xyxxkkfgfr
# def-Error: aaaaaa
# abc-ERROR.cpp

The output should be:

 1   (count)
 # abc-ERROR: xyxxkkfgfr   (line)
share|improve this question
add comment

2 Answers

You can use grep to get the lines and grep -c to get the number of lines. If you do not like running grep two times, you can use tee and the process substitution (the following is the bash syntax):

grep abc-ERROR: input.txt | tee >( wc -l )
share|improve this answer
    
No its not working means not giving any output –  Bhushan J Apr 17 '13 at 10:02
    
@Bhushan: Works for me. What shell are you running? –  choroba Apr 17 '13 at 10:51
    
grep -w make-ERROR "C:\Documents and Settings\bj3\comp_appli.txt" Is it correct syntax..? because its not working at my side.. I m running it through simple shell file (ksh) –  Bhushan J Apr 17 '13 at 11:11
    
Sorry I tried wrong one not yours... Thanks a lot –  Bhushan J Apr 17 '13 at 11:13
    
I am getting this error : syntax error: got (, expecting Word so, can you please tell me the correct syntax –  Bhushan J Apr 17 '13 at 11:15
show 2 more comments

How is this:

$ cat file
# abc-ERROR: xyxxkkfgfr
# abc-ERROR: xyxxkkfgfr
# abc-ERROR: xyxxkkfgfr
# def-Error: aaaaaa
# abc-ERROR.cp
# abc-ERROR: asdgsdgaaf
# abc-ERROR: asdgsdgaaf
# abc-ERROR: tttttttttq

$ awk '/abc-ERROR: /{a[$0]++}END{for(k in a) printf "%d\t(count)\n%s\t(line)\n",a[k],k}' file
1   (count)
# abc-ERROR: tttttttttq (line)
2   (count)
# abc-ERROR: asdgsdgaaf (line)
3   (count)
# abc-ERROR: xyxxkkfgfr (line)
share|improve this answer
add comment

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.