Read in my first input line and store it in $oldline Now loop over the rest of the input lines if the current line is the same as $oldline, then increment our count otherwise (that is, the current line is different) print $oldline and the counter reset the counter set the current line as the next line to keep track of by storing it in $oldline Loop if there's something in $oldline (which will always be the case), print it