0

I am looking for an awk solution which would fetch lines which are unique based on 2nd column when the 1st & 3rd and 4th columns are the same.

constraints :

  1. column 3 and column 4 has to be same for the rows (two consecutinve rows sorted on the 3rd column)
  2. if 1 is true, check if column 1 is same for those rows from step 1.
  3. then check if rows from step 2 have same column 2 values , if not then print out the rows (both rows) else print none of both rows.

input file :-

AB|abcd|0001|Active
AB|efgh|0001|Active
AB|efgh|0001|Closed
CD|xyz|0002|Active
CD|pqr|0002|Active
EF|xyz|0003|Active
EF|xyz|0003|Closed

output :-

AB|abcd|0001|Active
AB|efgh|0001|Active
CD|xyz|0002|Active
CD|pqr|0002|Active

tried below code but it gives me same $2 values

awk -F'|' '
  BEGIN{OFS=FS}
  $4 ~ /Active/ && a[$3]++ && $2 != c {
    print r ORS $0
  }
  {r=$0; c=$2}' <(sort -k3,3 file)
0

1 Answer 1

1
<infile sort -t'|' -k3,4 -k1,2 |
 awk -F'|' '
     ($3==p[3] && $4==p[4] && $1==p[1] && $2!=p[2] ) && 
      NR>1{ print buf ORS $0; next }
     { buf=$0; split($0, p) }'
2
  • 1
    Yes this works perfectly. Can you please elaborate on the usage of buf=$0 please Jun 26, 2022 at 12:08
  • 1
    @NadiaAlice that is backing up the current line so when the next processing line was matched with your conditions then that (previous line in buf) will be printed first then the current processing line as in print buf ORS $0 Jun 26, 2022 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .