将文件中的文本插入到另一个文件的开头 - bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3784672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting text from a file to the beginning of another file - bash
提问by Sriram
I am trying to insert some copy-right information into the beginning of some source files. I know that using sed in the following way:
我试图在某些源文件的开头插入一些版权信息。我知道以下列方式使用 sed:
sed "1iSome copyrighted information." sample.txt
would insert the text "Some copyrighted information" to the beginning of sample.txt.
I need to insert text from a file to the beginning of sample.txt. Is there any way in sed that I could use a catcommand for the above purpose, say something like the following?:
将在 sample.txt 的开头插入文本“一些受版权保护的信息”。
我需要将文件中的文本插入到 sample.txt 的开头。在 sed 中是否有任何方法可以将cat命令用于上述目的,请说以下内容?:
sed "1i{cat header.txt}" sample.txt
I have googled for the above and have not found exactly what I have been looking for. Any help on this is most welcome.
我在谷歌上搜索了上述内容,但没有找到我一直在寻找的内容。非常欢迎对此提供任何帮助。
Thanks.
谢谢。
回答by Paused until further notice.
If you have GNU sed:
如果你有 GNU sed:
sed -i -e '2{x;G};1{h;rheader.txt' -e 'd}' sample.txt
sample.txt must be contain at least two lines.
sample.txt 必须至少包含两行。
This method works even if the header file contains characters that are special to sed.
即使头文件包含对sed.
Explanation:
解释:
-i- edit the file in place-e- break the script up into sections - this is necessary in this case to delimit the end of the header filename (it could also be delimited by a newline)1{h;- on the first line of the file (sample.txt) save it to hold spacerheader.txt- read in the header file (header.txt)d}- delete the original first line of the file from pattern space and end processing of line 1 - deleting it here prevents an extra blank line from being inserted at the beginning of the file- the header file contents are now output
2{x;- when line 2 of the file (sample.txt) is read, swap it into hold space and swap hold space (containing the original line 1) into pattern spaceG}- append hold space onto the end of pattern space (which now contains original lines 1 and 2) and complete processing of line 2- lines 1 and 2 are now output then processing continues for the rest of the file which consists of simply reading and outputting each line.
-i- 就地编辑文件-e- 将脚本分成几个部分 - 在这种情况下,这是必要的以分隔头文件名的结尾(也可以用换行符分隔)1{h;- 在文件的第一行 (sample.txt) 保存它以容纳空间rheader.txt- 读入头文件(header.txt)d}- 从模式空间中删除文件的原始第一行并结束第 1 行的处理 - 在此处删除可防止在文件开头插入额外的空行- 现在输出头文件内容
2{x;- 当读取文件 (sample.txt) 的第 2 行时,将其交换到保持空间并将保持空间(包含原始第 1 行)交换到模式空间G}- 将保留空间附加到模式空间的末尾(现在包含原始第 1 行和第 2 行)并完成第 2 行的处理- 现在输出第 1 行和第 2 行,然后继续处理文件的其余部分,包括简单地读取和输出每一行。
Edit:I removed a superfluous command from the version I originally posted.
编辑:我从最初发布的版本中删除了一个多余的命令。
回答by sdaau
Sorry to use up a whole answer - just for the sake of formatting :) Basically, the same answer from @tangens, but without the temporary file:
很抱歉用完整个答案 - 只是为了格式化:) 基本上,来自@tangens 的相同答案,但没有临时文件:
echo "$(cat header.txt sample.txt)" > sample.txt
Note the quotes (") to preserve the line endings, and the fact that the catconcatenation runs in a subprocess - so there shouldn't be the possibility of overwriting a file (the sample.txt) which is open for reading..
请注意引号 ( ") 以保留行尾,以及cat串联在子进程中运行的事实- 因此不应该覆盖sample.txt打开以供阅读的文件 (the )。
Cheers!
干杯!
回答by hluk
Use ed text editor:
使用ed文本编辑器:
echo -e '0r header.txt\nw' | ed sample.txt
or use vi/ex command:
或使用 vi/ex 命令:
vi - +'0r header.txt|wq' sample.txt
But I don't see any way to run a command in sed.
但是我看不到在 sed 中运行命令的任何方法。
回答by tangens
cat header.txt sample.txt > temp.txt
mv temp.txt sample.txt
回答by codaddict
If you really want to do it using sed:
如果你真的想使用sed:
INFO=$(cat header.txt) # read the contents of file headers.txt into var INFO
sed -i "1i$INFO" sample.txt # insert $INFO, use -i to change the file inplace
I would however use the catbased method.
然而,我会使用cat基于方法。
回答by Gaius
Why not do this the other way round, e.g. append your source files to your copyright file?
为什么不反过来这样做,例如将您的源文件附加到您的版权文件?
The 'cat' command is actually short for 'concatenate', you can simply say
'cat' 命令实际上是 'concatenate' 的缩写,你可以简单地说
for f in *.c
do
cp $f /tmp
cat copyright.txt /tmp/$f >$f
done

