Unix 将多个文件追加到一个文件中

Unix 将多个文件追加到一个文件中,unix,Unix,我使用cat命令将多个数据文件追加到单个数据文件中。如何将单个文件的值分配到新文件中 我正在使用命令: cat file1 file2 file3 > Newfile.txt AnotherFile=`cat Newfile.txt` sort $AnotherFile | uniq -c 它显示错误,如无法打开其他文件 如何将此新文件值分配到另一个文件?原始问题的原始答案 嗯,最简单的方法可能是cp: cat file1 file2 file3 > Newfile.txt cp

我使用
cat
命令将多个数据文件追加到单个数据文件中。如何将单个文件的值分配到新文件中

我正在使用命令:

cat file1 file2 file3 > Newfile.txt
AnotherFile=`cat Newfile.txt`
sort $AnotherFile | uniq -c
它显示错误,如无法打开其他文件 如何将此新文件值分配到另一个文件?

原始问题的原始答案 嗯,最简单的方法可能是
cp

cat file1 file2 file3 > Newfile.txt
cp Newfile.txt AnotherFile.txt
否则,您可以使用:

cat file1 file2 file3 > Newfile.txt
AnotherFile=$(cat Newfile.txt)
echo "$AnotherFile" > AnotherFile.txt
修订问题的修订答覆 原始问题的第三行是
echo“$AnotherFile”
;修改后的问题将对$AnotherFile | uniq-c排序作为第三行

假设
sort$AnotherFile
没有对通过连接原始文件创建的列表中提到的文件的所有内容进行排序(即,假设
file1
file2
file3
不仅包含文件名列表),然后,目标是对源文件中找到的行进行排序和计数

整个作业可以在单个命令行中完成:

cat file1 file2 file3 | tee Newfile.txt | sort | uniq -c
或者(通常是):

它按频率的递增顺序列出行

如果确实要对
file1
file2
file3
中列出的文件内容进行排序,但只列出每个文件的内容一次,则:

cat file1 file2 file3 | tee Newfile.txt | sort -u | xargs sort | sort | uniq -c
一行中有三个与排序相关的命令看起来很奇怪,但每一步都有理由。排序-u确保每个文件名只列出一次。
xargs排序
将标准输入上的文件名列表转换为
sort
命令行上的文件名列表。它的输出是
xargs
生成的每批文件的排序数据。如果文件太少,
xargs
不需要多次运行
sort
,那么下面的普通
sort
是多余的。但是,如果
xargs
必须多次运行
sort
,则最终排序必须处理这样一个事实,即
xargs sort
生产的第二批产品的第一行可能在
xargs sort
生产的第一批产品的最后一行之前


这将成为基于原始文件中数据知识的判断调用。如果文件足够小,以至于
xargs
不需要运行多个
sort
命令,则省略最后的
sort
。一种启发式方法是“如果源文件的大小之和小于最大命令行参数列表,则不包括额外的排序”。

您可以一次性完成:

# Write to two files at once. Both files have a constantly varying
# content until cat is finished.
cat file1 file2 file3 | tee Newfile.txt> Anotherfile.txt

# Save the output filename, just in case you need it later
filename="Anotherfile.txt"

# This reads the contents of Newfile into a variable called AnotherText
AnotherText=`cat Newfile.txt`

# This is the same as "cat Newfile.txt"
echo "$AnotherText" 

# This saves AnotherText into Anotherfile.txt

echo "$AnotherText" > Anotherfile.txt

# This too, using cp and the saved name above
cp Newfile.txt "$filename"
如果要一次性创建第二个文件,这是一种常见模式:

# During this process the contents of tmpfile.tmp is constantly changing
{ slow process creating text } > tmpfile.tmp

# Very quickly create a complete Anotherfile.txt
mv tmpfile.tmp Anotherfile.txt

在此附加模式下生成文件并重定向

touch Newfile.txt
cat files* >> Newfile.txt

您想使用粘贴。您所说的“分配到另一个文件”是什么意思
cp Newfile.txt yetanotherfile.txt
touch Newfile.txt
cat files* >> Newfile.txt