将数据从一个文件前置到另一个文件

时间:2022-04-17 16:02:43

How do I prepend the data from file1.txt to file2.txt?

如何将file1.txt中的数据添加到file2.txt?

5 个解决方案

#1


8  

The following command will take the two files and merge them into one

以下命令将获取这两个文件并将它们合并为一个文件

cat file1.txt file2.txt > file3.txt; mv file3.txt file2.txt

#2


5  

You can do this in a pipeline using sponge from moreutils:

您可以使用moreutils中的海绵在管道中执行此操作:

cat file1.txt file2.txt | sponge file2.txt

#3


3  

Another way using GNU sed:

使用GNU sed的另一种方法:

sed -i -e '1rfile1.txt' -e '1{h;d}' -e '2{x;G}' file2.txt

That is:

  • On line 1, append the content of the file file1.txt
  • 在第1行,附加文件file1.txt的内容

  • On line 1, copy pattern space to hold space, and delete pattern space
  • 在第1行,复制图案空间以保留空间,并删除图案空间

  • On line 2, exchange the content of the hold and pattern spaces, and append the hold space to pattern space
  • 在第2行,交换保持和模式空间的内容,并将保留空间附加到图案空间

The reason it's a bit tricky is that the r command appends content, and line 0 is not addressable, so we have to do it on line 1, moving the content of the original line out of the way and then bringing it back after the content of the file is appended.

它有点棘手的原因是r命令附加内容,而第0行不可寻址,所以我们必须在第1行进行,将原始行的内容移开,然后在内容之后将其恢复该文件的附加。

#4


0  

The way of writing file is like 1). append at the end of the file or 2). rewrite that file.

写文件的方式就像1)。附加在文件的末尾或2)。重写该文件。

If you want to put the content in file1.txt ahead of file2.txt, I'm afraid you need to rewrite the combined fine.

如果你想将文件中的内容放在file2.txt之前,我恐怕你需要重写合并罚款。

#5


0  

This script uses a temporary file. It makes sure that the temporary file is not accessible by other users, and cleans it up at the end.

此脚本使用临时文件。它确保其他用户无法访问临时文件,并在最后清除它。

If your system, or the script crashes, you will need to clean up the temporary file manually. Tested on Bash 4.4.23, and Debian 10 (Buster) Gnu/Linux.

如果您的系统或脚本崩溃,您将需要手动清理临时文件。测试了Bash 4.4.23和Debian 10(Buster)Gnu / Linux。

#!/bin/bash
#
# ---------------------------------------------------------------------------------------------------------------------- 
# usage [ from, to ]
#       [ from, to ]
# ---------------------------------------------------------------------------------------------------------------------- 
# Purpose:
# Prepend the contents of file [from], to file [to], leaving the result in file [to].
# ---------------------------------------------------------------------------------------------------------------------- 

# check 
[[ $# -ne 2 ]] && echo "[exit]: two filenames are required" >&2 && exit 1

# init
from="$1"
to="$2"
tmp_fn=$( mktemp -t TEMP_FILE_prepend.XXXXXXXX )
chmod 600 "$tmp_fn"

# prepend
cat "$from" "$to" > "$tmp_fn"
mv "$tmp_fn" "$to"

# cleanup
rm -f "$tmp_fn"

# [End]

#1


8  

The following command will take the two files and merge them into one

以下命令将获取这两个文件并将它们合并为一个文件

cat file1.txt file2.txt > file3.txt; mv file3.txt file2.txt

#2


5  

You can do this in a pipeline using sponge from moreutils:

您可以使用moreutils中的海绵在管道中执行此操作:

cat file1.txt file2.txt | sponge file2.txt

#3


3  

Another way using GNU sed:

使用GNU sed的另一种方法:

sed -i -e '1rfile1.txt' -e '1{h;d}' -e '2{x;G}' file2.txt

That is:

  • On line 1, append the content of the file file1.txt
  • 在第1行,附加文件file1.txt的内容

  • On line 1, copy pattern space to hold space, and delete pattern space
  • 在第1行,复制图案空间以保留空间,并删除图案空间

  • On line 2, exchange the content of the hold and pattern spaces, and append the hold space to pattern space
  • 在第2行,交换保持和模式空间的内容,并将保留空间附加到图案空间

The reason it's a bit tricky is that the r command appends content, and line 0 is not addressable, so we have to do it on line 1, moving the content of the original line out of the way and then bringing it back after the content of the file is appended.

它有点棘手的原因是r命令附加内容,而第0行不可寻址,所以我们必须在第1行进行,将原始行的内容移开,然后在内容之后将其恢复该文件的附加。

#4


0  

The way of writing file is like 1). append at the end of the file or 2). rewrite that file.

写文件的方式就像1)。附加在文件的末尾或2)。重写该文件。

If you want to put the content in file1.txt ahead of file2.txt, I'm afraid you need to rewrite the combined fine.

如果你想将文件中的内容放在file2.txt之前,我恐怕你需要重写合并罚款。

#5


0  

This script uses a temporary file. It makes sure that the temporary file is not accessible by other users, and cleans it up at the end.

此脚本使用临时文件。它确保其他用户无法访问临时文件,并在最后清除它。

If your system, or the script crashes, you will need to clean up the temporary file manually. Tested on Bash 4.4.23, and Debian 10 (Buster) Gnu/Linux.

如果您的系统或脚本崩溃,您将需要手动清理临时文件。测试了Bash 4.4.23和Debian 10(Buster)Gnu / Linux。

#!/bin/bash
#
# ---------------------------------------------------------------------------------------------------------------------- 
# usage [ from, to ]
#       [ from, to ]
# ---------------------------------------------------------------------------------------------------------------------- 
# Purpose:
# Prepend the contents of file [from], to file [to], leaving the result in file [to].
# ---------------------------------------------------------------------------------------------------------------------- 

# check 
[[ $# -ne 2 ]] && echo "[exit]: two filenames are required" >&2 && exit 1

# init
from="$1"
to="$2"
tmp_fn=$( mktemp -t TEMP_FILE_prepend.XXXXXXXX )
chmod 600 "$tmp_fn"

# prepend
cat "$from" "$to" > "$tmp_fn"
mv "$tmp_fn" "$to"

# cleanup
rm -f "$tmp_fn"

# [End]

相关文章