awk 'NR % 5 == 1 {
# 1st line in a set of 5 lines.
# Set the saved output string to the 4th field in this line.
o = $4
next
}
NR % 5 {# 2nd through 4th line in a set of 5 lines.
# Add the 4th field in this line to the saved output string.
o = o " " $4
next
}
{ # 5th line in a set of 5 lines.
# Print the result of processing this set of 5 lines.
printf("peak%d %s %s\n", ++oc, o, $4)
}
END { # If the input file had less than 5 lines in the last set, print the
# partial set.
if(NR % 5) printf("peak%d %s\n", ++oc, o)
}' file
If you are using a Solaris/SusOS system, use /usr/xpg4/bin/awk or nawk instead of awk .