How to append contents of multiple files into one file
You need the cat
(short for concatenate) command, with shell redirection (>
) into your output file
cat 1.txt 2.txt 3.txt > 0.txt
Appending multiple files into one file
Original answer to original question
Well, the easiest way is probably cp
:
cat file1 file2 file3 > Newfile.txt
cp Newfile.txt AnotherFile.txt
Failing that, you can use:
cat file1 file2 file3 > Newfile.txt
AnotherFile=$(cat Newfile.txt)
echo "$AnotherFile" > AnotherFile.txt
Revised answer to revised question
The original question had echo "$AnotherFile"
as the third line; the revised question has sort $AnotherFile | uniq -c
as the third line.
Assuming that sort $AnotherFile
is not sorting all the contents of the files mentioned in the list created from concatenating the original files (that is, assuming that file1
, file2
and file3
do not contain just lists of file names), then the objective is to sort and count the lines found in the source files.
The whole job can be done in a single command line:
cat file1 file2 file3 | tee Newfile.txt | sort | uniq -c
Or (more usually):
cat file1 file2 file3 | tee Newfile.txt | sort | uniq -c | sort -n
which lists the lines in increasing order of frequency.
If you really do want to sort the contents of the files listed in file1
, file2
, file3
but only list the contents of each file once, then:
cat file1 file2 file3 | tee Newfile.txt | sort -u | xargs sort | sort | uniq -c
It looks weird having three sort-related commands in a row, but there is justification for each step. The sort -u
ensures each file name is listed once. The xargs sort
converts a list of file names on standard input into a list of file names on the sort
command line. The output of this is the sorted data from each batch of files that xargs
produces. If there are so few files that xargs
doesn't need to run sort
more than once, then the following plain sort
is redundant. However, if xargs
has to run sort
more than once, then the final sort has to deal with the fact that the first lines from the second batch produced by xargs sort
probably come before the last lines produced by the first batch produced by xargs sort
.
This becomes a judgement call based on knowledge of the data in the original files. If the files are small enough that xargs
won't need to run multiple sort
commands, omit the final sort
. A heuristic would be "if the sum of the sizes of the source files is smaller than the maximum command line argument list, don't include the extra sort".
Python append multiple files in given order to one big file
Just using simple file IO:
# tempfiles is a list of file handles to your temp files. Order them however you like
f = open("bigfile.txt", "w")
for tempfile in tempfiles:
f.write(tempfile.read())
That's about as OS agnostic as it gets. It's also fairly simple, and the performance ought to be about as good as using anything else.
Concatenate or Append Multiple Files in Numerical Order to New File with Python
You have:
tempfiles = ['output0.xyz', 'output1.xyz']
f = open("bigtextfile.xyz", "w")
for tempfile in tempfiles:
f.write(tempfile.read())
But tempfile
in your loop is being assigned the name of a file, such as 'output0.xyz', and being a string it has no method read
; you need to use that as an argument to open
:
tempfiles = ['output0.xyz', 'output1.xyz']
with open("bigtextfile.xyz", "w") as f:
for tempfile in tempfiles:
with open(tempfile) as infile:
f.write(infile.read())
Note that I am using with
context managers, which will automatically close the files when the blocks they govern terminate.
Update
This code will try to process files ouput0.xyz, output1.xyz, ... and will terminate until it gets an open error. It does not explicitly read the directory but rather assumes that the files are contiguously number 0, 1, 2 ... N:
from itertools import count
with open("bigtextfile.xyz", "w") as f:
for suffix in count(0): # suffix takes on values 0, 1, 2, ...
try:
with open(f'output{suffix}.xyz') as infile:
f.write(infile.read())
except Exception:
break # get out of loop when we can no longer open outputN.xyz for some value N
Appending multiple files to single output file but w/ message between the files
Assuming you have Gnu awk:
awk 'BEGINFILE{print "============="}1' lab*.txt > final.txt
The BEGINFILE
special pattern (a gawk extension) triggers just before the first line of each file. It defines the variable FILENAME
in case you want to include the name in the separator line.
The 1
at the end is a pattern which is always true. Since it has no action, the default action is executed, which prints the line.
This will print the line at the beginning as well. If you really don't want that, you could add a check:
awk 'BEGINFILE{if(nfiles++)print "============="}1' lab*.txt > final.txt
There is nothing special about nfiles
. Like any other awk variable, it is effectively initialised to 0, and the postfix ++
increments it, but only after its value is returned.
Related Topics
How to Build a Linux Kernel Module So That It Is Compatible with All Kernel Releases
Is There a Special Restriction on Commands Executed by Cron
Execute Script as Another User Whilst Not Being Root
How to Replace:Characters with Newline
Modify Http Headers on System Level (Linux)
How to Run Multiple Tor Processes at Once with Different Exit Ips
Abuse Curl to Communicate with Redis
Permission Denied When Trying to Append a File to a Root Owned File with Sudo
Pyqt5 Error "Pycapsule_Getpointer Called with Incorrect Name"
Chrome Certificate Selection Appears Multiple Times
Boost with Qt Creator and Linux
Overwrite Input File Using Awk
Mercurial Hg No Changes Found - Can't Hg Push Out
Writing a Program for Hiding Processes from Ps Command Result
Svn Checkout the Contents of a Folder, Not the Folder Itself