Unix: prepending a file without a dummy-file?
Asked Answered
C

8

7

I do not want:

$ cat file > dummy; $ cat header dummy > file

I want similar to the command below but to the beginning, not to the end:

$ cat header >> file
Cobalt answered 23/3, 2010 at 20:12 Comment(1)
"appending to the begining of the file" is also known as "prepending".Miscall
C
9

You can't append to the beginning of a file without rewriting the file. The first way you gave is the correct way to do this.

Claudeclaudel answered 23/3, 2010 at 20:15 Comment(0)
L
3

This is easy to do in sed if you can embed the header string directly in the command:

$ sed -i "1iheader1,header2,header3"

Or if you really want to read it from a file, you can do so with bash's help:

$ sed -i "1i$(<header)" file

BEWARE that "-i" overwrites the input file with the results. If you want sed to make a backup, change it to "-i.bak" or similar, and of course always test first with sample data in a temp directory to be sure you understand what's going to happen before you apply to your real data.

Latham answered 22/11, 2011 at 22:29 Comment(0)
P
2

The whole dummy file thing is pretty annoying. Here's a 1-liner solution that I just tried out which seems to work.

    echo "`cat header file`" > file

The ticks make the part inside quotes execute first so that it doesn't complain about the output file being an input file. It seems related to hhh's solution but a bit shorter. I suppose if the files are really large this might cause problems though because it seems like I've seen the shell complain about the ticks making commands too long before. Somewhere the part that is executed first must be stored in a buffer so that the original can be overwritten, but I'm not enough of an expert to know what/where that buffer would be or how large it could be.

Primero answered 27/6, 2013 at 16:46 Comment(2)
UPDATE: This approach seems to have trashed some source files I was trying to prepend a copyright notice onto. It seems to have quit when it encoutered single quotes in the source files and converted escape sequences into the characters. I guess it's not so useful.Primero
UPDATE 2: The command seems to work okay from the command line, but was interpreting the escape sequences from a script I was using to prepend many files at a time. I thought at first it was not using the default 'echo -E' but that still didn't fix it. Apparently the problem had to do with using '#\bin\sh' in the beginning of the script. From an 'sh' instead of a 'bash' command line, the escape sequences get interpreted all the time. I modfied my script so that the first line was '#\bin\bash' instead and things appear to work correctly. Moral: problematic with sh... not sure why?Primero
R
1

You can't prepend to a file without reading all the contents of the file and writing a new file with your prepended text + contents of the file. Think of a file in Unix as a stream of bytes - it's easy to append to an end of a stream, but there is no easy operation to "rewind" the stream and write to it. Even a seek operation to the beginning of the file will overwrite the beginning of with any data you write.

Ringe answered 23/3, 2010 at 20:50 Comment(0)
R
1

One possibility is to use a here-document:

cat > "prependedfile" << ENDENDEND
prepended line(s)
`cat "file"`
ENDENDEND

There may be a memory limitation to this trick.

Romanticist answered 14/2, 2014 at 15:26 Comment(0)
C
0

Thanks to right searchterm!

echo "include .headers.java\n$(cat fileObject.java )" > fileObject.java

Then with a file:

echo "$(cat .headers.java)\n\n$(cat fileObject.java )" > fileObject.java
Cobalt answered 23/3, 2010 at 20:47 Comment(2)
That contains a race condition; sometimes (if not always), the shell will set up your stdout redirection, which will zero out the file, before the second "cat" command is complete.Linage
@pra: fixed? $ echo "$( read -t 1; cat one) \n\n $(cat two)" > one?Cobalt
H
0

if you want to pre-pend "header" to "file" why not append "file" to "Header"

cat file >> header
Herriot answered 22/9, 2014 at 20:59 Comment(0)
T
0

Below is a simple c-shell attempt to solve this problem. This "prepend.sh" script takes two parameters:

  • $1 - The file containing the pre-appending wording.
  • $2 - The original/target file to be modified.
#!/bin/csh
if (if ./tmp.txt) then
  rm ./tmp.txt
endif

cat $1 > ./tmp.txt
cat $2 >> ./tmp.txt

mv $2 $2.bak
mv ./tmp.txt $2
Thormora answered 11/4, 2017 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.