1

I have 2 files and want to inner join them using awk. This is written using sql server :

SELECT [file1.column1],[file2.column2] FROM file1
INNER JOIN file2 on file2.column1 = file1.column5;

This is the file i want to join:

file1 :

file2:

so the key is column5 file1 and column1 file2.

How to write them in awk language?

Your help will be useful for me to learn this awk.

7
  • Could you possibly share some of the data and what you would want to see given that data? To write the thing with awk (or the standard join utility), things like what delimiter is used between the fields of the text matters.
    – Kusalananda
    Commented Dec 27, 2019 at 9:00
  • welcome to U&L, this could also be done using join. can you provide in your question a sample of data, along with expected result ?
    – Archemar
    Commented Dec 27, 2019 at 9:00
  • i have edited my question with picture @Kusalananda
    – Onta Ss
    Commented Dec 27, 2019 at 9:40
  • i have edited my question with picture @Archemar
    – Onta Ss
    Commented Dec 27, 2019 at 9:40
  • unix.meta.stackexchange.com/q/4086/5132
    – JdeBP
    Commented Dec 27, 2019 at 9:47

2 Answers 2

3

One way:

join -t"|" -1 5 -2 1 -o 1.1 2.2 file1 file2

-1 5 - Use the 5th column of file1 -2 1 - Use the 1st column of file2 -o 1.1 2.2 - Print as output 1st column of 1st file, 2nd column of 2nd file

1
  • just 4 rows showed up and outputing error messages = name file: is not sorted @Guru
    – Onta Ss
    Commented Dec 28, 2019 at 7:12
1

If awk is the tool of choice

awk -F'|' 'NR==FNR{id[$5]=0; next}{if ($1 in id) print $1, $2}' file1 file2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .