1

In unix/awk code

Essentially, I need to iterate through the file (data1.txt) and count the number of times the substr($0,29,2) of each line of the file == "04".

data1.txt:

6597243042 20160305001100003140152852153019000127000200
6597243042 20160305001100003140170306190306020000000200
6597243042 20160305001100003140170552190552020000000200
6597243042 20160305001100003140201430201543000113000400
6592311319 20160305041100003460072719072839000120001200
6592311319 20160305041100003460072927072952000025001200

In this case only 2 lines of the file (data1.txt) fulfill the condition (substr($0,29,2)=="04")

I am stuck at finding a way to do so

Below is my unix/awk code

Filename="def"
file="data1.txt"
#awk '{count1=0}'
while IFS= read line
do

   awk '{ if (substr($0,29,2)=="04") {print substr($0,29,4)}}' 

done <"$file"

How can I count this?

  • 3
    None of the lines in your text file match such condition. Beside, you're not reading $line in the while loop. And the loop wouldn't be needed either, use just AWK: awk 'substr($0,29,2)=="04" {print}' file (again, in your file no line matches such condition). – kos Mar 27 '16 at 13:37
  • 1
    I disagree on moving to SO, code inside awk have beed discussed here, beside OP is a bit about feeding awk with a file. – Archemar Mar 27 '16 at 15:23
4

Another approach (thanks to Archemar + kos for avoiding use of deprecated egrep and using -c)

grep -cE '^.{28}04' data1.txt
  • 1
    Also egrep and company (fgrep, rgrep) are deprecated. When possible use grep -E. – kos Mar 27 '16 at 17:16
3

simply try

awk 'BEGIN { count = 0 ;} substr($0,29,2) == "04" { count++ ; } END { print count ;}' files

where

  • substr($0,29,2) == "04" search fo condition
  • { count++ ; } if found increase count
  • END at the end
  • { print count ;} print it.

edit:

  • count=0 thanks to @steeldriver

awk command line can be entered as below for readability.

awk 'BEGIN { count = 0 ;} 
     substr($0,29,2) == "04" { count++ ; } 
     END { print count ;}' files
  • 2
    Alternately, END{print +count}will print 0 for a zero count without the need for initialization – iruvar Mar 27 '16 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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