Unix: adding a file without a dummy file?

I do not want:

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

I want to look like the command below, but at the beginning, and not to the end:

 $ cat header >> file 
+6
unix append prepend
source share
8 answers

You cannot add to the beginning of a file without overwriting the file. The first way you gave is the right way to do it.

+9
source share

This is easy to do in sed if you can embed the title bar directly in the command:

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

Or if you really want to read it from a file, you can do this using bash help:

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

BEWARE , which "-i" overwrites the input file with the results. If you want sed to back up, change it to "-i.bak" or similar, and of course always check the sample data in the temp directory first to make sure you understand what will happen before you apply to your real data.

+3
source share

The entire dummy file is quite annoying. Here's a 1-linear solution that I just tried, which seems to work.

  echo "`cat header file`" > file 

Ticks make the part inside the quotes first so that it does not complain about the output file, which is the input file. This seems like a hhh solution, but a little shorter. I believe that if the files are really large, this can cause problems, although I seem to have seen the shell complain about the ticks that make the commands take too long. Somewhere, the part that is executed first must be stored in a buffer so that the source text can be overwritten, but the expert is not enough for me to know what / where this buffer will be or how much it can be.

+2
source share

You cannot add a file to a file without reading the entire contents of the file and writing a new file with the added text + contents of the file. Think of a file on Unix as a stream of bytes β€” it is easy to add to the end of the stream, but there is no easy operation to β€œrewind” the stream and write to it. Even the search operation at the beginning of the file will overwrite the beginning with any data that you write.

+1
source share

One possibility is to use a document here:

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

There may be a memory limit for this trick.

+1
source share

Thanks to the correct search query!

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

Then with the file:

 echo "$(cat .headers.java)\n\n$(cat fileObject.java )" > fileObject.java 
0
source share

if you want to postpone the β€œtitle” to the β€œfile” beforehand, why not add the β€œfile” to the β€œTitle”

 cat file >> header 
0
source share

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

  • $ 1 - File containing pre-added wording.
  • $ 2 - The source / 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 
0
source share

All Articles