Linux commands: Difference between revisions

Jump to navigation Jump to search
2,030 bytes removed ,  30 January 2020
Line 59: Line 59:


=== Merge multiple plain text files ===
=== Merge multiple plain text files ===
Step 1: check the last line of text file is [[Return symbol | newline]]<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>
[[Merge multiple text files into one file]]
* {{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}}<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.


== File operation ==
== File operation ==
Anonymous user

Navigation menu