Split out the reading of the first file from reading the second file; gather all lines you want to match (unless you are reading hundreds of thousands of lines to match). Store all lines you want to match, minus the digit at the end, in a set for fast access.
Then scan the other file for matching lines:
def output(input1, input2):
with input1: # automatically close when done
# set comprehension of all lines starting with @, minus last character
to_match = {line.strip()[:-1] for line in input1 if line[0] == '@'}
with input2:
for line in input2:
if line[0] == '@' and line.strip()[:-1] in to_match:
print line.strip()
for i in range(3):
print next(input2, '').strip()
You need to print the matched line too, then read the next three lines (line number 1 was already read).