sed -i -e '/Leonardo/ s/$/ Narnie/' database_readers.txt
sed's in-place edit option (
-i) to search for all lines matching the regexp
Leonardo, and replace the end-of-line anchor (
$) with a single space and the word
Narnie. This effectively appends the
Narnie to the end of the matching lines.
Be careful with the regexp search pattern - it's easy to match more lines than you were expecting. Test first with:
sed -n -e '/Leonardo/ s/$/ Narnie/p' database_readers.txt
-n option combined with the
p at the end of the
sed print out ONLY the lines that were changed. Do NOT use this with
sed -i unless you want to delete ALL unchanged lines from your file.
Anyway, if that prints out more lines than you were expecting then change
Leonardo DaVinci or
^753[[:space:]]\+ (assuming that the number at the beginning of the line is a unique ID), and try again until it changes only the line(s) you were expecting it to.
For anything more complicated than that, you may want to look into using
sqlite (or even
postgresql) rather than a flat text file. Or use a language like
perl which has many library modules for manipulating flat-file databases.