How to Merge Data in Linux


Data merging is the process of bringing together disparate data when you want it to be in one place. It’s feasible that you split a single file into several files and now want to combine them all into one, or that you have multiple log files that you want to combine.

For some reason, combining several text files into a single file on Linux is quite straightforward. The main comparison of several Ubuntu 22.04 commands that may be used to merge data into a single place is the article’s main objective.

This method is typically necessary when you have raw data that is saved across many files, workbooks, or data tables that you wish to evaluate simultaneously.

The following steps describe how to combine data using several Ubuntu commands:

Use Cat Command to Merge Data

Concatenate, sometimes known as “the cat,” is preloaded in newer versions of Ubuntu; if you are using an earlier version, you must install it. It is a widely used command that shows all of the data from a file on the terminal screen after reading every bit of data from the file.

We can create, examine, and merge files thanks to it. Your terminal will get cluttered, and navigating will be challenging if you use the cat command to show the contents of large text file(s) to the terminal.

The command shown below will combine the information from two different file and then display it on the screen in the same order as the filenames are listed.

$ cat linux1.txt linux2.txt

Merge-Data-Linux

Merge-Data-Linux

Merge and Store Data in a File

With the help of the cat command and the redirection operator “>,” we can now combine data from many files and save it in a different file. The program listed below uses the cat command to combine the data from “linux1” and “linux2” and save it in “merged linux.”

$ cat linux1.txt linux2.txt > merged_linux.txt

Merge-Data-Linux

Use Sed Command to Merge Data

The pre-installed SED program, also known as stream editor in Linux, may execute a number of file actions like searching, finding and replacing, insertion, and deletion if it is not already installed.

It is far quicker to discover and modify anything in a file using SED than it is to open the file in VI editor first and then make changes to it.

$ sed h linux1.txt linux2.txt > merged_linux1.txt

Merge-Data-Linux

Merge-Data-Linux

Use Merge Command to Merge Data

The merge command also creates a new file by combining the contents of two other files, but it does so in a different way. An original file and two modified copies of the original are compared line by line in three files using the merge tool to resolve conflicts between the two sets of modifications and produce a single merged file that contains the changes from both files. The following command must be used to install the “merge” command because it is not preinstalled:

$ sudo apt install rcs
$ merge merge_linux2.txt linux1.txt linux2.txt 

Merge-Data-Linux

Use “For loop” to Merge data

The “for loop” can do away with the requirement to mention the file names directly. The consistency of the filenames is required for this to work. The file names in our case are formatted as follows: linux{1,2}.txt

Using a for loop and the redirection operator “>,” the command below will combine the data from “linux1” and “linux2” and save it in “merged Linux”.

$ for i in {1,2}; do cat “linux$i.txt” >> merged_linux3.txt; done

Merge-Data-Linux

Conclusion

Sometimes you want a single location where many data kinds from various locations are kept. You must do this on Linux by merging the data in several ways. In-depth discussions of four methods to combine data on Ubuntu utilizing the cat, sed, merge, and for loop commands were covered in this article. You are free to use any strategy that seems simple to you.

Print Friendly, PDF & Email
Categories
Tags