bash 将标题添加到多个文本文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13196993/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:41:00  来源:igfitidea点击:

Adding header into multiple text files

bashcommand-lineio

提问by Eric

Possible Duplicate:
Sed/Awk to search and replace/insert text in files

可能的重复:
Sed/Awk 在文件中搜索和替换/插入文本

I would like to know how to add one "header" line into multiple text files contained in one directory. Bash command line would be great!

我想知道如何将一个“标题”行添加到一个目录中包含的多个文本文件中。Bash 命令行会很棒!

Thx.

谢谢。

EDIT

编辑

I found my needs in here: http://perldoc.perl.org/index-faq.htmlenjoy! Here is my answer:

我在这里找到了我的需求:http: //perldoc.perl.org/index-faq.html享受!这是我的回答:

perl -pi -e 'print "**MyHeaderText**\n" if $. == 1' *

回答by Jonathan Leffler

Option 1: Works pretty much any version of Unix

选项 1:几乎适用于任何版本的 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.

这会安全地创建一个临时文件,并确保在合理的信号集合(HUP、INT、QUIT、PIPE 和 TERM)下将其删除。然后主循环将头字符串和文件复制到临时文件,并将临时文件移动到原始文件上,删除任何剩余的文件(以防出现问题)并取消清理,以便外壳程序可以干净地退出。

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 $tmptoo.

如果您的原始文件有多个(硬)链接,或者如果它是一个符号链接,您将失去这些特殊属性。要解决这个问题,您必须使用cp $tmp $file,然后您也必须删除该文件$tmp

If you don't have the mktempcomand, you can use:

如果您没有mktemp命令,则可以使用:

tmp=${TMPDIR:-/tmp}/ins.$$

to generate a name. It is more easily predictable than the name from mktempand less secure, especially if you're running as root. There might also be wisdom in using the current directory as $TMPDIRif you have multiple file systems.

生成名称。它比起名字更容易预测,mktemp而且安全性更低,尤其是当您以 root 身份运行时。像使用$TMPDIR多个文件系统一样使用当前目录也可能是明智之举。

Option 2: GNU sed

选项 2:GNU sed

If you have GNU sedand you don't have symlinks or hard links to deal with, then you can use its -ioption to do an in-place alter.

如果您有 GNUsed并且没有符号链接或硬链接要处理,那么您可以使用它的-i选项进行就地更改。

header="This is the line to be inserted"

for file in "$@"
do
    sed -i -e "1i\
$header" $file
done

This inserts the value of $headerbefore the first line of each file. You could write the edit script into a file and use sed -i -f sed.script $fileto avoid awkward indentation in the loop.

这会$header在每个文件的第一行之前插入值。您可以将编辑脚本写入文件并用于sed -i -f sed.script $file避免循环中尴尬的缩进。

Option 3: Other tools

选项 3:其他工具

There are many other possible techniques. For example, you could use edor exto edit the file. You could use Perl or Python or awkto do the processing.

还有许多其他可能的技术。例如,您可以使用edex来编辑文件。您可以使用 Perl 或 Python 或awk进行处理。