Skip to main content

Unix, count the number of Count times the substr($0,29,2)of each line of file == "04"an expression is true in awk.

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"

Please adviseHow can I count this?

Unix, count the number of times the substr($0,29,2)of each line of file == "04"

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"

Please advise

Count times an expression is true in awk.

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?

added 24 characters in body
Source Link
Mikel
  • 58.7k
  • 16
  • 136
  • 155

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

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"

Please advise

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"

Please advise

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"

Please advise

Source Link
John
  • 11
  • 1

Unix, count the number of times the substr($0,29,2)of each line of file == "04"

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"

Please advise