Unix: prepending a file without a dummy-file?

跟風遠走 提交于 2020-01-12 07:37:29

问题


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

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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



回答7:


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

cat file >> header



回答8:


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


来源:https://stackoverflow.com/questions/2503254/unix-prepending-a-file-without-a-dummy-file

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