user2285422
user2285422

Reputation: 125

Appending multiple files into one file

I append multiple data files into a single data file using the cat command. How can I assign that single file value into a new file?

I am using the command:

cat file1 file2 file3 > Newfile.txt
AnotherFile=`cat Newfile.txt`
sort $AnotherFile | uniq -c

it showing error like can not open AnotherFile How to assign this newfile value into another file?

Upvotes: 0

Views: 15011

Answers (3)

user7946703
user7946703

Reputation:

make file and redirectin this in append mode.

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

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753805

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".

Upvotes: 3

LSerni
LSerni

Reputation: 57398

You can probably do that in one go:

# 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"

If you want to create the second file all in one go, this is a common pattern:

# 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

Upvotes: 0

Related Questions