Prepending text to a file. How?
Published:
What? and why?
Prepending one file (or just a string of content) to another file, while not fairly common, is an operation that usually needs to be done in batches. As such, it’s useful to know some tricks to automate this process. Unlike the append operator (>>
) in UNIX, there is no unique prepend operator, meaning a straightforward solution can be hard to come by.
Click the drop-down menu to enjoy my anecdote about file prepending.
Story time!
Imagine being me, a teaching assistant for an Intro to Programming class. It's mid-March, I have 3 things overdue since yesterday, and I need to finish grading the latest problem set. (I'm pictured below)
So, like any rational person, I spend an evening creating an auto-grader pipeline - and feel very smart because I made students return some integer from a function, which I can easily test against. :-)
The last step in this grading pipeline is to add an annotated feedback template, showing what the student got right (and wrong). Back in the heydays, Abrar had enough energy to. But Abrar has aged... queue the need to prepend content from one file to another!
[If you think this is a ridiculous reason to go down this rabbit hole, then you are right. But this is a blog, and I'm trying to be funny.]
Note: This was at a time before I knew testing frameworks are a thing...
How?
Input redirection and cat
The trick to using cat
is to remember that cat
can “conCATenate” multiple files. As such, cat
can be used to concatenate the string to prepend and the file to prepend in. The string to prepend is piped into the standard input (-
), following which is some temporary file moves.
echo 'STRING' | cat - FILE.txt > temp && mv temp FILE.txt
Alternatively, if the content to be prepended resides in another file, the initial echo
command can be skipped altogether and the standard input stream to cat
can be replaced with the file containing the text to prepend.
sed
sed
can be used to perform basic text transformations on an input stream, hence fitting the bill for this task. sed
can be configured to perform in-place text transformations (with the -i
flag). Furthermore, a substitution directive for the first character of the first line may be provided to perform the necessary prepending - since all edits will occur in-place. For example:
sed -i.old '1s;^;STRING;' FILE.txt
Warning: This Does NOT work if the input file is empty (0 bytes). Moreover, sed
both misinterprets newlines as special pattern matching characters and has the possibility of not being cross-platform at least between GNU and BSD sed
.
vim
Vim can be opened in Ex mode (-e
) which allows entry of Ex
commands, typically useful for batch processing applications. The command required can be provided from the CLI using the -c
flag, which causes execution of a command after loading the first file provided.
vim -e -c '0r [file-with-content]|x [file-to-prepend-to]