remove 1st and last last values of a key

For every specific keys i.e. 1st and 4th columns (a1 and ABC_001144992) remove 1st and last value (bold ones - 87942437 and 87952030 ) and print remaining
input

a1 	87942437 	87943147 	1E 	ABC_001144992
a1 	87945162 	87945276 	2E 	ABC_001144992
a1 	87949524 	87952030 	3E 	ABC_001144992
a1 	89967353 	89968253 	1E 	ABC_201644
a1 	90111618	 90111750 	2E 	ABC_201644
a1 	90112369 	90112457 	3E 	ABC_201644
a1	 90112712 	90112932 	4E 	ABC_201644
a1 	90114701 	90116577 	5E 	ABC_201644

output

a1 	87943147 	87945162 	1I 	ABC_001144992
a1	 87945276 	87949524 	2I 	ABC_001144992
a1 	89968253 	90111618 	1I 	ABC_201644
a1 	90111750 	90112369 	2I 	ABC_201644
a1 	90112457 	90112712 	3I 	ABC_201644
a1 	90112932 	90114701 	4I 	ABC_201644

Untested:

awk 'p {
  n = split(p, t)
  if (t[n] == $NF) 
    print t[1], t[3], $2, t[4], t[5]
  }
{ p = $0 }
  ' infile  
1 Like

A minor tweak to radoulov's solution which fixes the 4th column:

awk 'p {
  n = split(p, t)
  if (t[n] == $NF) 
    print t[1], t[3], $2, ++i"I", t[5]
  else
    i=0
  }
{ p = $0 }
  ' infile
1 Like

Could you please explain the code ?