I would create a macro that operates on the selected files in the Finder. See:
Use the Read a File action to read the file
Use the Search using Regular Expression action to get the first line.
Or you can use the For Each action and the Lines In collection to read the lines from the file, and break out of the loop immediately.
Then use the Rename a File action to rename the file.
For safety sake, you might want to ensure that the initial file extension is “txt” (hopefully the files already have a .txt extension).
You also might want to limit the length and/or characters of the line before renaming.
And for heavens sake, make sure you have full and complete backups of your entire Mac and this folder specifically before testing the macro.