-1

I have the following rows in my data

029 32389047809475037568907376 000000 00000000000
029 32389047809475037568907376 000000 00000000000
029 32389047809475037568907376 000000 00000000000
029 32389047809475037568907376 000000 00000000000
029 32389047809475037568907376 000000 00000000000 

and I want to count number of "029"s beginning in these rows in my log view.

How do I do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
subash
  • 1
  • 2
  • 3
    This is a bit broad. Could you be a bit more specific and show some input data, expected output and what you have tried so far? – fedorqui Apr 24 '15 at 10:17
  • 2
    @fedorqui A bit broad is an understatement! –  Apr 24 '15 at 10:31
  • Right now i can't post a picture to let you know the file.But, i can tell the file and its pattern: – subash Apr 24 '15 at 10:32
  • 029 32389047809475037568907376 000000 00000000000 this kind of rows are millions and i want to count number of 029 in these rows.Please help me – subash Apr 24 '15 at 10:32
  • 1
    No, please `edit` your question to show this, since code or text does not show properly in comments. You may want to read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). – fedorqui Apr 24 '15 at 10:36
  • 1
    So you want to know how many times `029` appears in general? or in the beginning of a line? or how many lines have `029`? Please clarify it. – fedorqui Apr 24 '15 at 10:47
  • possible duplicate of [How can I count the occurrences of a string within a file using bash?](http://stackoverflow.com/questions/6741967/how-can-i-count-the-occurrences-of-a-string-within-a-file-using-bash) –  Apr 24 '15 at 10:58

2 Answers2

2

A solution without awk:

grep -c "^029" file

find all lines starting with 029 and count them (thats what the -c option does)

martin
  • 3,149
  • 1
  • 24
  • 35
  • How do i convert this huge log file into "file"(text format/any format) as you mentioned ? – subash Apr 24 '15 at 10:56
  • 1
    Use `grep -c` instead –  Apr 24 '15 at 10:57
  • @martin these rows are a million in my log file i.e after i write command " view cid_generation.GG.20150422.rpt " so i can perform only a limited operations when i view this file. – subash Apr 24 '15 at 11:02
  • @martin so where will the contents of my file go? as metioned _view ... | grep -c "^029" file_ will they go into the file? – subash Apr 24 '15 at 11:07
  • No, just do `view .. | grep -c "^029"`, without the file, @subash – martin Apr 24 '15 at 12:57
1

You could use uniq for this:

$ uniq -cw 3 file
      5 029 32389047809475037568907376 000000 00000000000

Options:

-c, --count prefix lines by the number of occurrences

-w, --check-chars=N compare no more than N characters in lines


If you don't have the -w option for your version of uniq a standard awk solution would be:

$ awk '/^029 /{c++}END{print c}' file
5
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • That is a nice solution with `uniq`, I didn't now that. – martin Apr 24 '15 at 10:56
  • You will need a version of GNU uniq for the -w option. OSX is a BSD variant so you will have to install the GNU coreutils with your favourite package manager for all the extra goodies. – Chris Seymour Apr 24 '15 at 13:02