UNIX script to compare 3rd column value with first column and display

Hello Team,

My source data (INput) is like below

EPIC1	router	EPIC2	Targetdefinition
Exp1	Expres	rtr1	Router
SQL		SrcQual	Exp1	Expres
rtr1	Router	EPIC1	Targetdefinition

My output like

SQL		SrcQual	Exp1	Expres
Exp1	Expres	rtr1	Router
rtr1	Router	EPIC1	Targetdefinition
EPIC1	router	EPIC2	Targetdefinition

I need a Unix script to search 3rd column value in 1st column and display the records in sorted order like above output.

Can someone help me how to achieve this.
Thanks in advance!!!
Regards,
Sekhar Lekkala.

Hi, try something like:

awk '
  {
    Rec[$1]=$0
    Col3[$3]
    Next[$1]=$3
  } 

  END {
    for(i in Rec)          # find first record
      if(!(i in Col3))
        break
    while(i in Rec) {      # print until next record does not exist
      print Rec
      i=Next 
    }
}' file
1 Like

Hello sekhar.lsb,

I am confused. If you file is called INput, what is wrong with just sort INput ?

How is EPIC1 router EPIC2 Targetdefinition to match up?

Can you provide more data and show what we need to match and what to avoid please. I'd like to help, but the rules you want to follow are not clear.

Thanks, in advance,
Robin

Thank you so much, above code is working fine.
I have question, in first column value is same for two or more columns then in out put is skipping duplicate rows
Now my input is like below:

rtr1	        Router	EPIC3	Targetdefinition
Exp1	Expres	rtr1	Router
SQL         SrcQual	Exp1	Expres
rtr1	Router	EPIC1	Targetdefinition

Expecting output like

SQL	SrcQual	Exp1	Expres
Exp1	Expres	rtr1	Router
rtr1	Router	EPIC1	Targetdefinition
rtr1	        Router	EPIC3	Targetdefinition

You are welcome.
You could try:

awk '
  {
    if($1 in Rec)              # if $1 already present in the records then append 
      Rec[$1]=Rec[$1] RS $0    # a newline character (RS) with the next line
    else
      Rec[$1]=$0
    Col3[$3]                             
    Next[$1]=$3
  } 

  END {
    for(i in Rec)
      if(!(i in Col3))
        break
    while(i in Rec) {
      print Rec
      i=Next 
    }
}' file

Output:

SQL         SrcQual	Exp1	Expres
Exp1	Expres	rtr1	Router
rtr1	        Router	EPIC3	Targetdefinition
rtr1	Router	EPIC1	Targetdefinition

--
Do multiple records with the same first field only occur for "leaf" (last) values, or can they occur on all levels?
If the latter is the case, could you be more elaborate in what variations can occur and what needs to happen?

Thank you so much Scrutinizer!!!!
This is really great forum. This is great help for me. Its working as per my requirement.

Regards,
Sekhar Lekkala.