get text between keywords in log file

One line of sed can do this for you:

sed -n 's/.*1234 \(.*\)9876.*/\1/p' textfile.txt > log


normally you can do this with grep and the -o param.

-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. 

so it would be something like:

 grep -Po '1234.*9876' >> log

Not 100% sure about the regex btw, I did not test it