AWK Compare previous value with current.

Hi,
I have one small doubt how to go ahead and process the below requirement.

File Content

1,abc,10
2,xyz,11
3,pqr,12
4,pqr,13
5,pqr,14

Output file expected:

1,mnq,1
1,ddd,2
1,qqq,3
1,sss,4
1,ddd,5
1,eee,6
1,fff,7
1,ddr,8
1,rrd,9
1,der,10
2,dwe,11
3,pqr,12
4,pqr,12
5,pqr,12
12,adhe,0
13,add,1
.
.
.
.
.
.
.

thousands of records

Note that the pqr is repeated 3 times in this case for the 3rd column we have the the 3rd column value of the 1st occurence of pqr copied for the 2nd and 3 occurenct
i.e 4,pqr,13 gets changed to 4,pqr,12
and 5,pqr,14 gets changed to 5,pqr,12

Once the output file is generated then
then i want something like

cat 1 >10
cat 2>11
cat 3 4 5 > 12

I want this to be done dynamically since the number of records might be in thousands atleast

Would appreciate if we can an awk solution for the same.

Regards,
Dikesh Shah

Well, fields 1 and 2 are preserved, save fields of each record out for interrogation with the next, and if last matches for 1 and 2, discard 3 and use old 3.

1 Like

How come your file-in and file-out do not match?
Your sample file has five sample lines, but three of them do not show up in your output. But, there are many lines in the output that are not in the input.

Please clarify.

1 Like

Init the input file:

$ cat infile
1,mnq,1
1,ddd,2
1,ddd,3
1,sss,4
1,ddd,5
1,add,6
1,fff,7
1,ddr,8
1,rrd,9
1,der,10
2,mnq,11
3,pqr,12
4,pqr,13
5,pqr,14
12,adhe,0
13,add,1

Get the output:

$ awk -F , '{if (!a[$2]) {a[$2]=$3} else {$3=a[$2]}}1' OFS="," infile
1,mnq,1
1,ddd,2
1,ddd,2
1,sss,4
1,ddd,2
1,add,6
1,fff,7
1,ddr,8
1,rrd,9
1,der,10
2,mnq,1
3,pqr,12
4,pqr,12
5,pqr,12
12,adhe,0
13,add,6

For your second request, I guess you need export to different files depend on the number on column 3.

awk -F , '{if (!a[$2]) {a[$2]=$3} else {$3=a[$2]}}1' OFS="," infile |awk -F , '{print >$3 ".txt"}'

after that, you will get file 1.txt, 12.txt, etc.

1 Like