Your code doesn't make sense. Are you posting the actual code you're using, or a sanitized version of it.
As I said before, you've wrapped the $gettok identifier around another $gettok, but the outer doesn't have a delimiter or a token#. And then on top of that, you're using $remove without any parameter pairs for removing stuff, and without a closing parenthesis - 3 open + 2 closing.

You define %ident then proceed to not use it. As I said before, the 4th parameter is an optional line number to start searching from if you don't want to start at 1. If %domain contains 123456, then it's starting at line 123456. Luckily, your %domain contains a text string, which $read evaluates as the number zero, so it starts at line 1 since 0 is an invalid line#.

You're not using the 'nt' switches, so if line#1 is a number, then $read thinks that's how many lines there's supposed to be in the file, and if the line contains %word or $word then it tries to evaluate them into variables or identifiers.

By doing a wildcard search for "*" of course it's going to match on the 1st line it encounters, even a blank line.

You can use the -s switch when creating the /var to send an info message to status window showing what string is created, or use echo -s to show the value of variables at different $scriptline's