Find the answer to your Linux question:
Results 1 to 3 of 3
I am working on Sun OS and utilizing the join command to join 2 files file1.txt 12.116.0.220|12.116.0.221|1.536|X|X|X|X|X|X|X 12.116.0.240|12.116.0.241|1.536|X|X|X|X|X|X|X file2.txt 12.116.0.221 |-1 |-1 |-1 |-1 12.116.0.241 |1 |1 |1 |1 I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2011
    Posts
    2

    join command usage issue


    I am working on Sun OS and utilizing the join command to join 2 files

    file1.txt

    12.116.0.220|12.116.0.221|1.536|X|X|X|X|X|X|X
    12.116.0.240|12.116.0.241|1.536|X|X|X|X|X|X|X

    file2.txt

    12.116.0.221 |-1 |-1 |-1 |-1
    12.116.0.241 |1 |1 |1 |1


    I want to join these 2 files based on the 2nd field in file1.txt
    tried using the below command
    join -a 1 -e " " -t: file1.txt file2.txt >file3.dat

    I am not getting file3.dat in the expected format.

    Can any one please help

  2. #2
    Just Joined!
    Join Date
    Jun 2011
    Posts
    2
    a small correction. the command i used is:

    join -a 1 -e " " -t"\|" -j1 2 file1.txt file2.txt >file3.dat

  3. #3
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    640
    Quote Originally Posted by easwaranvp View Post
    I am working on Sun OS and utilizing the join command to join 2 files

    file1.txt

    12.116.0.220|12.116.0.221|1.536|X|X|X|X|X|X|X
    12.116.0.240|12.116.0.241|1.536|X|X|X|X|X|X|X

    file2.txt

    12.116.0.221 |-1 |-1 |-1 |-1
    12.116.0.241 |1 |1 |1 |1


    I want to join these 2 files based on the 2nd field in file1.txt
    tried using the below command
    join -a 1 -e " " -t: file1.txt file2.txt >file3.dat

    I am not getting file3.dat in the expected format.

    Can any one please help
    The problem that you have is that the key in the second file contains a trailing space. You have lots of spaces in fields in the file2.txt

    file1.txt
    Code:
    12.116.0.220|12.116.0.221|1.536|X|X|X|X|X|X|X
    12.116.0.240|12.116.0.241|1.536|X|X|X|X|X|X|X
    file2.txt
    Code:
    12.116.0.221|-1 |-1 |-1 |-1
    12.116.0.241|1 |1 |1 |1
    The command:
    Code:
    join -a 1  -e " " -t\|  -1 2 -2 1 file1.txt file2.tx >file3.datt
    Note not using the option "-j2" as it is a short cut to specify both fields are the second. When the fields are differnt then you need to use both the "-1 2" and "-2 1" options.

    The output is:
    Code:
    12.116.0.221|12.116.0.220|1.536|X|X|X|X|X|X|X|-1 |-1 |-1 |-1
    12.116.0.241|12.116.0.240|1.536|X|X|X|X|X|X|X|1 |1 |1 |1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •