awk query

Hi,
I have a sample file in the following format.

000013560240|000013560240|001|P|155|99396|0||SS00325665|
000013560240|000013560240|002|P|17|99000|0||SS00325665|
000013560240|000013560240|002|F|-17|99000|0|R|SS00325665|
000013560240|000013560240|003|P|20|82270|0||SS00325665|
000013560240|000013560240|004|F|17|99000|0||SS00325665|
000012817460|000012817460|001|P|129.98|V2202|0||SS00425539|
000012817460|000012817460|001|F|-129.98|V2202|0|R|SS00425539|
000016132850|000016132850|001|P|59.99|E0607|0||SS00154667|
000016132850|000016132850|001|F|-59.99|E0607|0|R|SS00154667|
000016132850|000016132850|002|P|24|E1399|0||SS00154667|
000016132850|000016132850|002|F|-24|E1399|0|R|SS00154667|
000016132850|000016132850|003|F|59.99|E0607|0||SS00154667|
000016132850|000016132850|004|F|24|E1399|0||SS00154667|

I need the output file as below :

000013560240|000013560240|001|P|155|99396|0||SS00325665|
000013560240|000013560240|003|P|20|82270|0||SS00325665|
000013560240|000013560240|004|F|17|99000|0||SS00325665|
000016132850|000016132850|003|F|59.99|E0607|0||SS00154667|
000016132850|000016132850|004|F|24|E1399|0||SS00154667|

The logic is tom compare fields 3 and 5 and if field 5 has corresponding negative value which matched to the positive value based on field 3 , those records should be ignored.

This can be done in Perl using arrays but was wondering if this can be achieved through awk or sed or a shell script. Any help is appreciated.

The logic seems to be more than just comparing fields 3 and 5 in previous lines. Column one also seems important as well as column 4. The requirements are not clear,

Try (reading file twice, original order)

awk -F\| 'NR==FNR{if(($3,-$5) in A) delete A[$3,-$5]; else A[$3,$5]; next} ($3,$5) in A' file file

The input file is specified twice..

1 Like

As long as the lines with matching field 3 values are on adjacent lines (as they are in the sample input), there is no need to read the input file twice:

awk -F '|' '
save {	if($3 == last3 && $5 == -last5) {
		save = 0
		next
	}
}
{	if(save) print last0
	save = 1
	last0 = $0
	last3 = $3
	last5 = $5
}
END {	if(save) print last0
}' sample

produces the desired output only reading the input once.

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of the default /usr/bin/awk .