Prepending Data to a File

Problem

You want to prepend data to an existing file, for example to add a header after sorting.

Solution

Use cat in a subshell.

temp_file="temp.$RANDOM$RANDOM$$"
(echo 'static header line1'; cat data_file) > $temp_file \
  && cat $temp_file > data_file
rm $temp_file
unset temp_file

You could also use sed, the streaming editor. To prepend static text, note that back-slash escape sequences are expanded in GNU sed but not in some other versions. Also, under some shells the trailing backslashes may need to be doubled:

# Any sed, e.g., Solaris 10 /usr/bin/sed
$ sed -e '1i\
>static header line1
> ' data_file
static header line1
1 foo
2 bar
3 baz

$ sed -e '1i\
> static header line1\
> static header line2
> ' data_file
static header line1
static header line2
1 foo
2 bar
3 baz


# GNU sed
$ sed -e '1istatic header line1\nstatic header line2' data_file
static header line1
static header line2
1 foo
2 bar
3 baz

To prepend an existing file:

$ sed -e '$r data_file' header_file
Header Line1
Header Line2
1 foo
2 bar
3 baz

Discussion

This one seems to be a love/hate kind of thing. People either love the cat solution or love the sed solution, but not both. The cat version is probably faster and simpler, the sed solution is arguably more flexible.

You can also store a sed script in a file, instead of leaving it on the command line. And of course you would usually redirect the output into a new file, like sed -e '$r data' header> new_file, but note that will change the file’s inode and ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.