Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to merge and sort files on Linux


Jun 01, 2021 Article blog


Table of contents


There are many ways to merge and sort files on Linux but which one to use depends on what you want to do, such as whether you just want to put the contents of multiple files into one file or organize it in some way to make it easier to use. Next, the editor-in-chief will show you some commands for sorting and merging the contents of a file to let you know their characteristics.

Use cat

If you only want to put a set of files into a single file, cat command is an easy choice. A ll you have to do is enter cat and list them on the command line in the order you want them to be in the merged files. R edirects the output of the command to the file you want to create. I f the file with the specified name already exists, the file is overwritten. For example:

$ cat firstfile secondfile thirdfile > newfile

If you want to add the contents of a series of files to an existing file instead of overwriting it, simply turn > into >>

$ cat firstfile secondfile thirdfile >> updated_file

If the files you want to merge follow some convenient naming conventions, the task may be simpler. I f you can use regular expressions to specify all file names, you do not have to list all files. For example, if the files all end in file as shown above, you can do the following:

$ cat *file > allfiles

Note that the command above adds the contents of the file in alphanumeric order. O n Linux a file named filea is preceded by a file named fileA but after file7 A fter all, when we deal with alphanumeric sequences, we need to consider not only ABCDE but also 0123456789aAbBcCdDeE You can use commands such as ls *file to see the order of the files before they are merged.

Note: First make sure that your command contains all the files you need to merge files, not other files, especially if you use wildcards such as . Don't forget that the files used for the merge will still exist separately, and you may want to delete them after confirming the merge.

(Recommended tutorial: Linux tutorial)

Merge files by time frame

If you want to merge files based on the time period of each file instead of the file name, use the following command:

$ for file in `ls -tr myfile.*`; do  cat $file >> BigFile.$$; done

Using the -tr option (t-time, r-reverse) produces a list of files that are ranked first and earliest. This is useful, for example, if you want to keep logs of some activities and want to add content in the order in which they are executed.

$ in the command above represents $ process ID when the command is run. T his feature is not necessary, but it is almost impossible to inadvertently add to an existing file rather than create a new one. If you use $$, the resulting file might look like this:

$ ls -l BigFile.*
-rw-rw-r-- 1 justme justme   931725 Aug  6 12:36 BigFile.582914

Merge and sort files

Linux provides some interesting ways to sort the contents of files before or after merging.

Sort content by letter

If you want to sort the merged file contents, you can sort the overall contents using the following command:

$ cat myfile.1 myfile.2 myfile.3 | sort > newfile

If you want to group content by file, use the following command to sort each file before adding it to the new file:

$ for file in `ls myfile.?`; do sort $file >> newfile; done

Sort the files numerically

To sort the contents of a file numerically, use the -n option in sort T his option is useful only if the lines in the file start with numbers. K eep in mind that, in the default order, 02 will be less than 1. Use the -n option when you want to make sure that rows are sorted by numbers.

$ cat myfile.1 myfile.2 myfile.3 | sort -n > xyz

If the lines in the file start with a date format like 2020-11-03 or 2020/11/03 (year-and-day format), -n option also lets you sort content by date. Sorting dates in other formats can be tricky and will require more complex commands.

Use paste

paste command allows you to connect file contents line by line. W hen you use this command, the first line of the merged file will contain the first line of each file to merge. Here's an example where I used capital letters to make it easier to see where the rows came from:

$ cat file.a
A one
A two
A three


$ paste file.a file.b file.c
A one   B one   C one
A two   B two   C two
A three B three C thee
        B four  C four
                C five

Redirect the output to another file to save it:

$ paste file.a file.b file.c > merged_content

Alternatively, you can merge the contents of each file on the same line and paste the files together. T his requires the -s (sequence) option. Notice how this output shows the contents of each file:

$ paste -s file.a file.b file.c
A one   A two   A three
B one   B two   B three B four
C one   C two   C thee  C four  C five

Use join

Another command to merge files is join join command lets you merge the contents of multiple files based on a common field. F or example, you might have a file that contains a group of colleagues' phones, and the other contains a colleague's e-mail address, both of which are listed by individual name. You can use join create a file that contains phone and email addresses.

An important limitation is that the rows of the file must be in the same order and include fields for connection in each file.

This is an example command:

$ join phone_numbers email_addresses
Sandra 555-456-1234 [email protected]
Pedro 555-540-5405
John 555-333-1234 [email protected]
Nemo 555-123-4567 [email protected]

In this case, even if additional information is missing, the first field (name) must exist in each file, or the command will fail due to an error. Sorting content can be helpful and may be easier to manage, but you don't need to do so as long as the order is the same.

(Recommended micro-class: Linux micro-class)

summary

On Linux you have many ways to combine and sort data stored in separate files. These methods can make an otherwise cumbersome task extremely simple.

Source: www.toutiao.com/a6863629427096420875/

Here's a description of how W3Cschool编程狮 can merge and sort files on Linux, and I hope it's helpful.