如何在shell脚本/ bash中将输出附加到文本文件的末尾

时间:2021-08-01 18:28:00

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

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

8 个解决方案

#1


388  

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

将输出定向到文件时使用>>而不是>:

your_command >> file_to_append_to

If file_to_append_to does not exist, it will be created.

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

Example:

例:

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

#2


59  

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文件中。

#3


25  

Use command >> file_to_append_to to append to a file.

使用命令>> file_to_append_to追加到文件。

For example echo "Hello" >> testFile.txt

例如echo“Hello”>> testFile.txt

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

注意:如果您只使用一个>,则会完全覆盖文件的内容。为确保不会发生这种情况,您可以将-o noclobber设置为.bashrc。

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

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

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

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

#4


17  

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

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

#5


14  

To append a file use >>

要附加文件,请使用>>

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

then the output should be

然后输出应该是

hello world   
hello siva

To overwrite a file use >

要覆盖文件,请使用>

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

then the out put is

那么输出就是

hello tom

你好汤姆

#6


12  

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

#7


7  

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'

如果你想在文件末尾附加,那么---->记住'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

#8


6  

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. 在shell脚本中使用>>将内容追加到特定文件。文件名可以修复或使用某种模式。
  3. Setup a hourly cronjob to trigger the shell script
  4. 设置每小时cronjob以触发shell脚本

#1


388  

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

将输出定向到文件时使用>>而不是>:

your_command >> file_to_append_to

If file_to_append_to does not exist, it will be created.

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

Example:

例:

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

#2


59  

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文件中。

#3


25  

Use command >> file_to_append_to to append to a file.

使用命令>> file_to_append_to追加到文件。

For example echo "Hello" >> testFile.txt

例如echo“Hello”>> testFile.txt

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

注意:如果您只使用一个>,则会完全覆盖文件的内容。为确保不会发生这种情况,您可以将-o noclobber设置为.bashrc。

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

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

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

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

#4


17  

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

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

#5


14  

To append a file use >>

要附加文件,请使用>>

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

then the output should be

然后输出应该是

hello world   
hello siva

To overwrite a file use >

要覆盖文件,请使用>

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

then the out put is

那么输出就是

hello tom

你好汤姆

#6


12  

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

#7


7  

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'

如果你想在文件末尾附加,那么---->记住'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

#8


6  

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. 在shell脚本中使用>>将内容追加到特定文件。文件名可以修复或使用某种模式。
  3. Setup a hourly cronjob to trigger the shell script
  4. 设置每小时cronjob以触发shell脚本