Grep values on alternate lines

Hi,

I have a file like

2011|ACC|.*
2013|ACC|.*
2011|ACCC|.*
2013|ACCC|.*
2013|ACCV|.*
2011|ADB|.*
2013|ADB|.*
2011|ADBC|.*
2013|ADBC|.*
2011|AIA|.*
2013|AXJ|.*
2013|NNN|.*

.* represnts any alphanumeric characters after this part of the string

I need a code to return only the rows which has same value on the second field but different value on first

for eg:
for above piece of data, code should return

2011|ACC|.*
2013|ACC|.*
2011|ACCC|.*
2013|ACCC|.*
2011|ADB|.*
2013|ADB|.*
2011|ADBC|.*
2013|ADBC|.*

any suggestions ?

[OS Info : Red Hat Enterprise Linux Server release 5.3 Beta]

Thanks
Sam

Try:

awk -F"|" '$1!=a[$2]&&a[$2]{print b[$2];print}{a[$2]=$1;b[$2]=$0}' file
1 Like

figured out the solution myself, thanks for your help

Share it please :wink: