bash 如何将输出附加到文本文件的末尾

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

How to append output to the end of a text file

bashshell

提问by vincy

How do I append the output of a command to the end of a text file?

如何将命令的输出附加到文本文件的末尾?

回答by aioobe

Use >>instead of >when directing output to a file:

在将输出定向到文件时使用>>代替>

your_command >> file_to_append_to

If file_to_append_todoes not exist, it will be created.

如果file_to_append_to不存在,它将被创建。

Example:

例子:

$ echo "hello" > file
$ echo "world" >> file
$ cat file 
hello
world

回答by yoctotutor.com

To appenda file use >>

append文件使用 >>

echo "hello world"  >> read.txt   
cat read.txt     
echo "hello siva" >> read.txt   
cat read.txt

then the output should be

那么输出应该是

hello world   # from 1st echo command
hello world   # from 2nd echo command
hello siva

To overwritea file use >

overwrite文件使用 >

echo "hello tom" > read.txt
cat read.txt  

then the out put is

那么输出是

hello tom

hello tom

回答by walta

You can use the >> operator. This will append data from a command to the end of a text file.

您可以使用 >> 运算符。这会将命令中的数据附加到文本文件的末尾。

To test this try running:

要测试这个尝试运行:

echo "Hi this is a test" >> textfile.txt

Do this a couple of times and then run:

这样做几次,然后运行:

cat textfile.txt

You'll see your text has been appended several times to the textfile.txt file.

您会看到您的文本已多次附加到 textfile.txt 文件中。

回答by craft

Use command >> file_to_append_toto append to a file.

使用command >> file_to_append_to附加到文件中。

For example echo "Hello" >> testFile.txt

例如 echo "Hello" >> testFile.txt

CAUTION:if you only use a single >you will overwrite the contents of the file. To ensure that doesn't ever happen, you can add set -o noclobberto your .bashrc.

注意:如果您只使用一个,>您将覆盖文件的内容。为确保永远不会发生这种情况,您可以添加set -o noclobber到您的.bashrc.

This ensures that if you accidentally type command > file_to_append_toto an existing file, it will alert you that the file exists already. Sample error message: file exists: testFile.txt

这可确保如果您不小心键入command > file_to_append_to现有文件,它会提醒您该文件已存在。示例错误消息:file exists: testFile.txt

Thus, when you use >it will only allow you to create a new file, not overwrite an existing file.

因此,当您使用>它时,它只会允许您创建一个新文件,而不会覆盖现有文件。

回答by Nev Stokes

Use the >>operator to append text to a file.

使用>>运算符将文本附加到文件。

回答by victortv

Using teewith option -a (--append)allows you to append to multiple files at once and also to use sudo(very useful when appending to protected files). Besides that, it is interesting if you need to use other shells besides bash, as not all shells support the > and >> operators

使用带有选项-a (--append) 的tee允许您一次附加到多个文件,还可以使用sudo(在附加到受保护的文件时非常有用)。除此之外,如果您需要使用除 bash 之外的其他 shell,这很有趣,因为并非所有 shell 都支持 > 和 >> 运算符

echo "hello world" | sudo tee -a output.txt

This threadhas good answers about tee

线程对 T 恤有很好的回答

回答by jm666

for the whole question:

对于整个问题:

cmd >> o.txt && [[ $(wc -l <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txt

this will append 720 lines (30*24) into o.txt and after will rename the file based on the current date.

这会将 720 行 (30*24) 附加到 o.txt 中,之后将根据当前日期重命名文件。

Run the above with the cron every hour, or

每小时用 cron 运行上面的命令,或者

while :
do
    cmd >> o.txt && [[ $(wc -l <o.txt) -eq 720 ]] && mv o.txt $(date +%F).o.txt
    sleep 3600
done

回答by Braden Holt

I would use printf instead of echo because it's more reliableand processes formatting such as new line \nproperly.

我会使用 printf 而不是 echo 因为它更可靠并且可以\n正确处理诸如换行之类的格式。

This example produces an output similar to echo in previous examples:

此示例生成类似于前面示例中的 echo 的输出:

printf "hello world"  >> read.txt   
cat read.txt
hello world

However if you were to replace printf with echo in this example, echo would treat \n as a string, thus ignoring the intent

但是,如果在此示例中用 echo 替换 printf,则 echo 会将 \n 视为字符串,从而忽略意图

printf "hello\nworld"  >> read.txt   
cat read.txt
hello
world

回答by sballmer

I'd suggest you do two things:

我建议你做两件事:

  1. Use >>in your shell script to append contents to particular file. The filename can be fixed or using some pattern.
  2. Setup a hourly cronjob to trigger the shell script
  1. 使用>>你的shell脚本追加内容特定的文件。文件名可以是固定的或使用某种模式。
  2. 设置每小时一次的 cronjob 来触发 shell 脚本

回答by Mangesh Bhapkar

For example your file contains :

例如,您的文件包含:

 1.  mangesh@001:~$ cat output.txt
    1
    2
    EOF

if u want to append at end of file then ---->remember spaces between 'text' >> 'filename'

如果你想在文件末尾附加,那么---->记住“文本”>>“文件名”之间的空格

  2. mangesh@001:~$ echo somthing to append >> output.txt|cat output.txt 
    1
    2
    EOF
    somthing to append

And to overwrite contents of file :

并覆盖文件的内容:

  3.  mangesh@001:~$ echo 'somthing new to write' > output.tx|cat output.tx
    somthing new to write