Shell – How to concatenate two files on the fly and reference result as new file

filesshell-script

I want to concatenate two files, i.e. staticEntries.dic and dynamicEntries.dic. As the names might show, the content of staticEntries.dic stays the same over time (contains common words), but to the contrary, dynamicEntries.dic might change over time (contains abbreviations that are extracted from DB). In my editor, I want to provide a mydict.dic, which contains the content of both files. I could do that via Shell, of course, but then I would have to replace the dynamicEntries.dic stuff from the mydict.dic file before concatenating a new version of dynamicEntries.dic to my resulting mydict.dic. -> I would like to create a mydict.dic file that contains a command to concatenate the two other files, but I don't know how to do that. Since it is not executed or called by my editor (I guess), I can't use bash commands.

Subsequently a simple example:

Content of staticEntries.dic

house
dog
horse

Content of dynamicEntries.dic. This content is generated based upon a database, so it changes over time.

EGB38
PD
UH7ZT

Pursued content of mydict.dic

house
dog
horse
EGB38
PD
UH7ZT

If I do that combination via shell, I have a problem if a new version of the file dynamicEntries.dic is generated: how to avoid duplicate entries in the mydict.dic file? So I would like to put the concatenation command in a file and reference it as a normal file I can provide as dictionary file to my editor. I don't know whether this is possible or if I have to apply another approach.

Best Answer

You could possibly write a script that sits behind a named pipe and dumps the contents of both staticEntries.dic and dynamicEntries.dic whenever it's opened and read from. Take note of the pipe being closed and terminate output until it is opened again.

But you'd have to leave that script running in the background, and remember to start it up again after logout/login or reboot.

More importantly, it is not a novice shell programming task.

Sometimes (usually), the simplest solution is best.

It is far simpler to just create a Makefile that defines mydict.dic as being dependant on the other two files and remembering to run make to update it when you need it. or just a shell script - the advantage of a Makefile is that you could also run it from cron and it would only update the target file (mydict.dic) if either of the source files had changed.

for example:

#!/usr/bin/make -f

all: mydict.dic

mydict.dic: staticEntries.dic dynamicEntries.dic
        cat staticEntries.dic dynamicEntries.dic > mydict.dic.tmp
        mv mydict.dic.tmp mydict.dic

the lines with cat and mv start with a tab, not spaces.

The concatenated file is created as a tempfile first and then moved into place, so the replacement of the old with the new is an atomic operation. this is done so that whenever you use the file, you have either the complete old version or the complete new version, but never a partial version of the new.

if either of the source .dic files are in a different directory, you'll need to specify the full pathnames to the files.

Related Question