Skip to content
Advertisement

conditionally merge files by appending in bash

I have two files and would like to merge them – by appending to the last line.

file1.txt content

JavaScript

file2.txt content

JavaScript

Desired outcome – file3.txt

JavaScript

How do I go about this, please?

Note, zero is in place for null in the Stack line.

Advertisement

Answer

Using awk:

JavaScript

When file2.txt is read, store field2 in array a using field1 as index.
When file1.txt is read, print the line plus the value of array a at index of field1 if present or 0 otherwise.
Redirect output to file3.txt.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement