Wednesday, May 8, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 442  / 1 Year ago, sat, april 15, 2023, 9:48:52

I have two files and I'm trying to join them at a certain spot. I'd like to join them at the fourth column of the second file using the first column of the first. It's driving me nuts!



Here is what I'm trying:



join -j4 <(sort -k1 FirstFile.txt) <(sort -k4 SecondFile.txt)





FirstFile.txt:



24.136.152.171 US
24.136.152.171 US
24.136.152.171 US





SecondFile.txt



2014-08-03 00:00:00 User 24.136.152.171
2014-08-03 00:00:00 User 24.136.152.171
2014-08-03 00:00:00 User 24.136.152.171





Desired Output:



2014-08-03 00:00:00 User 24.136.152.171 US
2014-08-03 00:00:00 User 24.136.152.171 US
2014-08-03 00:00:00 User 24.136.152.171 US

More From » 12.04

 Answers
7

The default output format of join is to print the join field, then remaining fields from FILE1 and then the remaining fields from FILE2, unless the format is specified with -o. Further, the option -j4 means that join field is the 4th field in both FILE1 and FILE2. So you need to split -j4 to -1 1 -2 4 .



Try this:



join -o '2.1 2.2 2.3 2.4 1.2' -2 4 -1 1 <(sort -k1 FirstFile.txt) <(sort -k4 SecondFile.txt)

[#23621] Sunday, April 16, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antoccasiona

Total Points: 430
Total Questions: 127
Total Answers: 131

Location: Netherlands
Member since Sat, Jun 26, 2021
3 Years ago
antoccasiona questions
Sat, Oct 23, 21, 22:34, 3 Years ago
Sat, Sep 24, 22, 09:39, 2 Years ago
Sun, Jan 15, 23, 11:08, 1 Year ago
;