- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to join lines of two files on a common field in Linux?
To join lines of two files on a command field, we use the join command in the Linux system. The join command is used to join lines of two files on a common field in the Linux system. If we have two files and we want to join lines of files then no need to combine both files. We can join lines without combining files using the join command. By default, the join field is the first, delimited by blanks.
Syntax
The general syntax of the join command is as follows −
join [OPTION]... FILE1 FILE2
Note – If FILE1 or FILE2 or both no given then the join command read from standard input.
Brief description of options available in the fmt command.
Sr.No. | Option & Description |
---|---|
1 | -a, FILENUM Also print not pairable lines from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or FILE2 |
2 | -e EMPTY Replace missing input fields with EMPTY |
3 | -i, --ignore-case Ignore differences in case when comparing fields |
4 | -j FIELD Equivalent to ‘-1 FIELD -2 FIELD’ |
5 | -o FORMAT Obey FORMAT while constructing output lines |
6 | -t CHAR Use CHAR as input and output field separator |
7 | -v FILENUM Like -a FILENUM, but suppress joined output lines |
8 | -1 FIELD Join on this FIELD of file 1 |
9 | -2 FIELD Join on this FIELD of file 2 |
10 | --header Treat the first line in each file as field headers, print them without trying to pair them |
11 | -z, --zero -terminated Line delimiter is NULL, not newline |
12 | --help Displays a help message and then exits. |
13 | --version It gives info about the version and then exits. |
To join lines of two files and prints on standard output, we use the join command without any other option as shown below.
First, we will create two sorted files using the cat command in the Linux system.
$ cat > text1.txt 1 SID 2 SAYANI 3 GAURAV 4 VIKASH $ cat >text2.txt 1 23 2 19 3 22 4 21
Then, we will use the join command to join lines in the Linux system as shown below.
$ join text1.txt text2.txt 1 SID 23 2 SAYANI 19 3 GAURAV 224 VIKASH 21
Note - In order to combine two files, it is necessary to have some common field in both files.
We can create a new file in which the common field is joined as shown below.
$ join text1.txt text2.txt >jointext.txt
To check more information about the join command, we use the --help option with the join command in the Linux operating system as shown below.
$ join --help
To check version information of the join command, we use the --version option with the join command in the Linux operating system as shown below.
$ join --version