[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

select and order columns in linux join


I can use this command
 join -1 x -2 y file1 file2

to join 2 files on the xth column of file1 and the yth column of file2.

But is there a way to only retrieve certain columns from file1 and file2 and to order these columns in the new file

e.g. file 1 has cols 1 2 3 and file 2 has cols 1 2 3 4
I want to join on col1 in both files and retrieve all cols from file 1 and only col 4 from file 2

in this example cols 1 2 3 are the same in both files so i am effectively merging them to add col4 but I have more complicated examples than this where the 2 files only have a couple of columns in common and I want to merge them into a unifed file containing one copy of each column

2 Solutions
Would you consider something like
join -1 1 -2 1 file1 file2 | awk '{print $1,$2,$3,$7}'
> order these columns in the new file
Assume you want to sort the col1
Use farzanj's script and pipe to sort
join -1 1 -2 1 file1 file2 | awk '{print $1,$2,$3,$7}' | sort -k1,1 > new_file

andiejeAuthor Commented:

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now