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?

2
  • 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). Commented Mar 27, 2016 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. Commented Mar 27, 2016 at 15:23

2 Answers 2

4

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

grep -cE '^.{28}04' data1.txt
1
  • 1
    Also egrep and company (fgrep, rgrep) are deprecated. When possible use grep -E. Commented Mar 27, 2016 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
1
  • 2
    Alternately, END{print +count}will print 0 for a zero count without the need for initialization Commented Mar 27, 2016 at 16:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.