To print the lines for which the last word is something,
awk comes to mind:
awk '$NF == "123"' # string comparison
awk '$NF == 123' # number comparison, would also match on 0123, 123.0, 1.23e2
awk '$NF == ENVIRON["ENVVAR"]' # number comparison if both $NF and $ENVVAR
# look like numbers, string otherwise
awk, those words by default are blank-separated. Depending on the implementation blank may be only space or tab characters, some may include other Unicode blank characters if classified as such in the locale. The
awk implementation also includes vertical spacing characters including the carriage return character.
If your file is formatted with MS-DOS line delimiters, that is the CR LF sequence of characters as opposed to just LF, then except with busybox awk, the above won't work. And even with busybox awk, the output lines would still have those problematic control characters that shouldn't be there on Unix.
So you should preprocess the input with
d2u to convert the line endings to Unix format.
< file.dos dos2unix | awk '$NF == 123'
Your input file looks like it's made of records that are separated by blank lines. If you want to print the full records for a book number, you could do:
< file.dos dos2unix | awk -v RS= -F '\n' '
field[split($1, field, " ")] == ENVIRON["BOOKNUMBER"]'
There, we set the record separator to the empty string to enter the paragraph mode (records separated by blank lines), and the field separator to newline (aka LF) so the first line of each record is in
$1 (the first field). We split that first line with the special
" " field separator which is the default word splitting into the
field array. And then compare the last element of that array (
split() returns the number of elements, so
field[split()] is the last element) with the content of the
$BOOKNUMBER environment variable.