John Sim
John Sim

Reputation: 11

How to iterate through a file and append data?

In my awk code, I am stuck trying to find a way to iterate through file (data1.txt) and and append line of file and count line of file if the substr($0,29,2)of the line of file == "04"

data1.txt

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

which in this case only 2 lines fulfill the condition (substr($0,29,2)=="04")

    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"

Upvotes: 0

Views: 50

Answers (1)

Ed Morton
Ed Morton

Reputation: 203522

To do what it LOOKS like you're trying to do is just:

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

but your input file doesn't HAVE any cases where the 29/30th chars are 04 so idk...

Upvotes: 1

Related Questions