Editing
Merge multiple text files into one file
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Steps == Step 1: check the last line of text file is [[Return symbol | newline]] (return symbol)<ref>[https://stackoverflow.com/questions/34943632/linux-check-if-there-is-a-newline-at-the-end-of-a-file eof - Linux - check if there is a newline at the end of a file - Stack Overflow]</ref> * {{kbd | key=<nowiki>tail -c 1 file.txt</nowiki>}} on {{Linux}}. Parameter "-c <span style="text-decoration: underline;">number</span>: The location is <span style="text-decoration: underline;">number</span> bytes." quoted from the [http://man7.org/linux/man-pages/man1/tail.1.html commands manual]. If the last line is newline, returned result will be empty. {{exclaim}} How to check multiple files? * (optional) If the last line is not newline, you may add the new line manually. See details on [https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file bash - How to add a newline to the end of a file? - Unix & Linux Stack Exchange] Step 2: Merge the content * {{kbd | key=<nowiki>copy *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>copy file1.txt file2.txt > bundle.txt </nowiki>}} on {{Win}} * {{kbd | key=<nowiki>cat *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>cat file1.txt file2.txt > bundle.txt </nowiki>}} on {{Linux}} & {{Mac}}<ref>[http://www.maclife.com/article/columns/terminal_101_join_multiple_files_together_cat Terminal 101: Join Multiple Files Together with Cat]</ref><ref>[https://unix.stackexchange.com/questions/3770/how-to-merge-all-text-files-in-a-directory-into-one How to merge all (text) files in a directory into one? - Unix & Linux Stack Exchange]</ref> Step 3: (optional) Remove the duplicated lines * {{kbd | key=sort -us -o bundle_unique.txt bundle.txt}}<ref>[http://unix.stackexchange.com/questions/19641/how-to-remove-duplicate-lines-in-a-large-multi-gb-textfile linux - How to remove duplicate lines in a large multi-GB textfile? - Unix & Linux Stack Exchange]</ref> OS: {{Linux}}, cygwin of {{Win}} "-u means Unique keys; -s means stable sort; -o means output" quoted from [https://www.computerhope.com/unix/usort.htm sort] manual. Step 4: (optional) Remove the heading of CSV file Step 5: Verify the merged file * count number of lines {{kbd | key=<nowiki>wc -l merged.filename</nowiki>}}<ref>[https://www.tecmint.com/wc-command-examples/ 6 WC Command Examples to Count Number of Lines, Words, Characters in Linux]</ref> == References == <references /> [[Category:Data Science]] [[Category:String manipulation]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Exclaim
(
edit
)
Template:Kbd
(
edit
)
Template:Linux
(
edit
)
Template:Mac
(
edit
)
Template:Win
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information