Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to merge and sort files on Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to merge and sort files on the Linux related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this article on how to merge and sort files on the Linux will have a harvest, let's take a look at it.

Use cat

If you only want to put a set of files into a single file, then the cat command is an easy choice. All you have to do is type cat and list the files on the command line in the order you want them in the merge files. Redirect the output of the command to the file you want to create. If a file with the specified name already exists, the file will be 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 change > to > >.

$cat firstfile secondfile thirdfile > > updated_file

If the files you want to merge follow some convenient naming conventions, the task may be easier. If you can specify all file names using regular expressions, you don't 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 above command adds the contents of the file alphabetically. On Linux, a file named filea will come before the file named fileA, but will come after file7. After all, when dealing 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 files before merging.

Note: first make sure that your command contains all the files needed in the merge file, not any 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 limit

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 in the earliest order first. For example, this is useful if you want to keep a log of some activities and want to add content in the order in which the activities are performed.

The $$in the above command represents the process ID when the command is run. It is not necessary to use this feature, but it is almost impossible to inadvertently add to an existing file instead of creating 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 the merge.

Sort the content alphabetically

If you want to sort the contents of the merged file, 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, sort each file using the following command, and then add it to the new file:

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

Sort files digitally

To sort the contents of a file numerically, use the-n option in sort. This option is useful only if the lines in the file begin with a number. Remember, 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 begin with a date format such as 2020-11-03 or 2020-11-03, the-n option also allows you to sort the content by date. Sorting dates in other formats will be tricky and will require more complex commands.

Use paste

The paste command allows you to connect the contents of the file line by line. When you use this command, the first line of the merged file contains the first line of each file you want to merge. The following is an example, where I use uppercase letters to make it easier to see the source of the line:

$cat file.aA oneA twoA three$ paste file.a file.b file.cA one B one C oneA two B two C twoA 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. This requires the use of the-s (sequence) option. Notice how the output this time displays the contents of each file:

$paste-s file.a file.b file.cA one A two A threeB one B two B threeB fourC one C two C thee C fourC five uses join

Another command to merge files is join. The join command allows you to merge the contents of multiple files based on a common field. For example, you might have a file that contains the phone numbers of a group of colleagues, while the other contains the e-mail addresses of colleagues, both listed by personal name. You can use join to create a file that contains your phone and email address.

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

This is an example command:

$join phone_numbers email_addressesSandra 555-456-1234 bugfarm@gmail.comPedro 555-540-5405John 555-333-1234 john_doe@gmail.comNemo 555-123-4567 cutie@fish.com

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

This is the end of the article on "how to merge and sort files on Linux". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to merge and sort documents on Linux". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report