添加包头的多个文本文件 [英] Adding header into multiple text files
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/11563620/sed-awk-to-search-and-replace-insert-text-in-files\">Sed/Awk搜索和替换/插入文件中的文本
块引用>我想知道如何添加一个标头行成包含在一个目录中的多个文本文件。 bash命令行就太棒了!
THX。
的 修改的
我发现我需要这里: http://perldoc.perl.org/index-faq .HTML 享受!
这里是我的回答是:的perl -pi -e打印** ** MyHeaderText \\ n如果$。 == 1'*
解决方案选项1:工作pretty的Unix多任何版本
TMP = $(mktemp的)#创建一个临时文件
陷阱RM -f $ TMP;出口10 1 2 3 13 15标题=这是要被插入的标题行在文件$ @
做
{
回声$头
猫$文件
}&GT; $ TMP
MV $ TMP $文件
DONERM -f $ TMP
陷阱0这将创建一个安全的临时文件,并确保它得到下信号的合理集合中删除(HUP,INT,QUIT,PIPE和期限)。然后,主循环副本的头部字符串和文件到临时,并在原有的移动暂时的,删除任何剩余的文件(万一出事了),并取消清理使外壳可以完全退出。
如果您的原始文件有多个(硬)链接,或者如果它是一个符号链接,你失去了这些特殊属性。为了解决这个问题,你必须使用
CP $ TMP $文件
,然后你要删除的文件中$ TMP
了。如果你没有在
mktemp的
COMAND,你可以使用:TMP = $ {TMPDIR: - / tmp目录} / INS $$。
,以产生一个名称。它更容易从
mktemp的
并不太安全的predictable比的名字,尤其是当你以root身份运行。可能还有智慧在使用当前目录$ TMPDIR
如果您有多个文件系统。选项2:GNU SED
如果您有GNU
SED
和你没有符号链接或硬链接来处理,那么你可以使用它的-i
选择做一个就地改变。头=这是要插入行在文件$ @
做
SED -i -e1I \\
$头$文件
DONE这插入
$头
的每个文件的第一行前的值。你可以写编辑脚本到一个文件中,并使用SED -i -f sed.script $文件
来避免环路尴尬缩进。选项3:其他工具
有许多其它可能的技术。例如,你可以使用
ED
或前
来编辑文件。你可以使用Perl或Python或AWK
做处理。Possible Duplicate:
Sed/Awk to search and replace/insert text in filesI would like to know how to add one "header" line into multiple text files contained in one directory. Bash command line would be great!
Thx.
EDIT
I found my needs in here: http://perldoc.perl.org/index-faq.html enjoy! Here is my answer:
perl -pi -e 'print "**MyHeaderText**\n" if $. == 1' *
解决方案Option 1: Works pretty much any version of Unix
tmp=$(mktemp) # Create a temporary file trap "rm -f $tmp; exit 1" 0 1 2 3 13 15 header="This is the header line to be inserted" for file in "$@" do { echo "$header" cat $file } > $tmp mv $tmp $file done rm -f $tmp trap 0
This creates a temporary file securely and makes sure it gets removed under a reasonable collection of signals (HUP, INT, QUIT, PIPE and TERM). The main loop then copies the header string and the file to the temporary, and moves the temporary over the original, removes any leftover file (in case something went wrong) and cancel the cleanup so the shell can exit cleanly.
If your original file had multiple (hard) links, or if it was a symlink, you lose these special properties. To fix that, you have to use
cp $tmp $file
, and then you have to remove the file in$tmp
too.If you don't have the
mktemp
comand, you can use:tmp=${TMPDIR:-/tmp}/ins.$$
to generate a name. It is more easily predictable than the name from
mktemp
and less secure, especially if you're running as root. There might also be wisdom in using the current directory as$TMPDIR
if you have multiple file systems.Option 2: GNU sed
If you have GNU
sed
and you don't have symlinks or hard links to deal with, then you can use its-i
option to do an in-place alter.header="This is the line to be inserted" for file in "$@" do sed -i -e "1i\ $header" $file done
This inserts the value of
$header
before the first line of each file. You could write the edit script into a file and usesed -i -f sed.script $file
to avoid awkward indentation in the loop.Option 3: Other tools
There are many other possible techniques. For example, you could use
ed
orex
to edit the file. You could use Perl or Python orawk
to do the processing.这篇关于添加包头的多个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!