Unix: prepending a file without a dummy-file?

青春壹個敷衍的年華 提交于 2019-12-03 13:17:52

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.

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.

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.

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.

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.

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
larry

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

cat file >> header
Nasser Abreu

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!